Shield Won't Disappear After Get Hit

Guys, help me again please :frowning:

so basically i am testing a game where a ship (main player) will just die when it gets hit (collide) with a bomb. The player can get a shield powerup where it will create shield around the ship and it will prevent him from death when collide with the bomb. However, if the ship/shield collide with the bomb, the shield should get destroyed/gone so the next bomb would destroy the ship again (original collision).

I am stuck because every time the shield has been activated, it simply won’t run the codes to collide with the bomb.

Here are the codes basically:

[code]-- SPRITE

#Set sprite
ship = sprite.newSpriteSheet(“shipsprite.png”, 95, 134)
shipset = sprite.newSpriteSet (ship, 1, 6)

sprite.add (shipset, “playerleft”, 4, 3, 150, 0)
sprite.add (shipset, “playerright”, 1, 3, 200, 0)

player = sprite.newSprite(shipset)
player.x = 160
player.y = 450
player.name = “player”
player:prepare(“playerleft”)
physics.addBody(player, {density = 0, friction = 0, bounce = 0})
player.bodyType = “static”

shield = display.newImage(“shield.png”)
shield.x = player.x
shield.y = player.y
shield.alpha = 0

local playerGroup = display.newGroup()

playerGroup:insert(player)
playerGroup:insert(shield)
#Sprite movement
function stop (event)
if event.phase ==“ended” then
motionx = 0
motiony = 0
player:pause()
end
end
Runtime:addEventListener(“touch”, stop )

function moveplayer (event)
if gameIsActive == true then
player.x = player.x + motionx
player.y = player.y + motiony
shield.x = player.x
shield.y = player.y
end
end
timer1 = timer.performWithDelay(1,moveplayer,0)
function touchleft (event)
if gameIsActive == true then
motionx = -speed
motiony = 0
player:prepare(“playerleft”)
player:play(“playerleft”)
end
end
left:addEventListener(“touch”, touchleft)

function touchright (event)
if gameIsActive == true then
motionx = speed
motiony = 0
player:prepare(“playerright”)
player:play(“playerright”)
end
end
right:addEventListener(“touch”, touchright)

function wrapit (event)
if player.x < 40 then
player.x = 40
elseif player.x > 300 then
player.x = 300
end
end
Runtime:addEventListener(“enterFrame”, wrapit)

#Sprite collision
function catchStar(e)
if (e.other.name == “star”) then
starCaught = starCaught + 1
score = score + 1
scoreNum.text = score
scoreNum:setReferencePoint(display.CenterLeftReferencePoint)
scoreNum.x = 54
audio.play(getStar)
e.other:removeSelf()
e.other = nil
elseif (e.other.name == “shieldpowerup”) then
shield.alpha = 0.5
e.other:removeSelf()
e.other = nil
elseif (e.other.name == “bomb” and shield.alpha == 0) then
showRestartButton()
local gameOverText = display.newText(“GAME OVER”, 85, 250, Arial, 30)
physics.pause()
paused = true
gameIsActive = false
e.other:removeSelf()
e.other = nil
elseif (e.other.name == “bomb” and shield.alpha == 0.5) then
shield.alpha = 0
end
end
player:addEventListener(‘collision’, catchStar)
[/code]

and regarding the shieldPowerup itself, here’s the code:

function shieldPowerup ( objectType, x, y ) shieldpowerup = display.newImage("tameng.png") shieldpowerup.name = "shieldpowerup" shieldpowerup.x = math.random(320) shieldpowerup.y = -100 shieldpowerup.rotation = 10 physics.addBody(shieldpowerup, { density=2.0, friction=0.5, bounce=0.3}) end

so basically i can have the bomb to collide with the player and the game will be over and i can have the shield powerup to collide with the player to activate the shield by changing the transparency. The shield then move following the player but that’s about it. If the shield has been “activated”, the bomb won’t run any codes when it hits the player, why is this?? I have changed the shield size so the ship head is ahead of the shield animation itself but still… [import]uid: 114765 topic_id: 21440 reply_id: 321440[/import]

Have you added a collision event listener? Try print to see if the code runs from where you want. [import]uid: 111017 topic_id: 21440 reply_id: 84915[/import]

hi there, i have run the debugger and added print function there so it looks like this:

elseif (e.other.name == "bomb" and shield.alpha == 0) then print ("DEAD") showRestartButton() local gameOverText = display.newText("GAME OVER", 85, 250, Arial, 30) physics.pause() paused = true gameIsActive = false e.other:removeSelf() e.other = nil elseif (e.other.name == "bomb" and shield.alpha == 0.5) then print ("BOMB HIT SHIELD") shield.alpha = 0 end

when the bomb hits the ship, yes it printed “DEAD” on the debugger but it never printed “BOMB HIT SHIELD” when the shield is activated (considering the code itself doesn’t work) [import]uid: 114765 topic_id: 21440 reply_id: 84944[/import]

and what do you mean by collision event listener? Yes i have the collision event listener but only for player and not for the shield. I assume i don’t need to add event listener for the shield because the codes work when shield.alpha = 0 but it doesn’t when shield.alpha is 0.5

also if i add event listener for the shield, where to put it? Below the event listener for player directly? [import]uid: 114765 topic_id: 21440 reply_id: 84945[/import]

nevermind guys, i found the problem already thanks to debugger.

When i changed shield.alpha to 0.5, it never really changed it to 0.5 but it changed it to 0.498 something :o

so i had to change the code elseif … and shield.alpha > 0.3

(instead of = 0.5) [import]uid: 114765 topic_id: 21440 reply_id: 84954[/import]