Upon button press I want character to start walk animation
_ - walk animation is playing no issue with the code below _
- I want player to move forward so player.x = player.x + some value when button is pressed (this is not happening!)
However when pressing button the character is not moving forward with animation…how to fix this?
Please note I tried to add enterFrame, I had this confusion before but still havent wrapped my head around the implementation. Could anyone please help me how to fix this issue?
motionx = 1;
function movecharacter (event)
myAnimation.x = myAnimation.x + motionx ;
end
Runtime:addEventListener(“enterFrame”, movecharacter)
------------------------------------------------------------------------------------------ -- Implementing player animation ------------------------------------------------------------------------------------------ sheetData2 = { width=64, height=64, numFrames=20, sheetContentWidth=256, sheetContentHeight=320 } sheet2 = graphics.newImageSheet( "Assets/Sprites/player64.png", sheetData2 ) sequenceData2 = { { name="rightrun", sheet=sheet2, start=16, count= 4, time=1400, loopCount=0 }, } \_G.myAnimation = display.newSprite( sheet2, sequenceData2 ) myAnimation.x = 150; myAnimation.y = 450; -- myAnimation:play() ------------------------------------------------------------------------------------------ -- Button Touch and player animation reaction ------------------------------------------------------------------------------------------ local function playerAnimation( event ) if ( event.phase == "began" ) then -- Code executed when the button is touched print( "object touched = " .. tostring(event.target) ) -- "event.target" is the touched object myAnimation:play() -- Variable used to move character along x axis motionx = 1; function movecharacter (event) myAnimation.x = myAnimation.x + motionx ; end Runtime:addEventListener("enterFrame", movecharacter) elseif ( event.phase == "moved" ) then -- Code executed when the touch is moved over the object print( "touch location in content coordinates = " .. event.x .. "," .. event.y ) elseif ( event.phase == "ended" ) then -- Code executed when the touch lifts off the object print( "touch ended on object " .. tostring(event.target) ) myAnimation:pause() end return true -- Prevents tap/touch propagation to underlying objects end local myButton = display.newImage("Assets/button/moverightbutton.png") myButton.width = 50; myButton.height=50; myButton.x=100 ; myButton.y = 700 myButton:addEventListener( "touch", playerAnimation ) -- Add a "touch" listener to the object
I wish everyone Merry Christmas!