Shooting Bullets the Right Way

Hello!

I’ve done some reading and I can’t figure out the best way to create a bullet, shoot it (from left to right), and properly dispose the bullet when it’s off the screen. Assuming that I’m using a screen that’s 320x480 in landscape.

local bullet = display.newImage("bullet.png") transition.to(bullet, { time = 1000, x = 400, onComplete = function(bullet) display.remove(bullet) end })

Is this the right way to create a bullet, move it, and remove it at the end of the transition? Is there a better way or something that follows best practices?

Thank you for your help!

Question: Do you want to use physics or a transition?  I see you using a transition, so this should work:

-- Warning: May contain typos!! -- local offset = 50 local startX = display.contentCenterX - display.actualContentWidth/2 - offset local endX = display.contentCenterX + display.actualContentWidth/2 + offset local speed = 150 -- pixels per second local distance = endX - startX local time = 1000 \* distance/speed local bullet = display.newCircle( startX, display.contentCenterY, 5 ) transition.to ( bullet, { x = endX, time = time, onComplete = display.remove } ) 

Tip: Also, notice how I changed the onComplete call.  You don’t need to wrap display.remove in a closure.

Thanks Ed! Just curious, why just the display.remove and not display.remove(obj) ?

For anyone else that stumbles across this thread, there’s an extra end at the end of the code. Here’s the tested code:

local offset = 50 local startX = display.contentCenterX - display.actualContentWidth/2 - offset local endX = display.contentCenterX + display.actualContentWidth/2 + offset local speed = 150 -- pixels per second local distance = endX - startX local time = 1000 \* distance/speed local bullet = display.newCircle( startX, display.contentCenterY, 5 ) transition.to (bullet, {x=endX, time=time, onComplete=display.remove })

Thanks again Ed!!

onComplete takes a reference to a function:

onComplete = function() display.remove( bullet ) end -- is a reference to generic 'closure' function onComplete = display.remove -- is a reference to function remove() onComplete = display.remove(bullet) -- is a call to remove()

PS - Fixed my typo, good catch

Question: Do you want to use physics or a transition?  I see you using a transition, so this should work:

-- Warning: May contain typos!! -- local offset = 50 local startX = display.contentCenterX - display.actualContentWidth/2 - offset local endX = display.contentCenterX + display.actualContentWidth/2 + offset local speed = 150 -- pixels per second local distance = endX - startX local time = 1000 \* distance/speed local bullet = display.newCircle( startX, display.contentCenterY, 5 ) transition.to ( bullet, { x = endX, time = time, onComplete = display.remove } ) 

Tip: Also, notice how I changed the onComplete call.  You don’t need to wrap display.remove in a closure.

Thanks Ed! Just curious, why just the display.remove and not display.remove(obj) ?

For anyone else that stumbles across this thread, there’s an extra end at the end of the code. Here’s the tested code:

local offset = 50 local startX = display.contentCenterX - display.actualContentWidth/2 - offset local endX = display.contentCenterX + display.actualContentWidth/2 + offset local speed = 150 -- pixels per second local distance = endX - startX local time = 1000 \* distance/speed local bullet = display.newCircle( startX, display.contentCenterY, 5 ) transition.to (bullet, {x=endX, time=time, onComplete=display.remove })

Thanks again Ed!!

onComplete takes a reference to a function:

onComplete = function() display.remove( bullet ) end -- is a reference to generic 'closure' function onComplete = display.remove -- is a reference to function remove() onComplete = display.remove(bullet) -- is a call to remove()

PS - Fixed my typo, good catch