我很新,并且抬起我的问题无济于事,我所拥有的是从屏幕底部漂浮到顶部的气球等对象,我的问题是spawnBallons函数只被调用一次所以只有出现一个对象我想多次调用该函数来生成多个对象并增加每20秒生成一次的对象数量?
function spwanBalloons()
local allBalloons = {"green_balloon2.png", "red_balloon.png"}
ballons = display.newImage(allBalloons[math.random(#allBalloons)])
ballons.x = math.random(display.contentWidth)
ballons.y = display.contentHeight + 60
transition.to( ballons, { time=math.random(3500-speedBump, 4500-speedBump), y=-100} )
speedBump = speedBump + 15
end
function startGame()
scoreText = display.newText( "Score: 0", 0, 0, "Helvetica", 22 )
scoreText.x = centerX
scoreText.y = display.screenOriginY + 10
spwanBalloons()
end
我尝试了这个timer.performWithDelay( 500, spwanBalloons, 50 )
但它所做的就是在半秒内产生了50个物体,我想生成一个随机数量的物体,直到我告诉它停止?
答案 0 :(得分:1)
这应该每半秒产生1个气球20秒,然后每半秒产生2个气球等。
您可以根据需要使用stopSpawnIncrease()
和stopSpawn()
停止计时器。
local spawnIncreaseTimer
local spawnNumber=0
local function spawnIncrease()
spawnNumber=spawnNumber+1
spawnIncreaseTimer = timer.performWithDelay( 20000, spawnIncrease)
end
function stopSpawnIncrease()
timer.cancel( spawnIncreaseTimer )
end
local spawnTimer
function spwanBalloons()
for i=1,spawnNumber do
local allBalloons = {"green_balloon2.png", "red_balloon.png"}
ballons = display.newImage(allBalloons[math.random(#allBalloons)])
ballons.x = math.random(display.contentWidth)
ballons.y = display.contentHeight + 60
transition.to( ballons, { time=math.random(3500-speedBump, 4500-speedBump), y=-100} )
speedBump = speedBump + 15
end
spawnTimer = timer.performWithDelay( 500, spwanBalloons )
end
function stopSpawnTimer()
timer.cancel( spawnTimer )
end
function startGame()
scoreText = display.newText( "Score: 0", 0, 0, "Helvetica", 22 )
scoreText.x = centerX
scoreText.y = display.screenOriginY + 10
spawnIncrease() -- first call brings from 0 to 1 and starts timer
spwanBalloons()
end