Hi,
I am playing around with a game and have an enemy class and a bullet class. I can correctly remove the enemy class and all its event listeners, but I can’t seem to find a way to delete the bullet.
Here is some of my code. This will remove the image from the other instance, but the event listeners from within this instance keep running - this causes an error… I tried adding similar code to the bullet code but it does not fire (not sure why? maybe because the collision
enemy.lua
[lua]
elseif event.other.name == “bullet” then
Runtime:removeEventListener( “enterFrame”, self )
self.image:removeEventListener( “collision”, self ) – collided with another physics body
scene:removeEventListener(‘hit_rightWall_event’, self)
scene:removeEventListener(‘hit_leftWall_event’, self)
self.image:removeSelf()
event.other:removeSelf()
end
[/lua]
bullet.lua
[lua]
function bullet:enterFrame()
self.bullet.y = self.bullet.y - 10
end
function bullet:init(xloc) --initializer
– Create attributes
self.bullet = display.newRect(xloc,890,4,15)
self.bullet:setReferencePoint( CenterReferencePoint )
self.bullet:setFillColor ( 0, 255, 0 )
self.bullet.name = “bullet”
physics.addBody( self.bullet, “dynamic”)
Runtime:addEventListener( “enterFrame”, self )
end
function bullet:collision(self, event)
print(“Bullet Kaboom”)
Runtime:removeEventListener( “enterFrame”, self )
self.bullet:removeSelf()
end
function bullet:start()
— Create Listeneres
self.bullet:addEventListener( “collision”, self )
end
return bullet
[/lua]
[import]uid: 184705 topic_id: 36664 reply_id: 336664[/import]