I am trying to create a space shooter game but I am having trouble with obj ~= nill. Can anyone please help this newbie?
here is my code:
local function wave1( )
local sceneGroup = scene.view
local function e1( )
--create enemy1
local enemy = display.newImageRect(“images/enemy.png”,50,25)
enemy.x=-(screenW+enemy.contentWidth)
enemy.y=0
enemy.name=“enemy”
physics.addBody(enemy)
sceneGroup:insert(enemy)
enemy:addEventListener(‘collision’, collisionHandler)
local function removeEnemy( self, event )
if (enemy ~=nil) then
if (enemy.x > screenW ) then
Runtime:removeEventListener( “enterFrame”, self )
enemy:removeSelf()
enemy=nil
end
end
end
if (enemy ~= nil) then
enemy.enterFrame = removeEnemy
Runtime:addEventListener( “enterFrame”, enemy )
end
local function fire()
local function shoot()
local blaster = display.newImageRect( “images/ammoE.png”, 20, 20 )
blaster.x = enemy.x
blaster.y = enemy.y+5
transition.to( blaster, { time=1000, y=(blaster.y+screenH),} )
sceneGroup:insert(blaster)
--remove fire start
local function removeBullet( self, event )
if (blaster.y > 200) or (isGameOver==true)then
Runtime:removeEventListener( “enterFrame”, self )
display.remove( self )
end
end
blaster.enterFrame = removeBullet
Runtime:addEventListener( “enterFrame”, blaster )–remove fire end
end
timer.performWithDelay(50,shoot,3)
end
if (enemy ~=nil)then
timer.performWithDelay(3000,fire,1)
end
transition.to( enemy, { time=4500, x=display.contentWidth+enemy.contentWidth,y=100} )–control enemy speed
end
timer.performWithDelay(300,e1,6)
end
function collisionHandler(e)
if(e.other.name == ‘bullet’ and e.target.name == ‘enemy’) then
– audio.play(explo)
display.remove(e.other)
display.remove(e.target)
e.target=nil
end
end
I’m sorry if its kinda messy…
function collisionHandler works but it gives function removeEnemy an error even though removeEnemy should only work if enemy is not nil.