How to check which event is running?

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;

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. [import]uid: 67933 topic_id: 21472 reply_id: 321472[/import]

An example could be handy here, I’m having some trouble picturing it. [import]uid: 52491 topic_id: 21472 reply_id: 85150[/import]

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)

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)  

[import]uid: 67933 topic_id: 21472 reply_id: 85173[/import]

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.

[code]
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)
[/code] [import]uid: 67933 topic_id: 21472 reply_id: 85213[/import]

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 :slight_smile:

Peach [import]uid: 52491 topic_id: 21472 reply_id: 85272[/import]