We need to set up the panda collision event and animate it accordingly, using the image sheet:
createPanda()
:local createPanda = function()
onPandaCollision()
with the parameters self
and event
. Reload panda
every time a collision occurs with the stars or the edge of the screen, by using callNewRound()
:local onPandaCollision = function( self, event ) if event.phase == "began" then if panda.isHit == false then panda.isHit = true if event.other.myName == "star" then callNewRound( true, "yes" ) else callNewRound( true, "no" ) end if event.other.myName == "wall" then callNewRound( true, "yes" ) else callNewRound( true, "no" ) end elseif panda.isHit then return true end end end
gameGroup
group:arrow = display.newImage( "arrow.png" ) arrow.x = 240; arrow.y = 225 arrow.isVisible = false gameGroup:insert( arrow )
"set"
, "crouch"
, and "air"
:local sheetData = { width=128, height=128, numFrames=5, sheetContentWidth=384, sheetContentHeight=256 } local sheet = graphics.newImageSheet( "pandaSprite.png", sheetData ) local sequenceData = { { name="set", start=1, count=2, time=200 }, { name="crouch", start=3, count= 1, time=1 }, { name="air", start=4, count=2, time=100 } } panda = display.newSprite( sheet, sequenceData ) panda:setSequence("set") panda:play()
panda
before it launches into the air:panda.x = 240; panda.y = 225 panda.isVisible = false panda.isReady = false panda.inAir = false panda.isHit = false panda.isBullet = true panda.trailNum = 0 panda.radius = 12 physics.addBody( panda, "static", { density=1.0, bounce=0.4, friction=0.15, radius=panda.radius } ) panda.rotation = 0
panda
using "collision"
and apply an event listener:panda.collision = onPandaCollision panda:addEventListener( "collision", panda )
poof
object:poof = display.newImage( "poof.png" ) poof.alpha = 1.0 poof.isVisible = false
panda
and poof
objects into the gameGroup
group. Close the function:gameGroup:insert( panda ) gameGroup:insert( poof ) end
activateRound()
function and add the "set"
animation sequence for the panda:panda:setSequence("set") panda:play()
The collision events that occur for the panda start with if event.phase == "began"
. The panda reloads on screen through several cases of if
statements. event.other.myName == "star"
will call a new round when the panda launches off screen towards the right, left, or top sides of the stage.
The image sheet for the panda has three sets of animations. They are called "set"
, "air"
, and "crouch"
. There are a total of five frames in the image sheet.
The physical properties of the panda are set before launch. The body type is set to "static"
and will change when it's in the air.
The collision event for the panda is called by panda:addEventListener( "collision", panda )
.
Now that the image sheet has been set up, the "set"
animation needs to be added in the activateRound()
function to initiate movement.