为什么这个函数不能制造多个子弹?

我正在制作一个发射多个子弹的函数,代码如下:

local function shootBullets ( event )
    local bullet = display.newImageRect("images/Bullet.png", 12, 12) --创建子弹图片
    physics.addBody(bullet, "kinematic", {bounce = 0}) --允许物理效果发挥作用
    bullets:insert( bullet ) --将子弹添加到一个称为“bullets”的全局组中
    bullet:setLinearVelocity(20, 40) --给它一个速度

end

我用以下计时器来调用它:

timer.performWithDelay(10, shootBullets)

它会移动一个子弹,但它不会制造新的子弹。我该如何每次调用shootBullets(事件)时创建新的子弹?我对Lua不太熟悉,如果我做了什么明显错误的事情,请见谅,或者如果我没有提供足够的信息(如果您需要更多信息,请问)。

原文链接 https://stackoverflow.com/questions/7054019

点赞
stackoverflow用户667648
stackoverflow用户667648

哎呀,我应该更注意 API:

timer.performWithDelay(time, function, times) 的默认第三个参数是 1。要使它无限重复,我需要将其改为 0。因此,我将代码从:

timer.performWithDelay(10, shootBullets)

改为了:

timer.performWithDelay(10, shootBullets, 0)

现在可以发射子弹了。

2011-08-14 00:09:05