如何让这个每隔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>向凯文致敬
发布于 2013-02-21 23:13:15
您的代码缺少total_smiles value assignment和delay参数。
工作代码:
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);此外,您应该存储对创建的微笑的引用,以便正确地销毁它们,并且不会泄漏内存。more info on memory managment
local smiles = {}
table.insert(smiles, smile)和处理:
for i=#smiles,1,-1 do
smiles[i]:removeSelf()
smiles[i] = nil
end发布于 2013-02-12 15:06:03
将计时器改为每10.000毫秒执行一次,而不是0。并且您的侦听器函数并没有真正满足任何目的,请删除它,并将spawnsmile函数中的transition.to更改为
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中有一个值,但我猜你在其他地方也有它。
https://stackoverflow.com/questions/14820599
复制相似问题