Advertisement
Guest User

Untitled

a guest
Oct 20th, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. local mRandom = math.random
  2. local objects = {"Vehicle11" ,"Vehicle21","Vehicle31","Vehicle41"}
  3. local objectTag = 0
  4. local object = {}
  5.  
  6. local function spawncarright()
  7. local rightcar = {408,312}
  8.  
  9. objectTag = objectTag + 1
  10. local objIdx = mRandom(#objects)
  11. local objName = objects[objIdx]
  12. object[objectTag] = display.newImage(objName..".png") -- see the difference here
  13. object[objectTag].x = 32
  14. object[objectTag].y = rightcar[math.random(1,2)]
  15. object[objectTag].name = objectTag
  16. transition.to(object[objectTag], {time = 3500, x = 348})
  17.  
  18. end
  19. timer.performWithDelay(2000,spawncarright,0)
  20.  
  21. local function deSpawn()
  22. for i=1,objectTag do
  23. if(object[i]~=nil and object[i].x~=nil and object[i].x>=348)then
  24. -- If you want to remove the object, then use the following 2 lines --
  25. object[i]:removeSelf()
  26. print("Removed object["..i.."]")
  27. --or else if you want to reposition the object, then uncomment the following --
  28. --[[
  29. spawncarright()
  30. --]]
  31. end
  32. end
  33. end
  34. Runtime:addEventListener("enterFrame",deSpawn)
  35.  
  36. object[objectTag].deleteSelf = function(self)
  37. object[self.name] = nil -- Remove reference to object in table
  38. display.remove(self)
  39. self = nil
  40. end
  41.  
  42. 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
  43.  
  44. transition.to(localObj, {time = 3500, x = 348, onComplete = function() localObj:deleteSelf() end})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement