I am stuck on this. How do I make only one bullet fire per second? Right now a bullet fires on mouse click and when I let go of the click, and they fire with no interval. I need to get it so it only fires one at a time, and it only shoots 1 per second no matter how many times you click on it. [import]uid: 48020 topic_id: 23158 reply_id: 323158[/import]
Check the API listings for “timer.performWithDelay.” That will do exactly what you want.
Instead of firing, (pardon the pun) your bullet function on pressing the button, you’ll want to start a timer which runs your bullet function every 100 milliseconds. When you remove your finger the timer pauses/stops.
To get the touch event to recognise the difference between starting and stopping the touch event you can use
if (event.phase == "began") then
--start the timer
end
if (event.phase == "ended") then
--stop the timer
end
Hope that makes sense? [import]uid: 67933 topic_id: 23158 reply_id: 92633[/import]
Check the API listings for “timer.performWithDelay.” That will do exactly what you want.
Instead of firing, (pardon the pun) your bullet function on pressing the button, you’ll want to start a timer which runs your bullet function every 100 milliseconds. When you remove your finger the timer pauses/stops.
To get the touch event to recognise the difference between starting and stopping the touch event you can use
if (event.phase == "began") then
--start the timer
end
if (event.phase == "ended") then
--stop the timer
end
Hope that makes sense? [import]uid: 67933 topic_id: 23158 reply_id: 92634[/import]
Hi Xavasoft -
To keep it from firing twice, you could either use the “tap” event instead of the “touch” event. If you want to use the “touch” event, then you will need something like this in the event handler:
[lua]local function fireBullet(event)
if (event.phase==“released”) then
– whatever you need to fire your bullet
end
end[/lua]
To make it so you can only shoot once per second, you could add a boolean called canFire that you set to false when you shoot. Add code to the above function so canFire has to be true in addition to event.phase being “released”. Then use something like the following to reset canFire after 1 second:
[lua]local function allowedToShoot
canFire=true
end
timer.performWithdelay(1000, allowedToShot)[/lua]
Mark [import]uid: 117098 topic_id: 23158 reply_id: 92636[/import]