The objects do not remove once they get to the location specified in the code below
local composer = require(“composer”)
local scene = composer.newScene()
local physics = require “physics”
local carHit = false
function scene:create( event )
local group = self.view
background = display.newImage(“Background.png”)
background.x = 160; background.y = 245
group:insert(background)
car = display.newImage(“Car3.png”)
car.x = 225; car.y = 400
physics.addBody( car, { density = 0, friction = 0, bounce = 0})
car.gravityScale = 0
car.type = “car”
car.collision = carCollision
leftTouch = display.newRect(69,463,132,120)
leftTouch.isVisible = false
leftTouch.isHitTestable = true
rightTouch = display.newRect(260,463,132,120)
rightTouch.isVisible = false
rightTouch.isHitTestable = true
function leftTouch:touch(e)
if e.phase == “began” then
transition.moveTo( car, { x= 100, y= 400, time= 50} )
end
end
function rightTouch:touch(e)
if e.phase == “began” then
transition.moveTo(car, { x= 225, y = 601, time = 50})
end
end
function carCollision(self, event)
if event.phase == “began” then
print(“carHit”)
carHit = true
display.remove(car)
storyboard.gotoScene(“Scene_menu”)
end
end
function spawnBlockRight()
if (carHit == false) then
block = display.newRect(230,-75,50,59)
block:setFillColor(0.5,0.6,0.9)
block.isVisible = true
physics.addBody( block, { density = 0, friction = 0, bounce = 0})
block.gravityScale = 0
transition.moveTo(block, { x= 225, y = 601, time = 600})
elements:insert(block)
group:insert(block)
end
end
function spawnBlockLeft()
if (carHit == false) then
circle1 = display.newCircle(100, -75, 15)
circle1:setFillColor(255,255,255)
circle1.isVisible = true
transition.moveTo(circle1, { x = 100, y = 601, time = 600} )
physics.addBody( circle1, { density = 0, friction = 0, bounce = 0})
circle1.gravityScale = 0
elements:insert(circle1)
group:insert(circle1)
end
end
function randomnum()
if (carHit == false) then
WhichBlock = math.random(1,300)
print(WhichBlock)
end
end
function spawn()
if (WhichBlock < 150) then
spawnBlockLeft()
else
spawnBlockRight()
end
end
elements = display.newGroup()
function deleteBlocks()
for a = elements.numChildren, 1, -1 do
if (elements[a].y > 600) then
elements:remove(elements[a])
print(“deleted”)
end
end
end
end
function scene:show(event)
local group = self.view
physics.start()
car:addEventListener(“collision”, car)
group:insert(car)
leftTouch:addEventListener(“touch”, leftTouch)
group:insert(leftTouch)
rightTouch:addEventListener(“touch”, rightTouch)
group:insert(rightTouch)
timer.performWithDelay(10, deleteCar,0)
timer.performWithDelay(500 , randomnum,0)
timer.performWithDelay(1000, spawn, 0)
timer.performWithDelay(2, deleteBlocks,0)
end
scene:addEventListener(“create”, scene)
scene:addEventListener(“show”, scene)
return scene