What is the best way to re-spawn/relocate object after the function of collision is completed?

I’m stuck in my game I would appreciate some help.

My game is simple I have 2 main objects a bottle/ball and a bucket/basket. The bottle will spawn in top of the screen randomly and the bucket will move from left to right in kinematic mode in the bottom screen and the player has to drop the bottle/ball inside the bucket/basket. If he does so the level and the speed of the bucket will be increased . I have finished to manage the movement of the bucket, the random spawn of the bottle and the “hole” of the bucket my problem is what is the best way to remove the bottle and re-spawn it on top to increase the level which increases the speed of the bucket so the game wont stop. So basically like the game angry birds, the bird on the trebuchet to be respawn after gets throwed or like a 2d basketball game.

Below is my amateur game code.


– main.lua


– Physics

local physics = require( “physics” )

physics.start()

physics.setDrawMode(“hybrid”)

physics.setGravity(0,0)

–Objects

local background = display.newImageRect(“background.png”,360,570)

background.x = display.contentCenterX

background.y = display.contentCenterY

local ground = display.newImageRect(“platform.png”,1000,50)

ground.x = display.contentCenterX

ground.y = display.contentHeight-0

physics.addBody(ground,“static”,{ bounce=0, friction=0 })

local staticBox = display.newRect( 100, 100, display.contentWidth-240, 10 )

staticBox:setFillColor( 0.2, 0.2, 1 )

staticBox.x, staticBox.y = display.contentWidth-255, 450

staticBox.class = “inside”

physics.addBody( staticBox, “kinematic”, { bounce=0, friction=0 } )

–staticBox.isVisible = false

local bumperA = display.newRect(  0, 0, 10, 90 )

bumperA:setFillColor( 0.2, 0.2, 1 )

bumperA.x, bumperA.y = 20, 410

physics.addBody( bumperA, “kinematic”, { bounce=0.1, friction=1 } )

–bumperA.isVisible = false

local bumperB = display.newRect(  0, 0, 10, 90 )

bumperB:setFillColor( 0.2, 0.2, 1 )

bumperB.x, bumperB.y = 110, 410

physics.addBody( bumperB, “kinematic”, { bounce=0.1, friction=1 } )

–bumperB.isVisible = false

local bucket = display.newImageRect(“crate.png”,100,90)

bucket.x = 65

bucket.y = display.contentHeight-70

–physics.addBody(bucket, “kinematic”,{bounce=0, friction=0})

  balloon = display.newImageRect( “balloon.png”, 50, 50 )

  balloon.x = math.random(100,270)

  balloon.y = display.contentCenter20

  physics.addBody( balloon, “static”, { radius=25, bounce=0.1 } )

  balloon.alpha = 0.8

  balloon.class = “ball”

    – function to handle the collision on the ball

    function balloon:collision(e)

        – only perform logic when the ball is colliding with a wall

        if (e.other.class == “inside”) then

             balloon.bodyType = “static”

            transition.to(balloon,{time=0,x = math.random(50,270),y=0})

           

            – cannot remove objects during a collision, so wait a short moment for it to end

           

        end

    end

    balloon:addEventListener(“collision”,balloon)

function moveBucketRight()

transition.to (bucket,{time=5000, x=260})

transition.to (bumperB,{time=5000, x=305})

transition.to (bumperA,{time=5000, x=215})

transition.to (staticBox,{time=5000, x=260, onComplete=moveBucketLeft})

end

function moveBucketLeft()

–transition.to( platform, {} )

transition.to(bucket,{time=5000, x=60})

transition.to (bumperB,{time=5000, x=105})

transition.to (bumperA,{time=5000, x=15})

transition.to(staticBox,{time=5000, x=60, onComplete=moveBucketRight})

end

moveBucketRight()

–Functions

local tapCount = 0

local tapText = display.newText( tapCount, display.contentCenterX, 20, native.systemFont, 40 )

tapText:setFillColor( 0, 0, 0 )

local function pushBalloon()

  balloon:applyLinearImpulse(0,0.10,balloon.x, balloon.y)

  physics.setGravity(0,9.8)

  balloon.bodyType = “dynamic”

  tapCount = tapCount + 1

    tapText.text = tapCount

end

 Runtime:addEventListener( “tap”, pushBalloon )

Sorry for my noobish post here is the formated code.

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Physics local physics = require( "physics" ) physics.start() physics.setDrawMode("hybrid") physics.setGravity(0,0) --Objects local background = display.newImageRect("background.png",360,570) background.x = display.contentCenterX background.y = display.contentCenterY local ground = display.newImageRect("platform.png",1000,50) ground.x = display.contentCenterX ground.y = display.contentHeight-0 physics.addBody(ground,"static",{ bounce=0, friction=0 }) local staticBox = display.newRect( 100, 100, display.contentWidth-240, 10 ) staticBox:setFillColor( 0.2, 0.2, 1 ) staticBox.x, staticBox.y = display.contentWidth-255, 450 staticBox.class = "inside" physics.addBody( staticBox, "kinematic", { bounce=0, friction=0 } ) --staticBox.isVisible = false local bumperA = display.newRect( 0, 0, 10, 90 ) bumperA:setFillColor( 0.2, 0.2, 1 ) bumperA.x, bumperA.y = 20, 410 physics.addBody( bumperA, "kinematic", { bounce=0.1, friction=1 } ) --bumperA.isVisible = false local bumperB = display.newRect( 0, 0, 10, 90 ) bumperB:setFillColor( 0.2, 0.2, 1 ) bumperB.x, bumperB.y = 110, 410 physics.addBody( bumperB, "kinematic", { bounce=0.1, friction=1 } ) --bumperB.isVisible = false local bucket = display.newImageRect("crate.png",100,90) bucket.x = 65 bucket.y = display.contentHeight-70 --physics.addBody(bucket, "kinematic",{bounce=0, friction=0}) balloon = display.newImageRect( "balloon.png", 50, 50 ) balloon.x = math.random(100,270) balloon.y = display.contentCenter20 physics.addBody( balloon, "static", { radius=25, bounce=0.1 } ) balloon.alpha = 0.8 balloon.class = "ball" -- function to handle the collision on the ball function balloon:collision(e) -- only perform logic when the ball is colliding with a wall if (e.other.class == "inside") then balloon.bodyType = "static" transition.to(balloon,{time=0,x = math.random(50,270),y=0}) -- cannot remove objects during a collision, so wait a short moment for it to end end end balloon:addEventListener("collision",balloon) function moveBucketRight() transition.to (bucket,{time=5000, x=260}) transition.to (bumperB,{time=5000, x=305}) transition.to (bumperA,{time=5000, x=215}) transition.to (staticBox,{time=5000, x=260, onComplete=moveBucketLeft}) end function moveBucketLeft() --transition.to( platform, {} ) transition.to(bucket,{time=5000, x=60}) transition.to (bumperB,{time=5000, x=105}) transition.to (bumperA,{time=5000, x=15}) transition.to(staticBox,{time=5000, x=60, onComplete=moveBucketRight}) end moveBucketRight() --Functions local tapCount = 0 local tapText = display.newText( tapCount, display.contentCenterX, 20, native.systemFont, 40 ) tapText:setFillColor( 0, 0, 0 ) local function pushBalloon() balloon:applyLinearImpulse(0,0.10,balloon.x, balloon.y) physics.setGravity(0,9.8) balloon.bodyType = "dynamic" tapCount = tapCount + 1 tapText.text = tapCount end Runtime:addEventListener( "tap", pushBalloon )

Ok I managed to do it this way now my problem is when the player miss the bucket a screen that says game over and his score will appear. After that I need to make a replay button so the player can start over. What might be the best way here is my code:

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here -- Physics local physics = require( "physics" ) physics.start() physics.setDrawMode("normal") physics.setGravity(0,0) --physics.addBody(bucket, "kinematic") --Objects local background = display.newImageRect("background.png",360,570) background.x = display.contentCenterX background.y = display.contentCenterY local ground = display.newImageRect("platform.png",1000,50) ground.x = display.contentCenterX ground.y = display.contentHeight-0 physics.addBody(ground,"static",{ bounce=0, friction=0 }) ground.class = "outside" local staticBox = display.newRect( 100, 100, display.contentWidth-240, 10 ) staticBox:setFillColor( 0.2, 0.2, 1 ) staticBox.x, staticBox.y = display.contentWidth-255, 450 staticBox.class = "inside" physics.addBody( staticBox, "kinematic", { bounce=0, friction=0 } ) --staticBox.isVisible = false local bumperA = display.newRect( 0, 0, 10, 90 ) bumperA:setFillColor( 0.2, 0.2, 1 ) bumperA.x, bumperA.y = 20, 410 physics.addBody( bumperA, "kinematic", { bounce=0.1, friction=1 } ) --bumperA.isVisible = false local bumperB = display.newRect( 0, 0, 10, 90 ) bumperB:setFillColor( 0.2, 0.2, 1 ) bumperB.x, bumperB.y = 110, 410 physics.addBody( bumperB, "kinematic", { bounce=0.1, friction=1 } ) --bumperB.isVisible = false local bucket = display.newImageRect("crate.png",100,90) bucket.x = 65 bucket.y = display.contentHeight-70 --physics.addBody(bucket, "kinematic",{bounce=0, friction=0}) balloon = display.newImageRect( "balloon.png", 50, 50 ) balloon.x = math.random(100,270) balloon.y = display.contentCenter20 physics.addBody( balloon, "static", { radius=25, bounce=0.1 } ) balloon.alpha = 0.8 balloon.class = "ball" maxscore= 0 tapCount = 0 tapText = display.newText( tapCount, display.contentCenterX, 20, native.systemFont, 40 ) tapText:setFillColor( 0, 0, 0 ) -- function to handle the collision on the ball function balloon:collision(e) -- only perform logic when the ball is colliding with a wall if (e.other.class == "inside") and ( e.phase == "began" ) then transition.to(balloon,{time=0,x = math.random(100,270),y=0 ,bodyType = "static"}) tapCount = tapCount + 1 --levels if (tapCount == 7) then bucketTime = 3750 else if (tapCount == 10) then bucketTime = 3500 end else if (tapCount == 13) then bucketTime = 3250 end else if (tapCount == 15) then bucketTime = 3000 end end tapText.text = tapCount maxscore = tapCount elseif (e.other.class == "outside") then local gameOver = "Game Over!".." \nYour score is :".. maxscore overText = display.newText( gameOver, display.contentCenterX, 150, native.systemFont, 40 ) overText:setFillColor( 0, 0, 0 ) transition.to(balloon,{time=0,x = math.random(50,270),y=0 ,bodyType = "static"}) tapCount = 0 bucketTime = 4000 overText.text = gameOver end end balloon:addEventListener("collision") --[[local balloon = display.newImageRect( "balloon.png", 50, 50 ) balloon.x = display.contentCenterX balloon.y = display.contentCenter20 balloon.isSensor=true balloon.alpha = 0.8]] --timer.performWithDelay(100, moveBucketRight, -100) --Loading the platform --Loading the physics bucketTime = 4000 function moveBucketRight() transition.to (bucket,{time=bucketTime, x=260}) transition.to (bumperB,{time=bucketTime, x=305}) transition.to (bumperA,{time=bucketTime, x=215}) transition.to (staticBox,{time=bucketTime, x=260, onComplete=moveBucketLeft}) end function moveBucketLeft() --transition.to( platform, {} ) transition.to(bucket,{time=bucketTime, x=60}) transition.to (bumperB,{time=bucketTime, x=105}) transition.to (bumperA,{time=bucketTime, x=15}) transition.to(staticBox,{time=bucketTime, x=60, onComplete=moveBucketRight}) end moveBucketRight() --Functions local function pushBalloon() balloon:applyLinearImpulse(0,0,balloon.x, balloon.y) physics.setGravity(0,10) balloon.bodyType = "dynamic" end Runtime:addEventListener( "tap", pushBalloon )

Sorry for my noobish post here is the formated code.

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Physics local physics = require( "physics" ) physics.start() physics.setDrawMode("hybrid") physics.setGravity(0,0) --Objects local background = display.newImageRect("background.png",360,570) background.x = display.contentCenterX background.y = display.contentCenterY local ground = display.newImageRect("platform.png",1000,50) ground.x = display.contentCenterX ground.y = display.contentHeight-0 physics.addBody(ground,"static",{ bounce=0, friction=0 }) local staticBox = display.newRect( 100, 100, display.contentWidth-240, 10 ) staticBox:setFillColor( 0.2, 0.2, 1 ) staticBox.x, staticBox.y = display.contentWidth-255, 450 staticBox.class = "inside" physics.addBody( staticBox, "kinematic", { bounce=0, friction=0 } ) --staticBox.isVisible = false local bumperA = display.newRect( 0, 0, 10, 90 ) bumperA:setFillColor( 0.2, 0.2, 1 ) bumperA.x, bumperA.y = 20, 410 physics.addBody( bumperA, "kinematic", { bounce=0.1, friction=1 } ) --bumperA.isVisible = false local bumperB = display.newRect( 0, 0, 10, 90 ) bumperB:setFillColor( 0.2, 0.2, 1 ) bumperB.x, bumperB.y = 110, 410 physics.addBody( bumperB, "kinematic", { bounce=0.1, friction=1 } ) --bumperB.isVisible = false local bucket = display.newImageRect("crate.png",100,90) bucket.x = 65 bucket.y = display.contentHeight-70 --physics.addBody(bucket, "kinematic",{bounce=0, friction=0}) balloon = display.newImageRect( "balloon.png", 50, 50 ) balloon.x = math.random(100,270) balloon.y = display.contentCenter20 physics.addBody( balloon, "static", { radius=25, bounce=0.1 } ) balloon.alpha = 0.8 balloon.class = "ball" -- function to handle the collision on the ball function balloon:collision(e) -- only perform logic when the ball is colliding with a wall if (e.other.class == "inside") then balloon.bodyType = "static" transition.to(balloon,{time=0,x = math.random(50,270),y=0}) -- cannot remove objects during a collision, so wait a short moment for it to end end end balloon:addEventListener("collision",balloon) function moveBucketRight() transition.to (bucket,{time=5000, x=260}) transition.to (bumperB,{time=5000, x=305}) transition.to (bumperA,{time=5000, x=215}) transition.to (staticBox,{time=5000, x=260, onComplete=moveBucketLeft}) end function moveBucketLeft() --transition.to( platform, {} ) transition.to(bucket,{time=5000, x=60}) transition.to (bumperB,{time=5000, x=105}) transition.to (bumperA,{time=5000, x=15}) transition.to(staticBox,{time=5000, x=60, onComplete=moveBucketRight}) end moveBucketRight() --Functions local tapCount = 0 local tapText = display.newText( tapCount, display.contentCenterX, 20, native.systemFont, 40 ) tapText:setFillColor( 0, 0, 0 ) local function pushBalloon() balloon:applyLinearImpulse(0,0.10,balloon.x, balloon.y) physics.setGravity(0,9.8) balloon.bodyType = "dynamic" tapCount = tapCount + 1 tapText.text = tapCount end Runtime:addEventListener( "tap", pushBalloon )

Ok I managed to do it this way now my problem is when the player miss the bucket a screen that says game over and his score will appear. After that I need to make a replay button so the player can start over. What might be the best way here is my code:

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here -- Physics local physics = require( "physics" ) physics.start() physics.setDrawMode("normal") physics.setGravity(0,0) --physics.addBody(bucket, "kinematic") --Objects local background = display.newImageRect("background.png",360,570) background.x = display.contentCenterX background.y = display.contentCenterY local ground = display.newImageRect("platform.png",1000,50) ground.x = display.contentCenterX ground.y = display.contentHeight-0 physics.addBody(ground,"static",{ bounce=0, friction=0 }) ground.class = "outside" local staticBox = display.newRect( 100, 100, display.contentWidth-240, 10 ) staticBox:setFillColor( 0.2, 0.2, 1 ) staticBox.x, staticBox.y = display.contentWidth-255, 450 staticBox.class = "inside" physics.addBody( staticBox, "kinematic", { bounce=0, friction=0 } ) --staticBox.isVisible = false local bumperA = display.newRect( 0, 0, 10, 90 ) bumperA:setFillColor( 0.2, 0.2, 1 ) bumperA.x, bumperA.y = 20, 410 physics.addBody( bumperA, "kinematic", { bounce=0.1, friction=1 } ) --bumperA.isVisible = false local bumperB = display.newRect( 0, 0, 10, 90 ) bumperB:setFillColor( 0.2, 0.2, 1 ) bumperB.x, bumperB.y = 110, 410 physics.addBody( bumperB, "kinematic", { bounce=0.1, friction=1 } ) --bumperB.isVisible = false local bucket = display.newImageRect("crate.png",100,90) bucket.x = 65 bucket.y = display.contentHeight-70 --physics.addBody(bucket, "kinematic",{bounce=0, friction=0}) balloon = display.newImageRect( "balloon.png", 50, 50 ) balloon.x = math.random(100,270) balloon.y = display.contentCenter20 physics.addBody( balloon, "static", { radius=25, bounce=0.1 } ) balloon.alpha = 0.8 balloon.class = "ball" maxscore= 0 tapCount = 0 tapText = display.newText( tapCount, display.contentCenterX, 20, native.systemFont, 40 ) tapText:setFillColor( 0, 0, 0 ) -- function to handle the collision on the ball function balloon:collision(e) -- only perform logic when the ball is colliding with a wall if (e.other.class == "inside") and ( e.phase == "began" ) then transition.to(balloon,{time=0,x = math.random(100,270),y=0 ,bodyType = "static"}) tapCount = tapCount + 1 --levels if (tapCount == 7) then bucketTime = 3750 else if (tapCount == 10) then bucketTime = 3500 end else if (tapCount == 13) then bucketTime = 3250 end else if (tapCount == 15) then bucketTime = 3000 end end tapText.text = tapCount maxscore = tapCount elseif (e.other.class == "outside") then local gameOver = "Game Over!".." \nYour score is :".. maxscore overText = display.newText( gameOver, display.contentCenterX, 150, native.systemFont, 40 ) overText:setFillColor( 0, 0, 0 ) transition.to(balloon,{time=0,x = math.random(50,270),y=0 ,bodyType = "static"}) tapCount = 0 bucketTime = 4000 overText.text = gameOver end end balloon:addEventListener("collision") --[[local balloon = display.newImageRect( "balloon.png", 50, 50 ) balloon.x = display.contentCenterX balloon.y = display.contentCenter20 balloon.isSensor=true balloon.alpha = 0.8]] --timer.performWithDelay(100, moveBucketRight, -100) --Loading the platform --Loading the physics bucketTime = 4000 function moveBucketRight() transition.to (bucket,{time=bucketTime, x=260}) transition.to (bumperB,{time=bucketTime, x=305}) transition.to (bumperA,{time=bucketTime, x=215}) transition.to (staticBox,{time=bucketTime, x=260, onComplete=moveBucketLeft}) end function moveBucketLeft() --transition.to( platform, {} ) transition.to(bucket,{time=bucketTime, x=60}) transition.to (bumperB,{time=bucketTime, x=105}) transition.to (bumperA,{time=bucketTime, x=15}) transition.to(staticBox,{time=bucketTime, x=60, onComplete=moveBucketRight}) end moveBucketRight() --Functions local function pushBalloon() balloon:applyLinearImpulse(0,0,balloon.x, balloon.y) physics.setGravity(0,10) balloon.bodyType = "dynamic" end Runtime:addEventListener( "tap", pushBalloon )