I have a ship, it fires 4 bullets.
The bullets hit an enemy sprite, the enemy sprite is destroyed, an explosion sprite is played and then removed.
My issue is that the bullets still remain on screen and continue to react with other enemies.
What I need help with is I need the bullets to remove themselves once the enemy is hit.
I thought it was simple but its to be; I have some code below:
local bulletGroup = display.newGroup()
local update = {}
local fireButton = display.newCircle(410,260,24)
fireButton:setFillColor (0,0,0,50)
function fireButton:touch(event)
if event.phase == "began" then
display.getCurrentStage():setFocus( self ,event.id )
local bullet = display.newImage('images/bullet.png')
bullet.x = ship.x -10
bullet.y = ship.y -10
bullet.name = 'bullet'
physics.addBody(bullet)
bulletGroup:insert (bullet)
local bullet2 = display.newImage('images/bullet.png')
bullet2.x = ship.x -20
bullet2.y = ship.y -20
bullet2.name = 'bullet'
physics.addBody(bullet2)
bulletGroup:insert(bullet2)
local bullet3 = display.newImage('images/bullet.png')
bullet3.x = ship.x +20
bullet3.y = ship.y -20
bullet3.name = 'bullet'
physics.addBody(bullet3)
bulletGroup:insert(bullet3)
local bullet4 = display.newImage('images/bullet.png')
bullet4.x = ship.x +10
bullet4.y = ship.y -12
bullet4.name = 'bullet'
physics.addBody(bullet4)
bulletGroup:insert(bullet4)
bulletGroup:addEventListener("collision", bulletGroup)
elseif event.phase == "cancelled" or event.phase == "ended" then
display.getCurrentStage():setFocus( self ,nil )
end
end
fireButton:addEventListener('touch',fireButton)
function update(event)
for i = bulletGroup.numChildren,1,-1 do
local weapon = bulletGroup[i]
if weapon ~= nil and weapon.y ~= nil then
weapon:translate(0, -10);
end
end
end
Runtime:addEventListener('enterFrame', update)
I wrote a simple collision function that would hopefully remove the group on collision but it just didnt work, infact I spent a fair while messing around with it.
I probably shouldnt be putting the bullets in the fire button function either.
I am also using a modular approach to this project.
Any help would be great.
[import]uid: 127675 topic_id: 32321 reply_id: 332321[/import]
