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) 
[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]
[import]uid: 52491 topic_id: 13162 reply_id: 48323[/import]
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.