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) ;)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | 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 |
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 :)
Hey Peach,
I fully intend to use sprites and thought I was? :( 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 ;) Hopefully my questions will be answered.
Much Appreciated
Hey CELL,
Sorry, I got a bit muddled up last night - there was so much traffic and I got a bit, yeah, muddled ;)
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 :)
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
Hey CELL,
That's great news to come back to after being offline for most of the last two days :)
Really happy it's going more smoothly for you now.
Peach
It is going smoothly! Well almost. Except...... I have another little problem Peach! Any chance you could lend me your powers again? :)
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | 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 |