spawn bullet

local B1 = display.newCircle(0, 0, 40) B1:setFillColor(0.5) B1.x = 100 B1.y = 100 B1.anchorX, B1.anchorY =0.5, 0.5 function moveBullet(obj) if obj.y \> 0 then obj.y= obj.y + 6 obj:toBack() else obj:removeSelf() Runtime:removeEventListener("enterFrame",obj) obj = nil end end function Bullet() bullet = display.newCircle(0,0,5) bullet.x =200 bullet.y = 200 bullet.enterFrame = moveBullet Runtime:addEventListener("enterFrame",bullet) end function coolDown() B1:addEventListener("touch",shoot) end function shoot(e) if e.phase == "began" then Bullet() mShot = timer.performWithDelay(500,Bullet,0) elseif e.phase == "ended" then timer.cancel(mShot) B1:removeEventListener("touch",shoot) timer.performWithDelay(450,coolDown,1) end end B1:addEventListener("touch",shoot)

How to keep the same distance between the bullets and bullet?The performWithDelay of coolDown have problem.

Your listener function is not quite right … you’re trying to do an object-listener (or table-listener) but kind of calling it as if it were a function-listener.   I.e. your moveBullet function should really be taking an event argument, not an object argument.

This seems to work for me:

function Bullet() local bullet = display.newCircle(0,0,5) bullet.x =200 bullet.y = 200 function bullet:enterFrame(e) if bullet.y \> 0 then bullet.y= bullet.y + 6 bullet:toBack() else bullet:removeSelf() Runtime:removeEventListener("enterFrame",bullet) bullet = nil end end Runtime:addEventListener("enterFrame",bullet) end

 FWIW, the event (‘e’ argument) just has name and time components.

When i keep touching the button the distance between bullet and bullet is different ,when i hold the button the distance is same . How to make the distance become same when keep touching ?

local widget = require( "widget" ) function Bullet() local bullet = display.newCircle(0,0,5) bullet.x =200 bullet.y = 200 function bullet:enterFrame(e) if bullet.y \> 0 then bullet.y= bullet.y + 6 bullet:toBack() else bullet:removeSelf() Runtime:removeEventListener("enterFrame",bullet) bullet = nil end end Runtime:addEventListener("enterFrame",bullet) end function shoot(event) if event.phase == "began" then Bullet() mShot = timer.performWithDelay(500,Bullet,0) elseif event.phase == "ended" then timer.cancel(mShot) end end button1 = widget.newButton { left = 100, top = 200, id = "button1", label = "Button", onEvent = shoot }

If the differences are small, I’d wonder if this were just a glitch with the simulator, or a limit in the accuracy of the touch event mechanisms.

When I tried it out, when I clicked and held on the button, the bullets were a constant rate, constant separation between them.

Your listener function is not quite right … you’re trying to do an object-listener (or table-listener) but kind of calling it as if it were a function-listener.   I.e. your moveBullet function should really be taking an event argument, not an object argument.

This seems to work for me:

function Bullet() local bullet = display.newCircle(0,0,5) bullet.x =200 bullet.y = 200 function bullet:enterFrame(e) if bullet.y \> 0 then bullet.y= bullet.y + 6 bullet:toBack() else bullet:removeSelf() Runtime:removeEventListener("enterFrame",bullet) bullet = nil end end Runtime:addEventListener("enterFrame",bullet) end

 FWIW, the event (‘e’ argument) just has name and time components.

When i keep touching the button the distance between bullet and bullet is different ,when i hold the button the distance is same . How to make the distance become same when keep touching ?

local widget = require( "widget" ) function Bullet() local bullet = display.newCircle(0,0,5) bullet.x =200 bullet.y = 200 function bullet:enterFrame(e) if bullet.y \> 0 then bullet.y= bullet.y + 6 bullet:toBack() else bullet:removeSelf() Runtime:removeEventListener("enterFrame",bullet) bullet = nil end end Runtime:addEventListener("enterFrame",bullet) end function shoot(event) if event.phase == "began" then Bullet() mShot = timer.performWithDelay(500,Bullet,0) elseif event.phase == "ended" then timer.cancel(mShot) end end button1 = widget.newButton { left = 100, top = 200, id = "button1", label = "Button", onEvent = shoot }

If the differences are small, I’d wonder if this were just a glitch with the simulator, or a limit in the accuracy of the touch event mechanisms.

When I tried it out, when I clicked and held on the button, the bullets were a constant rate, constant separation between them.