Animation Binding to button function HELP!?!

hey im new to lua corona but i have used lua before.
 
My issue is simple but i have no idea how to get my function for buttons to oporate my animaton function.
 
 
revised and working but still having animation problem

local roundOutTickLimit = 15 -- 5min local roundOutTick = 0 local gameState = "game" local physics = require( "physics" ) physics.start() physics.setGravity( 0, 9.8 ) physics.setScale( 30 ) physics.setDrawMode( "normal" ) local background = display.newImage("BG.png") local ROI = display.newImage("UI/ROI.png") ROI.isVisible = false -- spawning locations (all maps have same locations) local spawnLocationFlagA = { Ax = 200, Bx = 240, Cx = 280 } local spawnLocationFlagB = { Ax = 1200, Bx = 1240, Cx = 1280 } local spawnLocationFlagC = { Ax = 2200, Bx = 2240, Cx = 2280 } local FlagAInfo = {x = 100, y = 300, mode = "Blue"} local FlagBInfo = {x = 1000, y = 300, mode = "Neutral"} local FlagBInfo = {x = 2000, y = 300, mode = "Red"} local clockSheetData = {width = 36,height = 35, numFrames = 5,sheetContentWidth= 180, sheetContentHeight= 35 }  local clockSheet = graphics.newImageSheet( "UI/SPCS.png", clockSheetData )  local clockData = { name = "running", start=1, count=5, time= 4000 } local clockAnimation = display.newSprite( clockSheet, clockData ) clockAnimation.x = 250--center the sprite horizontally clockAnimation.y = 30 clockAnimation:setSequence("running") clockAnimation.isvisible = false local playerSheetData = {width = 43,height = 36, numFrames = 10,sheetContentWidth= 430, sheetContentHeight= 36 }  local playerSheet = graphics.newImageSheet( "Player Data/ps.png", playerSheetData )  local sequenceData = { { name = "RunR", start=1, count=4, time= 250 }, { name = "CrouchR", start = 5,count = 1, time = 1 }, { name = "StandR", start = 1,count = 1, time = 1 }, { name = "RunL", frames = {10,9,8,7}, time= 250 }, { name = "CrouchL", start = 6,count = 1, time = 1 }, { name = "StandL", start = 10,count = 1, time = 1 } } local animation = display.newSprite( playerSheet, sequenceData ) animation.x = spawnLocationFlagA.Bx --center the sprite horizontally animation.y = display.contentHeight - animation.height animation.isVisible = false -- grouping guns = display.newGroup() objects = display.newGroup() -- player Info/ Ai info (contains some usefull information on game states and teams) local playerInfo = {currentWep = 2, score = 0, flags = 0, mode = "spawning", info = "ground", team = "Blue",direction = "r", ammo = 30, clip = 2, perk = "Mech", SPTimer = 0, dethCount = 0, health = 400, pressingR = "false", pressingL = "false", pressingD = "false"} local AIAInfo = {currentWep = 2, score = 0, flags = 0, mode = "game", info = "ground", team = "Blue", direction = "r", ammo = 30, clip = 2, perk = "Mech",  SPTimer = 0, dethCount = 0, health = 400} local AIBInfo = {currentWep = 2, score = 0, flags = 0, mode = "game", info = "ground", team = "Blue", direction = "r",ammo = 30, clip = 2, perk = "Mech",  SPTimer = 0, dethCount = 0, health = 400 } local AIEAInfo = {currentWep = 2, score = 0, flags = 0, mode = "game", info = "ground", team = "Red", direction = "l", ammo = 30, clip = 2, perk = "Mech",  SPTimer = 0, dethCount = 0, health = 400} local AIEBInfo = {currentWep = 2, score = 0, flags = 0, mode = "game", info = "ground", team = "Red", direction = "l", ammo = 30, clip = 2, perk = "Mech",  SPTimer = 0, dethCount = 0, health = 400} local AIECInfo = {currentWep = 2, score = 0, flags = 0, mode = "game", info = "ground", team = "Red", direction = "l", ammo = 30, clip = 2, perk = "Mech",  SPTimer = 0, dethCount = 0, health = 400} -- primary weps local GunSheetData = {width = 36,height = 12, numFrames = 6,sheetContentWidth= 216, sheetContentHeight= 12 }  local GunSheet = graphics.newImageSheet( "Guns/gunsheet.png", GunSheetData )  local sequenceBData = { { name = "AKR", start=1, count=1, time= 1 }, { name = "AKL", start = 2,count = 1, time = 1 }, { name = "MPR", start = 3,count = 1, time = 1 }, { name = "MPL", start = 4,count = 1, time= 1 }, { name = "M4R", start = 5,count = 1, time = 1 }, { name = "M4L", start = 6,count = 1, time = 1 } } gunorintation = 0 local gunAnimation = display.newSprite( GunSheet, sequenceBData ) gunAnimation.x = display.contentWidth/2 --center the sprite horizontally gunAnimation.y = display.contentHeight - 30 guns:insert( gunAnimation ) physics.addBody( gunAnimation, "kinematic", { friction=0.0, bounce=0.0 } ) gunAnimation.rotation = gunorintation gunAnimation.isVisible = false local buttonR = display.newImage( "UI/buttonR.png" ) buttonR.x = 90 buttonR.y = 60 local buttonL = display.newImage( "UI/buttonL.png" ) buttonL.x = 30 buttonL.y = 60 local buttonUP = display.newImage( "UI/buttonUP.png" ) buttonUP.x = 60 buttonUP.y = 30 local buttonDN = display.newImage( "UI/buttonDN.png" ) buttonDN.x = 60 buttonDN.y = 90 local buttonSQ = display.newImage( "UI/bar.png" ) buttonSQ.x = 420 buttonSQ.y = 60 local scrollbar = display.newImage("UI/scroll.png") scrollbar.x = 420 scrollbar.y = 60 ------------------------------------- --- Animation button binding ------------------------------------- function buttonR:touch( event ) if gameState == "game" then if playerInfo.mode == "game" then     if event.phase == "began" then     display.getCurrentStage():setFocus(event.target)          playerInfo.pressingR = "true" playerInfo.direction = "r"  elseif ( event.phase == "ended" or event.phase == "cancelled" ) then  display.getCurrentStage():setFocus( nil )  --setting focus to 'nil' removes the focus   playerInfo.pressingR = "false"     end return true end end end buttonR:addEventListener( "touch", buttonR ) function buttonL:touch( event ) if gameState == "game" then if playerInfo.mode == "game" then     if event.phase == "began" then     display.getCurrentStage():setFocus(event.target)      playerInfo.pressingL = "true" playerInfo.direction = "l"  elseif ( event.phase == "ended" or event.phase == "cancelled" ) then         display.getCurrentStage():setFocus( nil )  --setting focus to 'nil' removes the focus                  playerInfo.pressingL = "false"          end return true end end end buttonL:addEventListener( "touch", buttonL ) function buttonDN:touch( event ) if gameState == "game" then     if event.phase == "began" then      playerInfo.pressingD = "true"  elseif ( event.phase == "ended" or event.phase == "cancelled" ) then         display.getCurrentStage():setFocus( nil )  --setting focus to 'nil' removes the focus          playerInfo.pressingD = "false"     end return true end end buttonDN:addEventListener( "touch", buttonDN ) local function movementBinding() if playerInfo.pressingL == "true" then if playerInfo.direction == "l" then print("run left")      animation:setSequence("RunL")     animation:play()     end     end      if playerInfo.pressingR == "true" then if playerInfo.direction == "r" then print("run right") animation:setSequence("RunR")     animation:play() end end if playerInfo.pressingR == "false" then if playerInfo.pressingL == "false" then if playerInfo.pressingD == "false" then if playerInfo.direction == "r" then print("stand Right") animation:setSequence("StandR")     animation:play() end end end end if playerInfo.pressingR == "false" then if playerInfo.pressingL == "false" then if playerInfo.pressingD == "false" then if playerInfo.direction == "l" then print("stand Left") animation:setSequence("StandL")     animation:play() end end end end if playerInfo.pressingD == "true" then if playerInfo.direction == "r" then animation:setSequence("CrouchR")     animation:play() end end if playerInfo.pressingD == "true" then if playerInfo.direction == "l" then animation:setSequence("CrouchL")     animation:play() end end if playerInfo.direction == "r" then if playerInfo.currentWep == 1 then     gunAnimation:setSequence("AKR")         gunAnimation:play() end if playerInfo.currentWep == 2 then     gunAnimation:setSequence("MPR")         gunAnimation:play() end if playerInfo.currentWep == 3 then     gunAnimation:setSequence("M4R")         gunAnimation:play() end end if playerInfo.direction == "l" then if playerInfo.currentWep == 1 then     gunAnimation:setSequence("AKL")         gunAnimation:play() end if playerInfo.currentWep == 2 then     gunAnimation:setSequence("MPL")         gunAnimation:play() end if playerInfo.currentWep == 3 then     gunAnimation:setSequence("M4L")         gunAnimation:play() end end end Runtime:addEventListener("enterFrame",movementBinding) -- player functions local function playerSpawning() if playerInfo.mode == "dead" then animation.isVisible = false playerInfo.deathCount = playerInfo.deathCount +1 playerInfo.mode = "spawning" end if playerInfo.mode == "spawning" then playerInfo.SPTimer = playerInfo.SPTimer +1 animation.isVisible = false gunAnimation.isVisible = false clockAnimation.isvisible = true clockAnimation:play() end if playerInfo.SPTimer \>= 5 then playerInfo.mode = "game" playerInfo.SPTimer = 0 print("player spawned") -- move world to position clockAnimation.isvisible = false end if playerInfo.mode == "game" then clockAnimation.isVisible = false display.remove( clockAnimation ) gunAnimation.isVisible = true animation.isVisible = true end end timer.performWithDelay(1000,playerSpawning,playerInfo.SPTimer) local function roundOut() if gameState == "game" then roundOutTick = roundOutTick + 1 if (roundOutTick \>= roundOutTickLimit) then print("Round Out") gameState = "round out" end end if gameState == "round out" then ROI.isVisible = true end end timer.performWithDelay(1000,roundOut,roundOutTick)

new issue: problem with running the animation because of the run time. when i press Right trigger player just stand and when i press L trigger the animation is still in stand left. it should jump into run animation. but i think there is an issue with Runtime

is it how im handling the function ?

It’s really hard to read your code the way you posted it in.  If you use and tags (taking out the space after the [, then the code formats much better.

Are you getting any errors?  Have  you tried putting some prints in your touch function to see what values you are getting there?

If you do not know how to get to your console log to see the errors, please read this blog post:

http://www.coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

I want the function to run all the time I think it’s not running all the time. Just once when I start the game. How to I get the function to keep repeating.

Also I’m not getting errors and I added print functions to the button listeners and the variables are changing to the action of pressing the button.

I also added a print function to the start of the function

Function movement binding ()
Print (“movement animation function”)
So on and so forth…

And it only prints it one time.

So I’m guessing it’s an issue of how I’m handling this function. It’s deferent from lua player.

Corona uses an event driven looping system called “enterFrame”.  Basically every frame (30 times per second or 60 depending on your app’s settings), an enterFrame event gets generated and you can do work.  That’s one way to loop:

local function doSomethingAllTheTime()

    – do your work, remember going to happen 30 times or 60 times per second

end

Runtime:addEventListener(“enterFrame”, doSomethingAllTheTime)

Another way is to do a looping timer:

timer.performWithDelay(5000, doSomethingAllTheTime, 0)

This will cause doSomeThingAllTheTime() to run every 5 seconds (change to be the duration of your animation) and the 0 says loop forever.

Depending on your animation, you can set loop parameters and can capture onComplete events to chain animations together and have them re-call each other when they are finished.  I’m not a big spite user, so I can’t produce the code off of the top of my head for that.

So I tried adding runtime:addeventlistener(“enteFrame”, movementBinding) and I get the error assertion failed

Assertion failed !
[c]: ?
[c]: in function 'assert
In function ‘getOrCreateTable’
In function addeventlistener

Is the Runtime capitalized? Did you put the Runtime call before or after your function definition for movementBinding?

oh i fixed it, it wasn’t capitalized or something. now i have a new problem

for some reason when i press right and when i press left my character changes to stand L and R but it doesn’t play my run animations. only animations with one frame play. is it because of the constant calling of the function ?

also you where talking about loop parameters for linking animations. how would you set that up with StandR, standL, crouchL crouchR, runR and runL.run R and Run L are 4 frames at 250 milliseconds for each frame in the set. im using the simple way of creating sprites but i cant seem to get the animations to run.

I fixed the animation by adding an update function like so

function UpdatePlayer(event)
movementBinding()

end

timer.performWithDelay(250,UpdatePlayer,-1)

runs perfectly thank you for your help, i greatly appreciate it  :slight_smile:

is it how im handling the function ?

It’s really hard to read your code the way you posted it in.  If you use and tags (taking out the space after the [, then the code formats much better.

Are you getting any errors?  Have  you tried putting some prints in your touch function to see what values you are getting there?

If you do not know how to get to your console log to see the errors, please read this blog post:

http://www.coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

I want the function to run all the time I think it’s not running all the time. Just once when I start the game. How to I get the function to keep repeating.

Also I’m not getting errors and I added print functions to the button listeners and the variables are changing to the action of pressing the button.

I also added a print function to the start of the function

Function movement binding ()
Print (“movement animation function”)
So on and so forth…

And it only prints it one time.

So I’m guessing it’s an issue of how I’m handling this function. It’s deferent from lua player.

Corona uses an event driven looping system called “enterFrame”.  Basically every frame (30 times per second or 60 depending on your app’s settings), an enterFrame event gets generated and you can do work.  That’s one way to loop:

local function doSomethingAllTheTime()

    – do your work, remember going to happen 30 times or 60 times per second

end

Runtime:addEventListener(“enterFrame”, doSomethingAllTheTime)

Another way is to do a looping timer:

timer.performWithDelay(5000, doSomethingAllTheTime, 0)

This will cause doSomeThingAllTheTime() to run every 5 seconds (change to be the duration of your animation) and the 0 says loop forever.

Depending on your animation, you can set loop parameters and can capture onComplete events to chain animations together and have them re-call each other when they are finished.  I’m not a big spite user, so I can’t produce the code off of the top of my head for that.

So I tried adding runtime:addeventlistener(“enteFrame”, movementBinding) and I get the error assertion failed

Assertion failed !
[c]: ?
[c]: in function 'assert
In function ‘getOrCreateTable’
In function addeventlistener

Is the Runtime capitalized? Did you put the Runtime call before or after your function definition for movementBinding?

oh i fixed it, it wasn’t capitalized or something. now i have a new problem

for some reason when i press right and when i press left my character changes to stand L and R but it doesn’t play my run animations. only animations with one frame play. is it because of the constant calling of the function ?

also you where talking about loop parameters for linking animations. how would you set that up with StandR, standL, crouchL crouchR, runR and runL.run R and Run L are 4 frames at 250 milliseconds for each frame in the set. im using the simple way of creating sprites but i cant seem to get the animations to run.

I fixed the animation by adding an update function like so

function UpdatePlayer(event)
movementBinding()

end

timer.performWithDelay(250,UpdatePlayer,-1)

runs perfectly thank you for your help, i greatly appreciate it  :slight_smile: