Lua 如何每10秒生成多个对象

Lua 如何每10秒生成多个对象,lua,coronasdk,corona-storyboard,Lua,Coronasdk,Corona Storyboard,如何每10秒生成一个“math.random(1,3)”smile.png,并在左屏幕后删除smile.png <code> local physics = require ("physics"); physics.start(); local function listener(me) transition.to (me, {time = math.random(1000,4000), x = math.random(10,310), y = -30, onComplete =

如何每10秒生成一个“math.random(1,3)”smile.png,并在左屏幕后删除smile.png

<code>
local physics = require ("physics");
physics.start();

local function listener(me)
 transition.to (me, {time = math.random(1000,4000), x = math.random(10,310), y = -30, onComplete = function()listener(me)end});
end

--Spawning multiple objects in randoms locations
local function spawnsmile()

        local smile = display.newImageRect("smile.png", 45, 45);
        smile:setReferencePoint(display.CenterReferencePoint);
        smile.x = math.random(-10, 400);
        smile.y = -40;
        transition.to( smile, {time = math.random(2000, 8000), x = math.random(-10, 400) , y = 600,});
        physics.addBody(smile, "dynamic", {density = 0.1, bounce = 0.1, friction = .1, radius = 0});

                --Adding touch event
                smile:addEventListener("touch", smile);
end
 tmr = timer.performWithDelay(0, spawnsmile, total_smiles);
<code>

将计时器更改为每10.000毫秒执行一次,而不是0。而你的侦听器函数并没有真正达到任何目的,请删除它,并将你的转换更改为

    transition.to( smile, {time = math.random(2000, 8000), x = math.random(-10, 400) , y = 600, onComplete = function(obj) obj:removeSelf() obj = nil end});

这应该是您希望它做的事=)total_smiles中也需要有一个值,但我猜您在其他地方有它。

您的代码缺少total_smiles值赋值延迟参数

工作代码:

local physics = require ("physics");
physics.start();

local function listener(me)
    transition.to (me, {time = math.random(1000,4000), x = math.random(10,310), y = -30, onComplete = function()listener(me)end});
end

--Spawning multiple objects in randoms locations
local function spawnsmile()
    local smile = display.newImageRect("Button.png", 45, 45);
    smile:setReferencePoint(display.CenterReferencePoint);
    smile.x = math.random(-10, 400);
    smile.y = -40;
    transition.to( smile, {time = math.random(2000, 8000), x = math.random(-10, 400) , y = 600,});
    physics.addBody(smile, "dynamic", {density = 0.1, bounce = 0.1, friction = .1, radius = 0});

    --Adding touch event
    smile:addEventListener("touch", smile);
end

local total_smiles = 15
tmr = timer.performWithDelay(10000, spawnsmile, total_smiles);
此外,您应该存储对已创建微笑的引用,以便正确地销毁它们,并且不会泄漏内存

和处置:

for i=#smiles,1,-1 do
   smiles[i]:removeSelf()
   smiles[i] = nil
end

好的,所以我修改了代码,但它仍然只生成了一个smile.png
local函数spawnsille()local smile=display.newImageRect(“smile.png”,45,45);微笑:设置参考点(display.CenterReferencePoint);smile.x=math.random(-10400);微笑。y=-40;转换到(smile,{time=math.random(20008000),x=math.random(-10400),y=600,onComplete=function(obj)obj:removeSelf()obj=nil end});addBody(微笑,动态,{密度=0.1,反弹=0.1,摩擦力=0.1,半径=0});结束tmr=计时器。性能延迟(10000,生成微笑,总微笑,5)那么你的总微笑值是多少?计时器(tmr)的最后一个参数是它应该执行多少次。尝试将值5赋给total_smiles,看看会发生什么=)
for i=#smiles,1,-1 do
   smiles[i]:removeSelf()
   smiles[i] = nil
end