Trying to play an animation during a collision

In my game I have my physics objects set to sensor and I want to be able to play an attack animation when the player is in collision with an enemy and has pressed the attack button. I take it that I will have to remove the objects from the stage, put in the attack animation. I’m having some trouble with this. Can anyone help me? Here is my code thus far. All the important bits anyway (I think) :wink:

[lua]function setUpHUD()
–Player’s Health
playerHealth = display.newText( “100”,0, 0, “Helvetica-Bold”, scoreTextSize )
playerHealth.x = 30
playerHealth.y = 30
playerHealth:setTextColor(255, 0, 0)
hudGroup:insert( playerHealth )

–Enemy Ships Health
dudesHealth = display.newText( “100”,0, 0, “Helvetica-Bold”, scoreTextSize )
dudesHealth.x = 455
dudesHealth.y = 30
dudesHealth:setTextColor(255, 255, 0)
hudGroup:insert( dudesHealth )
–Attack Button

attackButton = ui.newButton{
default = “AttackButton.png”,
over = “Attack-Button-Pressed.png”,
onEvent = buttonHandler,
id = “attack”
}

attackButton.x = 420; attackButton.y = 250;

end

function drawPlayer()
– Load Player
player = movieclip.newAnim{
“SHARK_1.png”,
“SHARK_2.png”
}
player.x = 300
player.y = 1700
player:stopAtFrame( 2 )
player.myName = “SHARK”
–Load initial camera shot
camera:insert(player)
camera.x = -player.x
camera.y = -player.y + 260
physics.addBody( player, { isSensor = true } )

end
–ADD ENEMIES
function addDudes()

local theShape = {0,-35, 37,30, -37,30}
dude = display.newImageRect( “Ship-large.png” , 53, 48 )
dude:setReferencePoint( display.TopLeftReferencePoint )
physics.addBody( dude, { isSensor = true, shape = theShape } )
dude.x = 600; dude.y = 150
camera:insert(dude)
dude.myName = “enemy”
end


– Animate Player : ADD JOY ANGLE INSTEAD
function animatePlayer( joyPos )

– Pick Correct Sprite

if joyPos == false then
player:stopAtFrame( )

elseif joyPos >= 0 and joyPos <=180 then
player:stopAtFrame( 2 )

elseif joyPos >= 180 and joyPos <=360 then
player:stopAtFrame( 1 )

end

end

function reducePlayerHealth()
print(“Reduce Health”)
if playerHit == true then
playerHealth.text = tonumber(playerHealth.text) - 1
end

end

function onGlobalCollision( event )
if ( event.phase == “began” ) then

playerHit = true
– reduce score every second
collisionTimer = timer.performWithDelay(500, reducePlayerHealth,0)

elseif ( event.phase == “ended” ) then
if (playerHit) then
playerHit=false
if(collisionTimer~=nil) then – just to be safe
timer.cancel(collisionTimer)
end
end
end
end

Runtime:addEventListener( “collision”, onGlobalCollision )
function buttonHandler(event)
if event.phase == “release” and playerHit == true then
print(“ATTACK!!!”)
attack()

end
end

–THIS FUNCTION DOESN"T WORK AT ALL

function attack(event)
dude.isVisible = false
player.isVisible = false

local attackAnim = movieclip.newAnim {“sharkAttack.png, sharkAttack2.png”}
attackAnim.x = dude.x
attackAnim.y = dude.y
attackAnim:play{ startFrame=1, endFrame=10, loop=2, remove=true }
camera:insert(attackAnim)
dude.isVisible = true
player.isVisible = true

return true

end[/lua] [import]uid: 26289 topic_id: 13162 reply_id: 313162[/import]

Firstly, I’d encourage you to use sprites instead. (Unless there’s a reason not to?)

If you can do that, then here is a simple sprite tutorial which will show you how to play different frames depending on what’s being pressed or the like.

Then check out this physics tutorial - it’s part of a mini series and will show you how to make your sprite/hero listen for a collision - then play whatever sprite you want.

Peach :slight_smile: [import]uid: 52491 topic_id: 13162 reply_id: 48323[/import]

Hey Peach,

I fully intend to use sprites and thought I was? :frowning: My character/shark plays different frames depending on which way the joystick is pushed? My attack animation uses a series of frames with different animation. I was only using physics to detect collisions. I don’t intend to use any other part of the physics engine at this point.

Also my characters detect the collision. It is the part where I push the button, when I want to swap both of them out of the screen for an attack animation which decreases both of their health, until one of them loses. Either the shark or the boat. At which point another animation will be triggered depending on who won.

Basically the boat can attack the shark if they are detecting a collision, and the sharks health will go down, but the shark will only return the favour when the user presses the attack button.

I will go through your wonderful tutorials though, as they are always a pleasure :wink: Hopefully my questions will be answered.

Much Appreciated [import]uid: 26289 topic_id: 13162 reply_id: 48458[/import]

Hey CELL,

Sorry, I got a bit muddled up last night - there was so much traffic and I got a bit, yeah, muddled :wink:

SO! Yes, you’re using sprites but you’re using moveclip - which can cause people headaches. What I meant (but totally failed to convey) was that using sprites in this way instead.

If you check out how it’s done in the sprite tutorial in my previous post it could make this a LOT simpler for you.

Sorry again about the last post, it was totally ridiculous of me XD

Peach :slight_smile: [import]uid: 52491 topic_id: 13162 reply_id: 48554[/import]

Hey Peach,

No worries. I’ve switched to the sprite method that you showed in your tutorial and it does work a lot better. I would of had to have switched at some stage anyway because of the requirements of the game. I’ve got the collision working.

Cheers
[import]uid: 26289 topic_id: 13162 reply_id: 49080[/import]

Hey CELL,

That’s great news to come back to after being offline for most of the last two days :slight_smile:

Really happy it’s going more smoothly for you now.

Peach [import]uid: 52491 topic_id: 13162 reply_id: 49288[/import]

It is going smoothly! Well almost. Except… I have another little problem Peach! Any chance you could lend me your powers again? :slight_smile:

My set focus isn’t working on my attack button. It is changing to the ‘over image’ and then staying there. Disabling the button. Although I can click it but that just spawns more attack images, not what I want.

[lua]function setUpHUD()

attackButton = ui.newButton{
defaultSrc = “AttackButton.png”,
defaultX = 81,
defaultY = 81,
overSrc = “Attack-Button-Pressed.png”,
overX = 81,
overY = 81,
onEvent = buttonHandler,
id = “attackButton”,
text = “”,
font = “Helvetica”,
textColor = { 255, 255, 255, 255 },
size = “”,
emboss = false
}

attackButton.x = 420; attackButton.y = 250;

hudGroup:insert(attackButton)

end
function buttonHandler(event, self)

if event.phase == “press” and playerHit == true then

smallShipHit=True

player.isVisible = false
smallShip.isVisible = false

local reducesmallShipHealth = function() smallShipHealth.text = tonumber(smallShipHealth.text) - 5 end

smallShipCollisionTimer = timer.performWithDelay(500, reducesmallShipHealth,0)

attacksheet = sprite.newSpriteSheet(“attack.png”, 55, 169)

attackset = sprite.newSpriteSet (attacksheet, 1, 2)
sprite.add (attackset, “grab”, 1, 2, 300, 0)
sprite.add (attackset, “attack”, 2, 2, 300, 0)

newAttack = sprite.newSprite (attackset)
newAttack.x = smallShip.x
newAttack.y = smallShip.y
newAttack:prepare(“grab”)
newAttack:play(“attack”)

camera:insert(newAttack)

display.getCurrentStage():setFocus(self, attackButton)
self.isFocus = true

elseif event.phase == “release” and playerHit == true then
display.getCurrentStage():setFocus(self, nil)
self.isFocus = false

smallShipHit=false
if smallShipHit==false then
if(smallShipCollisionTimer~=nil) then – just to be safe
timer.cancel(smallShipCollisionTimer)
end
end

player.isVisible = true
smallShip.isVisible = true

newAttack:removeSelf()

end
end[/lua]
[import]uid: 26289 topic_id: 13162 reply_id: 49402[/import]