A problem with shooting a machinegun continuously

Right now I am making a simple game; there is a cannon that shoots machinegun bullets at the upcoming viruses. However, the problem is that I want to make the gun shoot continuously when the screen is pressed (towards that location). However, the cannon only seems to shoot bullets when the finger is moved (when the finger is stationary it doesn’t shoot at all). Here’s part of the code:

[code]
function touched( event )
if( event.phase == “began” ) then

Runtime:addEventListener(“enterFrame”, shootMachinegun)
if bulletType == normal then
createBullet(0.5 * sr)
end
end

if (event.phase == “ended”) then
if bulletType == machinegun then
Runtime:removeEventListener(“enterFrame”, shootMachinegun)
end
end

local function shootMachinegun()
– we set the aim to where we touched
aim.x = event.x
aim.y = event.y

– make the cannon face the right direction!
directionX = aim.x - cannon.x
directionY = aim.y - cannon.y
cannon.rotation = math.deg(math.atan2(directionY,directionX)) + 90

– if the bulletType is machineGun
if bulletType == machinegun then
– important, bulletDuration sets the speed of the bullet firing.
– for example, if bulletDuration = 3, one bullet is fired every 3 frames
if bulletDurationCheck == 3 then
createBullet(1 * sr)
bulletDurationCheck = 0
else bulletDurationCheck = bulletDurationCheck + 1
end
end
end
end
[/code] [import]uid: 73204 topic_id: 15687 reply_id: 315687[/import]

i suggest to use Runtime Listener and a variable that will store information about “to fire/not to fire”
something like that, thats how i would do it anyway [import]uid: 16142 topic_id: 15687 reply_id: 57891[/import]

Phil,
strangely enough I will direct you to read up on this article here

what you need to do is on began, set a flag that says fStartShooting to true and then on ended, set it to false. In the enterFrame, keep spawning and moving the bullets till fStartShooting is false.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15687 reply_id: 57904[/import]