为什么这个函数不能生成多个子弹?
我正在开发一个可以发射多颗子弹的函数,它是:
local function shootBullets ( event )
local bullet = display.newImageRect("images/Bullet.png", 12, 12) --Create the bullet image
physics.addBody(bullet, "kinematic", {bounce = 0}) --Allow physics stuff to work on it
bullets:insert( bullet ) --Add it to a global group called "bullets"
bullet:setLinearVelocity(20, 40) --Give it a velocity
end
我用这个计时器调用它:
timer.performWithDelay(10, shootBullets)
它移动一颗子弹,但不会制造新的子弹。每次调用 shootBullets ( event )
时,如何让它生成新的子弹?我对 Lua 不太熟悉,如果我做了一些明显错误的事情,或者我没有提供足够的信息,那么抱歉(如果您需要更多信息,请询问)。
I am working on a function to shoot multiple bullets here it is:
local function shootBullets ( event )
local bullet = display.newImageRect("images/Bullet.png", 12, 12) --Create the bullet image
physics.addBody(bullet, "kinematic", {bounce = 0}) --Allow physics stuff to work on it
bullets:insert( bullet ) --Add it to a global group called "bullets"
bullet:setLinearVelocity(20, 40) --Give it a velocity
end
And I am calling it with this timer:
timer.performWithDelay(10, shootBullets)
It moves one bullet, but it isn't making new ones. How can I have it spawn new bullets each time I call the shootBullets ( event )
? I am not that familiar with Lua so sorry if I am doing something obviously wrong, or if I am not giving enough information (if you need more information, ask).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哎呀,我应该更仔细地关注 API:
timer.performWithDelay(time, function, times)
的默认 3 个参数是 1。为了使其永远重复,我需要将其设置为 0。所以我更改:改为:
现在有子弹了。
Oops, I should pay closer attention to the API:
The default 3 parameter for
timer.performWithDelay(time, function, times)
is 1. To make it repeat forever I need to make it 0. So I changed:To this:
Now there are bullets.