Move a character using buttons

Hello,
I’m trying to develop a small rpg game and I am having some problems when launching the character animation taking steps and moving the character.

The problem comes when I press the button and keep it pressed, the character moves until I release the button, but the animation that must do, only played once.

I want that for every two frames to advance the character, the animation be shown.
The code is as follows:

local options =  
{  
  
  
 width = 44,  
 height = 44,  
 numFrames = 12  
}  
   
local buttonPressed = false  
local mySheet = graphics.newImageSheet( "imagenes/ic\_example.png", options )--Declaramos la animación y le damos una imagen  
--Animación de imagenes consecutivas:  
--Animación de imagenes no consecutivas:  
local sequenceData = {  
   
 { name = "arriba",  
 frames = { 10,11,12}, --Especificamos el orden de los indices del frame de la animación.  
 time = 500,  
 loopCount = 1  
 }, --Si queremos definar mas secuencias, ponemos una coma aqui y ponemos la siguiente secuencia en una subtabla.  
   
 { name = "abajo",  
 frames = { 1,2,3},  
 time = 500,  
 loopCount = 1  
 }  
  
}  
local animation = display.newSprite( mySheet, sequenceData )--declaramos la animación  
  
--posicionamos la animación en el centro del mapa y procedemos a su movimiento  
--Animación arriba:  
animation.x = display.contentWidth/2 --center the sprite horizontally  
animation.y = display.contentHeight/2 --center the sprite vertically  
 --animación abajo:  
---------------------------------------------| BOTON ADELANTE |-------------------------------------  
local botonalante = display.newImage("imagenes/botonalante.png")  
botonalante.x = display.contentWidth / 2 --Posiciona el boton en el centro orizontal de la pantalla  
botonalante.y = display.contentHeight -130 --Posiciona el boton a 75 pixels en vertical  
  
function moveradelante( event )  
 -- Move up  
  
 if buttonPressed then  
 animation:setSequence( "arriba" )   
 animation:play()  
 animation.y = animation.y - 2  
 end  
 buttonPressed=false -- Whithout that the character advance, but the animations won't reproduce  
end  
   
 --timer.performWithDelay(10000, moveradelante,1)  
   
--timer.performWithDelay( 1000, function()  
 -- hello\_world( "first", "second" )  
--end, 1 )  
  
function botonalante:touch(event)  
 if event.phase == "began" then  
 buttonPressed = true  
 elseif event.phase == "ended" then  
 buttonPressed = false  
 end  
 return true  
 end  
  
 botonalante:addEventListener("touch", botonalante)  
  
 --Runtime:addEventListener("enterFrame", moveCharacter)  
Runtime:addEventListener("enterFrame", moveradelante)  

if I have not explained well let me know that, my English is quite good even.

Thank you very much ^ ^
[import]uid: 156044 topic_id: 35155 reply_id: 335155[/import]

Hello @jesumayoral,
See lines #20 and #26. “loopCount” = the number of times your animation will loop (so, it’s set to 1 now). If you want to repeat the animation forever, set loopCount=0.

Here is a sprite tutorial:
http://www.coronalabs.com/blog/2012/10/02/animated-sprites-and-methods/

Also, in your button press, you should start the animation on the “began” phase, and pause the animation on the “ended” phase.

Hope this helps!
Brent Sorrentino [import]uid: 200026 topic_id: 35155 reply_id: 139750[/import]

Thanks Brent Sorrentino,

really the tutorial that you sent me I had already followed, but continued with my problem (although with your modifications works better). First the character advances and play the animation, but, if I hold down the button, the character advance, but the animation does not play more until I release the button.

I think the problem is that I enter in the function moveradelante() too fast and not enough time for the animation be played. The problem is that i do not know how to fix it.

edit:
for you can see better, this is the interesting part, as amended:

function moveradelante( event )  
 -- Move up  
  
 if buttonPressed then  
 animation.y = animation.y - 2  
 end  
 --buttonPressed=false -- Whithout that the character advance, but the animations won't reproduce  
end  
   
 --timer.performWithDelay(10000, moveradelante,1)  
   
--timer.performWithDelay( 1000, function()  
 -- hello\_world( "first", "second" )  
--end, 1 )  
  
function botonalante:touch(event)  
 if event.phase == "began" then  
 animation:setSequence( "arriba" )   
 animation:play()  
 buttonPressed = true  
 elseif event.phase == "ended" then  
 buttonPressed = false  
 animation:pause()  
 end  
 return true  
 end  
  
 botonalante:addEventListener("touch", botonalante)  
  
 --Runtime:addEventListener("enterFrame", moveCharacter)  
Runtime:addEventListener("enterFrame", moveradelante)  

Thanks one more time [import]uid: 156044 topic_id: 35155 reply_id: 139756[/import]

Hi @jesumayoral,
Your code looks fine… I see no reason why it shouldn’t work.

Can you please try your animation without the button? Just try animating it “manually” using commands (play, pause, change sequence, etc.).

Thank you,
Brent [import]uid: 200026 topic_id: 35155 reply_id: 140018[/import]

Hi Brent Sorrentino,

Yes, finally, the code works fine, the only problem was that:

function botonalante:touch(event)  
 if event.phase == "began" then  
 animation:setSequence( "arriba" )   
 animation:play()  
 buttonPressed = true  
 elseif event.phase == "ended" then  
 buttonPressed = false  
 animation:pause()  
 end  
 return true --Here.  
 end  

In the version who I execute, this “return true” didn’t exist, I removed this to do proves, but later I forgot to reinsert that return. Sorry, it was my mistake.

Thank you. [import]uid: 156044 topic_id: 35155 reply_id: 140082[/import]

Hello @jesumayoral,
See lines #20 and #26. “loopCount” = the number of times your animation will loop (so, it’s set to 1 now). If you want to repeat the animation forever, set loopCount=0.

Here is a sprite tutorial:
http://www.coronalabs.com/blog/2012/10/02/animated-sprites-and-methods/

Also, in your button press, you should start the animation on the “began” phase, and pause the animation on the “ended” phase.

Hope this helps!
Brent Sorrentino [import]uid: 200026 topic_id: 35155 reply_id: 139750[/import]

Thanks Brent Sorrentino,

really the tutorial that you sent me I had already followed, but continued with my problem (although with your modifications works better). First the character advances and play the animation, but, if I hold down the button, the character advance, but the animation does not play more until I release the button.

I think the problem is that I enter in the function moveradelante() too fast and not enough time for the animation be played. The problem is that i do not know how to fix it.

edit:
for you can see better, this is the interesting part, as amended:

function moveradelante( event )  
 -- Move up  
  
 if buttonPressed then  
 animation.y = animation.y - 2  
 end  
 --buttonPressed=false -- Whithout that the character advance, but the animations won't reproduce  
end  
   
 --timer.performWithDelay(10000, moveradelante,1)  
   
--timer.performWithDelay( 1000, function()  
 -- hello\_world( "first", "second" )  
--end, 1 )  
  
function botonalante:touch(event)  
 if event.phase == "began" then  
 animation:setSequence( "arriba" )   
 animation:play()  
 buttonPressed = true  
 elseif event.phase == "ended" then  
 buttonPressed = false  
 animation:pause()  
 end  
 return true  
 end  
  
 botonalante:addEventListener("touch", botonalante)  
  
 --Runtime:addEventListener("enterFrame", moveCharacter)  
Runtime:addEventListener("enterFrame", moveradelante)  

Thanks one more time [import]uid: 156044 topic_id: 35155 reply_id: 139756[/import]

Hi @jesumayoral,
Your code looks fine… I see no reason why it shouldn’t work.

Can you please try your animation without the button? Just try animating it “manually” using commands (play, pause, change sequence, etc.).

Thank you,
Brent [import]uid: 200026 topic_id: 35155 reply_id: 140018[/import]

Hi Brent Sorrentino,

Yes, finally, the code works fine, the only problem was that:

function botonalante:touch(event)  
 if event.phase == "began" then  
 animation:setSequence( "arriba" )   
 animation:play()  
 buttonPressed = true  
 elseif event.phase == "ended" then  
 buttonPressed = false  
 animation:pause()  
 end  
 return true --Here.  
 end  

In the version who I execute, this “return true” didn’t exist, I removed this to do proves, but later I forgot to reinsert that return. Sorry, it was my mistake.

Thank you. [import]uid: 156044 topic_id: 35155 reply_id: 140082[/import]