How to check which event is running?

4 replies [Last post]
spider_newgent
User offline. Last seen 4 days 18 hours ago. Offline
Joined: 8 Jun 2011

Hi all.

I have a pretty simple setup with a player character on the center of the screen and several buttons at the bottom of the screen.

Each button triggers a different event, which makes the player run through some sprite sheets in different ways.

At the moment I have it setup so that in each event there is a function to check which frame of the relevant spritesheet is currently displayed.

As my project scales in complexity, I can see it becoming a bit of a problem.

Does anyone know of a way to check which event AND frame are "active" at any given time?

For example, something like;

1
2
3
if player1.event == event1 and player1.currentFrame == x then
--relevant logic here
end

If there is a way to do this, I could have one event listener which effectively filters the logic for each of the buttons, instead of calling/deleting listeners with each and every button press.

Hope my question is clear? I can post a full example if it'd help.

Thanks

Spider.

Replies

peach pellen's picture
peach pellen
User offline. Last seen 1 day 12 hours ago. Offline
Staff
Joined: 12 Apr 2011

An example could be handy here, I'm having some trouble picturing it.

spider_newgent
User offline. Last seen 4 days 18 hours ago. Offline
Joined: 8 Jun 2011

Ok, so in the full program I have a skater displayed in the center of the screen. There are 5 buttons on the bottom of the screen which each do different things to the skater sprite, (grab, flip, turn, crash etc.

The following code is the "grab" event from the "grab trick" button. The "whatGrabFrame" function is there to check which frame is displayed, so that when the player is grabbing the skateboard the sprite sheet playback can be paused, until the finger is released from the button during the ended phase.

Instead of having a function to check what frame is displayed for every button, (which creates an enterFrame listener for every button press) What I'd like to do is have one enterFrame listener which constantly tests for which button has been pressed then filters the results based on what conditions are met. In the below example, when the grab button is pressed and the player1.currentFrame == 4, the playback is paused.

How do I set up a whatFrame() function which filters based on what button has been pressed?

Hope that's a bit clearer? (probably not)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function touchgrab (event)
                if event.phase == "began" then
                
                        player1:prepare("grab")
                        player1:play("grab")
                        
                        local function whatGrabFrame()
                                if player1.currentFrame == 4 then 
                                        player1:pause("grab")
                                end
                                
                                if player1.currentFrame == 8 then
                                        Runtime:removeEventListener("enterFrame", whatGrabFrame)        
                                        resetToIdle()
                                end
                        end
        
                        Runtime:addEventListener("enterFrame", whatGrabFrame)   
                                        
                end --end of began phase
                
                if event.phase == "ended" then 
                        player1.currentFrame = 5
                        player1:play("grab")
                        
                end --end of ended phase
                        
end
grab:addEventListener("touch", touchgrab)

spider_newgent
User offline. Last seen 4 days 18 hours ago. Offline
Joined: 8 Jun 2011

Ok, so I've managed to figure it out.

For anyone who gets themselves similarly tied up in knots the solution is dead simple.

I've used the following function to listen for each condition based on the sprite sequence currently being played, and put my logic there. The old method had an onEnterFrame listener being created and destroyed with each button press which isn't ideal.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function whichSpriteFrame() --controls sprite events. 
 
        ---------------------------------------------GRAB---------------------------------------------------------
        if player1.sequence == "grab" then
        
                if player1.currentFrame == 4 then 
                        player1:pause("grab")
                        Runtime:removeEventListener("enterFrame", whatGrabFrame) --removes the listener 
                end
                
                if player1.currentFrame == 8 then
                        resetToIdle()
                end
        end -- end of grab
 
        ---------------------------------------------FLIP---------------------------------------------------------
 
        if player1.sequence == "flip" then
                if player1.currentFrame == 10 then
                        resetToIdle()
                end
        end
        
        ---------------------------------------------CRASH---------------------------------------------------------
 
        if player1.sequence == "crash" then
                if player1.currentFrame == 9 then
                        resetToIdle()
                end
        end
        
        ---------------------------------------------PUMP---------------------------------------------------------
        
        if player1.sequence == "pump" then
                if player1.currentFrame == 5 then
                        resetToIdle()
                end
        end
 
        ---------------------------------------------TURN---------------------------------------------------------
        
        if player1.sequence == "turn" then
                if player1.currentFrame == 8 then
                        resetToIdle()
                end
        end
        
end --end of whatSpriteFrame Function
 
player1:addEventListener("sprite", whichSpriteFrame) 

peach pellen's picture
peach pellen
User offline. Last seen 1 day 12 hours ago. Offline
Staff
Joined: 12 Apr 2011

Hey, well done - sorry I didn't get back here sooner, would have liked to provide assistance.

Thanks for sharing this, I'm sure it will help others in the future :)

Peach

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.