Runtime Error - attempt to call method removeSelf (a nil value)

Runtime Error - attempt to call method removeSelf (a nile value) stack traceback: [C]: in function ‘removeSelf’

I’m having bomb.png (E_bomb1) in this code raining in randomn positions at a set time interval. When it reaches the bottom it removeself. So far this part of the code works really well.

It’s when I try to have it so that clicking on the E_bomb1s removes themselves that things begin to act weird.
Here is my code:

local \_H = display.contentHeight;  
local \_W = display.contentWidth;   
  
--Enemy gets touched   
  
 function touchBomb(enemy)  
 enemy.target:removeSelf();  
 end  
  
--Bomb Code Begin  
 function E\_hit(bomb)  
 bomb:removeSelf();  
 end  
  
 --Enemy Bomb1 spawn  
 --Take note that "local" makes the E\_bomb variable local to one instant of it, as opposed to without local, which makes it apply  
 --to all variables.  
 function E\_bomb1Spawn()  
 local E\_bomb1  
  
 E\_bomb1 = display.newImage("E\_bomb1.png")  
 E\_bomb1.x = math.random( \_W )  
 E\_bomb1.y = -10  
 E\_bomb1.xScale = 1  
 E\_bomb1.yScale = 1  
 --Adding the E\_bomb1.height/2 covers the other half of the height when it touches the bottom.  
 transition.to( E\_bomb1, { time=3000, y= (display.contentHeight - display.screenOriginY) + E\_bomb1.height/2 , onComplete=function() E\_hit(E\_bomb1) end })  
 E\_bomb1:addEventListener("touch", touchBomb);  
 end  
  
 E\_bomb1Spawn()  
  
 --SpawnTimer  
 E\_bomb1Spawner = timer.performWithDelay(1000, E\_bomb1Spawn, -1);  
  
--Bomb Code End  

I originally had it so that touchbomb was just a copy ‘n’ paste of E_hit, but I started receiving “Runtime Error - attempt to call method removeSelf (a nile value) stack traceback: [C]: in function ‘removeSelf’” followed by it not working.

Now with this new version after a touch, the next few E_bomb1s that are transitioning down freeze in mid-air (though they can be removed by simply clicking on them).

What is going wrong? Is the event listener apply the removeself to all the other E_bomb1s present in the screen? Any suggestions? [import]uid: 44572 topic_id: 8151 reply_id: 308151[/import]

Ugh, I have an improper title attached to this post. I tried editing it, but the site wouldn’t let me change the forum topic and thus, would not allow me to post a change to it. How can I edit this correctly? [import]uid: 44572 topic_id: 8151 reply_id: 29025[/import]

You cant, unfortunately this forum is terrible in those areas. you also cannot send private messages to other forum members.

Not sure exactly whats wrong with you code, but whenever i get errors like that its usually because i have not declared variables in the proper order. so the best thing to do is at the very top of your code just declare them simply. for example

local bomb  
local enemy  

that way the rest of your code can for sure see the variables. maybe give that a try. [import]uid: 19620 topic_id: 8151 reply_id: 29336[/import]

Which line exactly is that error showing up on? And remember that the line numbers here may be different than the line numbers in your editor, so tell us the line number here. [import]uid: 12108 topic_id: 8151 reply_id: 29348[/import]

This is ultimately why I wanted to edit the post. The error doesn’t show up anymore based on the code I posted on here.

The error used to occur in line 9, but now that I’ve assigned it to the actual target, when I click on the object, it does exactly what it’s suppose to do and disappears. The problem is, it also makes the follow two others that appear after it freeze mid-way through their transitions. I’m not sure why.

Yeah, I’m a little frustrated that I can’t edit this, as most people seem to be coming here trying to answer what’s on my title.

Should I just make a new topic with my new problem? [import]uid: 44572 topic_id: 8151 reply_id: 29359[/import]

I have different code but exactly the same problem. If you have looked at this more then I would be interested in the solution. [import]uid: 20912 topic_id: 8151 reply_id: 50833[/import]

Also adding in a quick check is also a good idea to make sure the object still has a removeSelf property, ie, its not equal to nil…

if myObject.removeSelf then myObject:removeSelf() myObject = nil end [import]uid: 6981 topic_id: 8151 reply_id: 51012[/import]

its a simple issue… you are using oncomplete to remove every object and also you are removing objects when it is touched so for the objects that are touched you are trying to remove it even if its not available. you can change your E_hit function as below to fix this
[lua]–Bomb Code Begin
function E_hit(bomb)
if bomb ~= nil then
bomb:removeSelf();
end
end[/lua] [import]uid: 71210 topic_id: 8151 reply_id: 50840[/import]

and even this works
[lua]function E_hit(bomb)
if bomb then
bomb:removeSelf();
end
end[/lua] [import]uid: 71210 topic_id: 8151 reply_id: 51019[/import]