change sprite event whilst button is pressed

Hi everyone.

I’m trying to get back into Corona following a hectic Christmas but I’ve hit a snag in handling sprite and touch events at the same time.

I have a skateboarder who needs to perform a grab trick when a button is pressed.

1,On initially pressing the button, he changes from his idle state and starts playing through the animation.

2,If the button is still held down when the skater actually grabs the board, he keeps holding it, (and the score counts up)

3,If the button is released then he plays through the rest of the animation.

I’m stuck on getting him to pause on a certain frame whilst the button is pressed, and listen for the button being released.

Can anyone direct me to a tutorial or post some simple pseudo-code so I know where I’m going wrong?

If you need sample code, I’ll post what I’ve got, but I’m stuck on how to organise functions and getting confused listening for touch and sprite events at the same time…

Thanks in advance.

Dan [import]uid: 67933 topic_id: 20230 reply_id: 320230[/import]

Can you add an if statement at the start of the touch function that checks the frame, then if the frame is X (grab frame) pause the sprite, then on release resume playing it?

Does that make sense? [import]uid: 52491 topic_id: 20230 reply_id: 79105[/import]

Thanks for the reply (again) peach.

I totally get what you’re saying, but Corona seems to over complicate things when you’ve come from Macromedia Director :slight_smile:

Ok, so I’ve got this far.

  
function playerFrame()  
 print ("current frame is".. player1.currentFrame)  
  
end  
Runtime:addEventListener("enterFrame", playerFrame)   
  
player1:prepare("idle")  
player1:play("idle")  
  
function touchgrab (event)  
 if event.phase == "began" then  
  
 player1:prepare("grab")  
 player1:play("grab")  
  
 if player1.currentFrame = 4 then  
 player1:pause("grab")  
 print ("yipee")  
 end  
  
 end  
  
 if event.phase == "ended" then   
 player1:prepare("idle")  
 player1:play("idle")  
 print ("idle")  
 end  
  
 end  
grab:addEventListener("touch", touchgrab)  

The issue I have is that Corona refuses to detect that “player1.currentFrame = 4” even though I can actually see it printing to the terminal.

It also doesn’t print “Yipee” so clearly I’m doing something wrong…

Thanks again for looking. [import]uid: 67933 topic_id: 20230 reply_id: 79232[/import]

This is probably just a typo when you brought the code accross, but this line…

if player1.currentFrame = 4 then   

should be…

if player1.currentFrame == 4 then   

You were missing an equals sign, that being the difference between setting or comparing a variable. [import]uid: 69826 topic_id: 20230 reply_id: 79233[/import]

Thanks for the reply, but I’ve already tried that.

Must have copied the code into the post after I’d been messing with it and forgot to add the equals in.

Any other suggestions as to what the problem might be? [import]uid: 67933 topic_id: 20230 reply_id: 79235[/import]

Hrm, try moving lines 16-19 so that they are BEFORE if event.phase == “began”.

As this is that part of the function would only be called if you released the button and pressed it again when the sprite was at frame 4.

Peach :slight_smile: [import]uid: 52491 topic_id: 20230 reply_id: 79361[/import]