[Resolved] Removing objects problem with enterFrame

Hi,

I’m trying to make sprites called “enemy” move and when I touch them I want it to disappear from screen and free the memory by removing the object. The problem is, after I remove the object, the “enterFrame” event method is giving a nil error where it says, “enemy.x is nil” or “enemy.width is nil”.

I tried a nil check control but it didn’t work out. I’m pasting my code below. Any comments on what I’m doing wrong would be much appreciated.
[lua]module ( …, package.seeall )

local pngLib = require ( “scripts.pngLib” )

function new()
enemies = { }
end

local function createEnemy()
local numOfFrames = 6

math.randomseed ( os.time( ) )
local randomEnemy = math.random ( 1 , 9 )
local tempStr = “/images/enemies/”…randomEnemy…".png" randomEnemy = nil
local enemySpriteSheet = sprite.newSpriteSheet( tempStr, pngLib.getPngInfo( tempStr ).width / numOfFrames, pngLib.getPngInfo( tempStr ).height ) tempStr = nil
local enemyMoveSet = sprite.newSpriteSet( enemySpriteSheet, 1, numOfFrames )
sprite.add(enemyMoveSet, “walk”, 1, 4, 1000, 0)

local enemy = sprite.newSprite( enemyMoveSet )
enemy:setReferencePoint( display.CenterReferencePoint )

enemy.minSpeed = 1
enemy.maxSpeed = 5

local randomDirection = math.random ( 1 , 4 )
if randomDirection == 1 then
enemy.direction = “FROM_LEFT”
elseif randomDirection == 2 then
enemy.direction = “FROM_RIGHT”
elseif randomDirection == 3 then
enemy.direction = “FROM_AIR_LEFT”
elseif randomDirection == 4 then
enemy.direction = “FROM_AIR_RIGHT”
end

randomDirection = nil

enemy.speed = math.random ( enemy.minSpeed, enemy.maxSpeed )

if enemy.direction == “FROM_LEFT” then
enemy.x = -enemy.width
enemy.y = display.contentHeight - enemy.height / 2
elseif enemy.direction == “FROM_RIGHT” then
enemy.x = display.contentWidth + enemy.width
enemy.y = display.contentHeight - enemy.height / 2
enemy.speed = -enemy.speed
elseif enemy.direction == “FROM_AIR_LEFT” then
enemy.x = display.contentCenterX - enemy.width
enemy.y = 0
elseif enemy.direction == “FROM_AIR_RIGHT” then
enemy.x = display.contentCenterX + enemy.width
enemy.y = 0
end

enemy:prepare( “walk” )
enemy:play()

table.insert(enemies, enemy)
gameScreen:insert(enemy)

function enemy:touch ( event )
if event.phase == “ended” then
killEnemy( self )
end
return true
end

function enemy:enterFrame ( event )
if (enemy.direction == “FROM_LEFT”) then
if (enemy.x <= display.contentCenterX - enemy.width) then
enemy.x = enemy.x + enemy.speed
end
elseif (enemy.direction == “FROM_RIGHT”) then
if (enemy.x >= display.contentCenterX + enemy.width) then
enemy.x = enemy.x + enemy.speed
end
elseif (enemy.direction == “FROM_AIR_LEFT”) then
if (enemy.y <= display.contentHeight - enemy.height / 2) then
enemy.y = enemy.y + enemy.speed
end
elseif (enemy.direction == “FROM_AIR_RIGHT”) then
if (enemy.y <= display.contentHeight - enemy.height / 2) then
enemy.y = enemy.y + enemy.speed
end
end
end

enemy:addEventListener( “touch”, enemy )
Runtime:addEventListener( “enterFrame”, enemy )
end

function killEnemy( obj )
table.remove( enemies, table.indexOf( enemies, obj ) )
obj:removeEventListener(“touch”, obj)
Runtime:removeEventListener( “enterFrame”, moveEnemy )
obj:removeSelf( )
end

local timer = timer.performWithDelay( 1000, createEnemy, 0 )[/lua] [import]uid: 154911 topic_id: 31114 reply_id: 331114[/import]

You’re not removing the same listener you registered. You register the enemy object itself as a listener on Runtime, but you unregister a global called moveEnemy, which is probably nil. So the dead enemy continues receiving enterFrame events. [import]uid: 94737 topic_id: 31114 reply_id: 124411[/import]

Oh! Thank you for your answer, I corrected it. It sure was a dumb mistake. [import]uid: 154911 topic_id: 31114 reply_id: 124413[/import]

You’re not removing the same listener you registered. You register the enemy object itself as a listener on Runtime, but you unregister a global called moveEnemy, which is probably nil. So the dead enemy continues receiving enterFrame events. [import]uid: 94737 topic_id: 31114 reply_id: 124411[/import]

Oh! Thank you for your answer, I corrected it. It sure was a dumb mistake. [import]uid: 154911 topic_id: 31114 reply_id: 124413[/import]