remove self help

hey all,
newbie question here. I am trying to remove an object when it gets to the left side of the screen. my code is something like this:

hero_false = display.newImage(“hero.png”)
function move (event)
hero_false.x = hero_false.x-10
end
timer.performWithDelay(100, move, 0)
function remove_hero_false (event)
if hero_false.x <= 45 then
hero_false:removeSelf()
hero_false = nil
end
return true
end
Runtime:addEventListener(“enterFrame”, remove_hero_false )

I get an error when do this. How do I correctyl set the object to nil? Thanks in advance.
SM [import]uid: 79586 topic_id: 28746 reply_id: 328746[/import]

@middleton.s.a ,

Try that below please:

[lua]hero_false = display.newImage(“hero.png”)
hero_false:setReferencePoint(display.CenterReferencePoint)
hero_false.x = display.contentWidth * 0.5
hero_false.y = display.contentHeight * 0.5
function move(event)
hero_false.x = hero_false.x-10
end

function remove_hero_false(event)
if hero_false ~= nil then
if hero_false.x <= 45 then
Runtime:removeEventListener(“enterFrame”, remove_hero_false)
print(Runtime.remove_hero_false) – OUTPUT: print “nil” as there isn`t anymore events attached to the Runtime.

timer.cancel(myTimer)

hero_false:removeSelf()
hero_false = nil
print(hero_false,"= value of the variable hero_false") – OUTPUT: print “nil” as the variable was removed.
end
end
return true
end

myTimer = timer.performWithDelay(100, move, 0)

Runtime:addEventListener(“enterFrame”, remove_hero_false )[/lua]

As Ive commented into the code, youre going to see the real value of the objects via the [lua]print()[/lua] statements to be sure that it is working as expected.
Let me know how it goes,

Cheers,
Rodrigo. [import]uid: 89165 topic_id: 28746 reply_id: 115856[/import]

You are removing the hero like you should and you are niling out just like you should, the problem i see is you are not canceling the timer. So you remove the hero and the reference to it but the timer is still trying to find it. Also in most cases you would want to localize everything that you can. Here is an example that should work.

[lua]–Your hero
local hero_false = display.newImage (“hero.png”)
hero_false.x = 310; hero_false.y = 200

–Forward Reference your timer so you can delete it even though its was created after the function
local heroTimer

–You hero move function
local function moveHero ()
hero_false.x = hero_false.x - 10

if hero_false <= 45 then
timer.cancel (heroTimer); heroTimer = nil
hero_false:removeSelf(); hero_false = nil
return true
end
end

heroTimer = timer.performWithDelay ( 100, moveHero, -1)[/lua]

I did not test this but it should work fine, i hope this helps! If you have any questions let me know! [import]uid: 126161 topic_id: 28746 reply_id: 115857[/import]

FYI, the title to this thread can be taken way out of context. [import]uid: 147305 topic_id: 28746 reply_id: 115861[/import]

@budershank haha [import]uid: 126161 topic_id: 28746 reply_id: 115863[/import]

thats true about the title. I didn’t see that after working so long on my game. Thanks for the help everyone. I didn’t realize the timer was still going. that helps a ton and I understand things a lot better now thanks to all of your posts. Thanks tons!
SM [import]uid: 79586 topic_id: 28746 reply_id: 115906[/import]

Glad we could help! [import]uid: 126161 topic_id: 28746 reply_id: 115955[/import]

hey guys,
that worked great. now I am trying to remove an object from a table and I get the same error. Here is my code.
function print_it (event)
for i = 1, #spawnEnemy do
if spawnEnemy[i] ~= nil then
if spawnEnemy[i].x <= -51 then
– Runtime:removeEventListener(“enterFrame”, print_it)
print(spawnEnemy[i].myName)
table.remove (spawnEnemy, i)
spawnEnemy[i]:removeSelf()
spawnEnemy[i] = nil
end
end
end
return true
end
Runtime:addEventListener(“enterFrame”, print_it)

Am I doing something wrong here to? Thanks in advance.
SM [import]uid: 79586 topic_id: 28746 reply_id: 116254[/import]

  1. you remove the spawnEnemy from the table, THEN you refer to it. Not good. Do:

spawnEnemy[i]:removeSelf()
spawnEnemy[i] = nil
table.remove (spawnEnemy, i)

  1. you use table.remove for i-th member of the table, but your for loop is from 1 to table count - so every time you do the table.remove, you’re skipping the next member. Do the look from table count down to 1 instead. [import]uid: 160496 topic_id: 28746 reply_id: 116260[/import]

Hi Mike,
Thanks for the suggestion. How do I do a look from table count down? The other stuff you said made sense. Thanks.
SM [import]uid: 79586 topic_id: 28746 reply_id: 116261[/import]

for i = #spawnEnemy,1,-1 do [import]uid: 160496 topic_id: 28746 reply_id: 116262[/import]

thats what thought, still seem to be having trouble. But I will keep at it. Thanks again. [import]uid: 79586 topic_id: 28746 reply_id: 116264[/import]