Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local mRandom = math.random
- local objects = {"Vehicle11" ,"Vehicle21","Vehicle31","Vehicle41"}
- local objectTag = 0
- local object = {}
- local function spawncarright()
- local rightcar = {408,312}
- objectTag = objectTag + 1
- local objIdx = mRandom(#objects)
- local objName = objects[objIdx]
- object[objectTag] = display.newImage(objName..".png") -- see the difference here
- object[objectTag].x = 32
- object[objectTag].y = rightcar[math.random(1,2)]
- object[objectTag].name = objectTag
- transition.to(object[objectTag], {time = 3500, x = 348})
- end
- timer.performWithDelay(2000,spawncarright,0)
- local function deSpawn()
- for i=1,objectTag do
- if(object[i]~=nil and object[i].x~=nil and object[i].x>=348)then
- -- If you want to remove the object, then use the following 2 lines --
- object[i]:removeSelf()
- print("Removed object["..i.."]")
- --or else if you want to reposition the object, then uncomment the following --
- --[[
- spawncarright()
- --]]
- end
- end
- end
- Runtime:addEventListener("enterFrame",deSpawn)
- object[objectTag].deleteSelf = function(self)
- object[self.name] = nil -- Remove reference to object in table
- display.remove(self)
- self = nil
- end
- local localObj = object[objectTag] -- Do this so the object doesn't change with the objectTag does; if objectTag is incremented, then when the transition ends, it won't be pointing to the same object when we call the function
- transition.to(localObj, {time = 3500, x = 348, onComplete = function() localObj:deleteSelf() end})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement