Hello guys I am back. I hope you will help me again this time.
This time I am having trouble with collision detection. Here is my code:
function collisionHandler(e) if(e.other.name == 'bullet' and e.target.name == 'enemy') then --it works with this line -- audio.play(explo) -- timer.cancel(enemyFire) -- enemyFire=nil -- print(enemyFire) display.remove(e.other) display.remove(e.target) e.target=nil elseif(e.other.name == 'eBlaster' and e.target.name == 'ship') then --but does not with this line -- audio.play(explo) -- timer.cancel(enemyFire) -- enemyFire=nil -- print(enemyFire) display.remove(e.other) display.remove(e.target) e.target=nil end end
Here is the code for eBlaster:
local function fire() if enemy == nil or enemy.removeSelf == nil then return true end local function shoot() local blaster = display.newImageRect( "images/ammoE.png", 20, 20 ) blaster.x = enemy.x blaster.y = enemy.y+5 blaster.name='eBlaster' physics.addBody(blaster, "kinematic"); -- blaster.bodyType='static' transition.to( blaster, { time=5000, y=(screenH+100),transition=easing.outSine} ) sceneGroup:insert(blaster) blaster:addEventListener('collision',collisionHandler)
And here is the code for the ship:
local function createShip( ) --ship creation ship = display.newImageRect ("images/ship.png",100,50) ship.x = display.contentWidth/2 ship.y = display.contentHeight-10 ship.name = 'ship' ship:addEventListener('collision', collisionHandler) physics.addBody(ship, "dynamic", {density = 1, friction = 0, bounce = 0});
What I want to happen is upon contact with enemy fire (‘eBlaster’) my ship will explode. But its not even detecting collision. I tried adding (“Collision Detected!”) to my collisionHandler to check if there is collision but it still does not work. Also I am working with composer and eBlaster is above
function scene:create( event )
while ship is within it. Can somebody please help?