在两个位置生成一个sprite

时间:2013-10-18 12:06:32

标签: object random lua corona

我想在y = 408和y = 360时生成一个图像。我有随机精灵代码,只是不知道如何在屏幕上的两个特定位置产生它。

local  mRandom = math.random
local  objects = {"Vehicle11" ,"Vehicle21","Vehicle31","Vehicle41"}
local objectTag = 1
local object = {}

local function spawncarright()
    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 = 408
    object[objectTag].name = objectTag
    print(objectTag)
end
timer.performWithDelay(1000,spawncarright,0)

1 个答案:

答案 0 :(得分:3)

只需做一点修改即可完成工作

local  mRandom = math.random

local  objects = {"Vehicle11" ,"Vehicle21","Vehicle31","Vehicle41"}
local objectTag = 1
local object = {}

local function spawncarright(y)
    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 = y
    object[objectTag].name = objectTag
end
timer.performWithDelay(1000, function() spawncarright(408); spawncarright(360);  end,0)