Respawning the same objects after removeSelf()

Hi guys

Is there any way to respawn an object after removing it?

I’m using the code of a scrolling background, and here is what I have so far.

  
local object1 = display.newImage("object1.png")  
object1.x = object1.contentWidth - 100  
object1.y = object1.y - 30  
physics.addBody(object1, "kinematic")  
  
local tSpeed = 0.1  
  
local tPrevious = system.getTimer()  
local function move(event)  
 local tDelta = event.time - tPrevious  
 tPrevious = event.time  
  
   
 local yOffset = ( tSpeed \* tDelta )  
   
 object1.y = object1.y + yOffset  
  
  
 if (object1.y + object1.contentHeight) \> 1200 then  
  
 object1:translate(0, - 480 \* 2)  
   
 end  
   
end  
  
Runtime:addEventListener( "enterFrame", move );  
local function onCollision( self, event )  
 if ( event.phase == "began" ) then  
 object1:removeSelf(object1)  
 end  
end  
  
object1.collision = onCollision  
object1:addEventListener( "collision", object1 )  
  

Once the object has collided & removed, I’d like it to respawn on the next loop.
Anyone able to help with this please?
Thanks in advance! [import]uid: 40538 topic_id: 9544 reply_id: 309544[/import]

You can use timer.performWithDelay to spawn the object.

like this:

Note: I haven’t tested the code below.

[lua]local createObj = function ()
local object1 = display.newImage(“object1.png”)
object1.x = object1.contentWidth - 100
object1.y = object1.y - 30
physics.addBody(object1, “kinematic”)
end

– this is the first or initial spawn
local tTimer = timer.performWithDelay(1, createObj, 1)

– inside your collision you can try this
local function onCollision( self, event )
if ( event.phase == “began” ) then
– use self instead of object1
– i dont’ think it matters in your case, but I just like self better than the object’s name
self:removeSelf()

– now do your spawn
local tTimer2 = timer.performWithDelay(500, createObj, 1)

end
end[/lua]

good luck.
check out my game:
Touch Cannon – a **FREE** game

Please rate :slight_smile: [import]uid: 12455 topic_id: 9544 reply_id: 34866[/import]

teex84,

Thanks for looking into this, but unfortunately the above code doesn’t work.

Is there anyway, to use isBodyActive/isVisible with the the code I’ve given?

I think it may be better so that I’m not constantly spawning & removing. I’ve tried playing around with this myself, but I cannot seem to make the item active/visible again.
Thank again! [import]uid: 40538 topic_id: 9544 reply_id: 34874[/import]

I am struggling with removeself, I am new to corona and just testing still I am trying to write a simple app, it performs a simple query from an sqlite database and displays text on screen, then I want to hit a button overwrite the text with new text
here is a section of the code:

[lua]for row in db:nrows(“SELECT * FROM 1 WHERE 2 <365 ORDER BY RANDOM() LIMIT 1”) do

local myGroup = display.newGroup()

local myText = row.1…"\n"…row.2
local myTextObject = wrappedText( myText, 40, 18, “Calibri”, {255, 255, 255} )
myGroup:insert( myTextObject )

myGroup.x = 30

myGroup.y = 190

–works gret up to here

function surprise:tap( event )

myGroup:removeSelf ()
– myGroup text removed

for row in db:nrows(“SELECT * FROM 1 WHERE id <365 ORDER BY RANDOM() LIMIT 1”) do

local myGroup = display.newGroup()

local myText = row.1…"\n"…row.2

local myTextObject = wrappedText( myText, 40, 18, “Calibri”, {255, 255, 255} ) --syntax: wrappedText(string, lineLength, fontSize, fontFace, {r, g, b}, indentString1, indentString2 )

myGroup:insert( myTextObject )

myGroup.x = 30

myGroup.y = 190
– second text displayed
end
end

end

surprise:addEventListener( “tap”, surprise )[/lua]

The first tap work, the previous text is removed and the new text replaced, on the second tap after I get an error in the terminal:
Attempt to call method ‘removeSelf’
Stack traceback

Thanks [import]uid: 15691 topic_id: 9544 reply_id: 36585[/import]

Here’sa function for a tiled background that I wrote that works. It selects from a set of four tiles and flips them randomly to create a constinuous seemingly unique background.

This is for iPad.

I use a runtimelistener rather than a collision listener to remove the tiles.

[code]
local tilex, tiley, tiletimer, tilenum, tiletime, tilerotate
local tilegroup = display.newGroup()

function maketile()
tilenum = math.random(1,2)
if tilenum == 1 then
tilerotate = 180
elseif tilenum == 2 then
tilerotate = 0
end
tilenum = math.random(1,4)
if tilenum == 1 then
local tile = display.newImage(“tileone.png”)
tiles[#tiles+1] = tile
tile.x = tilex
tile.y = tiley
tile:rotate(tilerotate)
tilegroup:insert( tile )
transition.to(tile, {time=tiletime, x=tile.x, y=1290} )
elseif tilenum == 2 then
local tile = display.newImage(“tiletwo.png”)
tiles[#tiles+1] = tile
tile.x = tilex
tile.y = tiley
tile:rotate(tilerotate)
tilegroup:insert( tile )
transition.to(tile, {time=tiletime, x=tile.x, y=1290} )
elseif tilenum == 3 then
local tile = display.newImage(“tilethree.png”)
tiles[#tiles+1] = tile
tile.x = tilex
tile.y = tiley
tile:rotate(tilerotate)
tilegroup:insert( tile )
transition.to(tile, {time=tiletime, x=tile.x, y=1290} )
elseif tilenum == 4 then
local tile = display.newImage(“tilefour.png”)
tiles[#tiles+1] = tile
tile.x = tilex
tile.y = tiley
tile:rotate(tilerotate)
tilegroup:insert( tile )
transition.to(tile, {time=tiletime, x=tile.x, y=1290} )
end
end

function tilesetup()

tilex = 378
tiley = 890
tiletime = 22500

maketile()

tilex = 378
tiley = 634
tiletime = 37500

maketile()

tilex = 378
tiley = 378
tiletime = 52500

maketile()

tilex = 378
tiley = 118
tiletime = 67500

maketile()

tilex = 378
tiley = -138
tiletime = 82500

maketile()

tilex = 378
tiley = -396
tiletime = 97500

maketile()

tilex = 378
tiley = -138
tiletime = 82500
end

tilesetup()

function listener()
for i, tile in pairs(tiles) do
if tile.y > 1289 then
tile:removeSelf()
tile = nil
tiles[i] = nil
–print(“tile removed”)
timer.performWithDelay(10, maketile, 1)
end
end
end

Runtime:addEventListener(“enterFrame”, listener)
[/code] [import]uid: 10903 topic_id: 9544 reply_id: 37234[/import]