开发者

Why is this function not making multiple bullets?

开发者 https://www.devze.com 2023-03-28 11:57 出处:网络
I am working on a function to shoot multiple bullets here it is: local function shootBullets ( event )

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).


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:

timer.performWithDelay(10, shootBullets)

To this:

timer.performWithDelay(10, shootBullets, 0)

Now there are bullets.

0

精彩评论

暂无评论...
验证码 换一张
取 消