0

我有随机生成的对象,它们会自动在屏幕上移动。我想要它,以便当对象到达某个 x 位置时它会自行消失。

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)

所以一旦到达object[objectTag].x = 348对象就会消失

4

2 回答 2

3

尝试这个:

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)

继续编码...... :)

于 2013-10-18T15:44:22.247 回答
0

您应该在transition.to通话中执行此操作:

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})
于 2013-10-18T18:39:56.117 回答