Object stops motion after removeSelf()

I’ve seemed to run into a problem, I have multiple enemies being spawned with the perform with delay call. But when i kill one of the enemies they all stop moving! Which is not what i’m trying to achieve! Any help would be appreciated

The Code –

[code]
local function dino()
local health = 100;
local dinos = display.newCircle(0,0,10);
dinos.x = -47;
dinos.y = waypointsY[rnd(1, #waypointsY)];
local healthBar = display.newRect(0,0,100,5);
healthBar.x = dinos.x
healthBar.y = dinos.y - 30
healthBar:setFillColor(255,0,0);
healthBar.isVisible = false;
local function tapGone(e)
if(e.phase == “began”) then
healthBar.isVisible = true;
health = health - 20;
healthBar.xScale = health / 90;
end
if(e.phase == “ended”)then
healthBar.isVisible = false;
end
if(health == 0) then
healthBar:removeSelf();
dinos:removeSelf();
end
end – end TapGone
local function move(e)

dinos.x = dinos.x + 2;
healthBar.x = healthBar.x + 2;

end – end Move
– Listeners
dinos:addEventListener(“touch”, tapGone);
Runtime:addEventListener(“enterFrame”, move);
– End Listeners

end – end function

timer.performWithDelay(300, dino, 23);
[/code] [import]uid: 56659 topic_id: 12611 reply_id: 312611[/import]

as soon as you kill one -

:slight_smile:

ok am not in front of my computer but i would

instead of calling dinos; inside of the event call

do something like

local obj = event.object  
obj:removeEventListerner();  
obj:removeSelf()  

c [import]uid: 24 topic_id: 12611 reply_id: 46158[/import]

That still didn’t work T.T! the console says that x and y become nil values… [import]uid: 56659 topic_id: 12611 reply_id: 46306[/import]

Take a look in your console window to see if there are error messages, that might be why they all seem to stop. (If you’re on a Mac you can start Corona using the Corona Terminal to see the console messages)

Maybe try like this - don’t scale the healthBar, instead reset the width on the enterFrame i.e. in your move()

[code]
local waypointsY = {100, 150, 200, 250, 300, 350}; – or whatever

local function dino()
local health = 100;
local dinos = display.newCircle(0,0,10);
dinos.x = -47;
dinos.y = waypointsY[math.random(1, #waypointsY)];

local healthBar = display.newRect(0,0,100,5);
healthBar.x = dinos.x
healthBar.y = dinos.y - 30
healthBar:setFillColor(255,0,0);
healthBar.isVisible = false;

local function tapGone(e)
if(e.phase == “began”) then
healthBar.isVisible = true;
health = health - 20;
end
if(e.phase == “ended”)then
healthBar.isVisible = false;
end
end – end TapGone

local function move(e)
if dinos then
dinos.x = dinos.x + 1;
end
if healthBar then
healthBar.x = healthBar.x + 1;
healthBar.width = health;
end
if health <= 0 then
if healthBar then
healthBar:removeSelf(); healthBar = nil;
end
if dinos then
dinos:removeSelf();
dinos = nil;
end
end
end – end Move

– Listeners
dinos:addEventListener(“touch”, tapGone);
Runtime:addEventListener(“enterFrame”, move);
– End Listeners

end – end function

timer.performWithDelay(300, dino, 23);
[/code] [import]uid: 6787 topic_id: 12611 reply_id: 46334[/import]

Hello,

I have a similar problem. Whenever i have another collision and add these words “tempLine:removeSelf()” I can’t draw any more of the line the same as tempLine = false. Another problem is adding collision to the line, i’m not sure why it has had so many problems but it is not working properly. Do you know how to add collision and to remove it ? Thanks a lot.
The code i’ve been using:

[code]
require “physics”
physics.start()
physics.setGravity(0,5)
–physics.setDrawMode “hybrid”

local ball = display.newCircle(0,0,20);
ball.x = display.contentWidth/2
ball.y = display.contentHeight/8

physics.addBody(ball, {friction=0.3, bounce = 0.3, radius = 20})

–Draw line

local i = 1
local tempLine
local ractgangle_hit = {}
local prevX , prevY
local function runTouch(event)
if(event.phase==“began”) then
if(tempLine==nil) then
tempLine=display.newLine(event.x, event.y, event.x, event.y)
r = math.random (0, 255)
g = math.random ( 0, 255)
b = math.random (0, 255 )
tempLine:setColor(r,g, b)
prevX = event.x
prevY = event.y
end
elseif(event.phase==“moved”) then
tempLine:append(event.x,event.y-2)
tempLine.width=tempLine.width+0.9
ractgangle_hit[i] = display.newLine(prevX, prevY, event.x, event.y)
ractgangle_hit[i]:setColor(r,g, b)
ractgangle_hit[i].width = 4
local Width = ractgangle_hit[i].width * 0.6
local Height = ractgangle_hit[i].height * 0.2
local lineShape = {-Width,-Height,Width,-Height,Width,Height,-Width,Height}
physics.addBody(ractgangle_hit[i], “static”, { bounce = -1, density=0.3, friction=0.7, shape = lineShape})
prevX = event.x
prevY = event.y
i = i + 1
elseif(event.phase==“ended”) then
tempLine.parent.remove(tempLine)
tempLine=nil
end
end
Runtime:addEventListener(“touch”, runTouch)

– The collision that does not work ( And the line does not remove self)
– But usually if you have another collision and have the ball remove Self you can’t draw anymore.

local function touchLine( self, event )

tempLine:removeSelf()

end

tempLine.collision = touchLine
tempLine:addEventListener( “collision”, tempLine) [import]uid: 23689 topic_id: 12611 reply_id: 46357[/import]