Spawn same object after it is destroyed

Hey guys how do I make it so when the “main player” dies then when you go back into the level it will spawn the main player and same for the enemy. I have already tried so when one of them dies it destroys both of them then displays a new one but that only works a couple of times and the objects have no controls.

Thanks ,Tyler Jacobson

function scene:create( event ) physics.start() -- Called when the scene's view does not exist. -- INSERT code here to initialize the scene -- e.g. add display objects to 'sceneGroup', add touch listeners, etc. local movement = 5 local playerhlth = 8 local enemyhlth = 13 local sceneGroup = self.view print(enemyhlth) print(playerhlth) -- create a grey rectangle as the backdrop local sidebar = display.newRect(-50,0,50,600) physics.addBody(sidebar,"static") local bg = display.newRect(250,100,1000,1000) bg:setFillColor(0,0,1) local square = display.newRect(50,150,30,30) square:setFillColor(1,0,0) physics.addBody(square,"dynamic") local sidebar2 = display.newRect(500,0,50,600) physics.addBody(sidebar2,"static") local enemy = display.newRect(400,50,30,30) physics.addBody(enemy,"dynamic") local spike = display.newImage("spike.png",-10,230) physics.addBody(spike,"static") enemy.category = "enemy" local jumpbtn = display.newImage("arrowup.png",0,290) jumpbtn:scale(0.1,0.1) -- Make character jump function jump(event) if(event.phase == "began") then square:setLinearVelocity( 0, -200 ) end end jumpbtn:addEventListener("touch",jump) local function attackenemy( event ) enemyhlth = enemyhlth- 1 print("main player is attacking") local enemyhealth = display.newText(enemyhlth,enemy.x,enemy.y,25,25) transition.to(enemyhealth,{time = 4000,x=0,y=-500}) print("enemy health is",enemyhlth) if enemyhlth == 0 then composer.removeScene("level1") print("enemy is dead :)") composer.gotoScene("youwin") square:removeSelf() local square = display.newRect(50,150,30,30) square:setFillColor(1,0,0) physics.addBody(square,"dynamic") local enemy = display.newRect(400,50,30,30) physics.addBody(enemy,"dynamic") enemy:removeSelf() end end enemy:addEventListener("touch",attackenemy) local function spikeC(self,event) composer.gotoScene("lose") print("collision with spike") end spike.collision = spikeC local function enemyattack( self,event ) if (event.phase == "began") then if ( event.other.category == "enemy" ) then --subtract health, etc. playerhlth = playerhlth - 1 print("you are being attacked") print("your health is",playerhlth) local urhealth = display.newText(playerhlth,square.x,square.y,25,25) urhealth:setFillColor(0,1,0) transition.to(urhealth,{time= 4000,x=0,y=-500}) end end if playerhlth == 0 then composer.removeScene("level1") composer.gotoScene("lose") square:removeSelf() local square = display.newRect(50,150,30,30) square:setFillColor(1,0,0) physics.addBody(square,"dynamic") enemy:removeSelf() local enemy = display.newRect(400,50,30,30) physics.addBody(enemy,"dynamic") enemy:removeSelf() end end

Have you read/watched this:  https://coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/

It was done for storyboard, so the code isn’t that practical, but this tutorial is about concepts more than code (i.e. when do you remove scenes, how do you reset things), so it’s still quite applicable.

Rob

Have you read/watched this:  https://coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/

It was done for storyboard, so the code isn’t that practical, but this tutorial is about concepts more than code (i.e. when do you remove scenes, how do you reset things), so it’s still quite applicable.

Rob