Re: Firing / shoot a bullet from object

Hey fellow devs, just a quick question from a beginner so please forgive me.

Ok so have this very simple demo of game i have been working on, its going very well so far.
Basic game concept is you move your spaceship left to right and shoot the enemies (UFO.png)

My question is regarding shooting a bullet from the “center” of the spaceship (player) when the “shoot button”
is pressed and then move up the screen along the x axis the hit and destroy the enemy (UFO)

I just cant figure out how to ‘spawn’ the bullet when the button is pressed and then move up the x axis,
I imagine its something to do with time per frame or something along those lines.

If anyone can help me with this issue by maybe giving me a snippet of example code to put in or maybe a tutorial etc I would be very much grateful.

Kind Regards,
Jordan

Here is the code I have so far…

[code]

local physics = require(“physics”)
physics.start()

–> Hide the status bar
display.setStatusBar( display.HiddenStatusBar )

–> Display background image
local background = display.newImage( “background.png” )

–> Display button_a image and position
local button_a = display.newImage( “button_a.png” )
button_a.x = 100; button_a.y = 850

–> Display button_b image and position
local button_b = display.newImage( “button_b.png” )
button_b.x = 540; button_b.y = 850

–> Display bullet image and position
local bullet = display.newImage( “bullet.png” )
bullet.x = display.contentWidth / 2
bullet.y = 500

–> Display spaceship image and position
local spaceship = display.newImage( “spaceship.png” )
spaceship.x = 330; spaceship.y = 700
physics.addBody( spaceship, “static”, { density=1.0 } )

–> Add a new function using :touch (event) to move the
– spaceship .x position -20 when the “button_a” is pressed
function button_a:touch (event)
spaceship.x = (spaceship.x)-15
end

–> Add event listener (touch) to the “button_a” to link to the
– ‘function’ we just made above
button_a:addEventListener( “touch”, button_a )

–> Add a new function using :touch (event) to the move the
– spaceship .x position + 10 when the “button_b” is pressed
function button_b:touch (event)
spaceship.x = (spaceship.x)+15
end

–> Add event listener (touch) to the “button_b” to link to the
– ‘function’ we just made above
button_b:addEventListener( “touch”, button_b )

–> Display UFO image and position
local UFO = display.newImage( “UFO.png” )
UFO.x = 315

–> Display shoot button image and position
local button_fire = display.newImage( “button_fire.png” )
button_fire.x = display.contentWidth / 2
button_fire.y = 850

[code] [import]uid: 13946 topic_id: 5358 reply_id: 305358[/import]

Alright two part response:

  1. In the button function set an event listener for “enterFrame” to move the bullet a little bit each frame. Something like
function button(event)  
 local bullet = display.newImage("bullet.png")  
 bullet.x = spaceship.x  
 bullet.y = spaceship.y  
 bullet:addEventListener("enterFrame", moveBullet)  
 local function moveBullet(event)  
 event.target.x = event.target.x + 2  
 end  
end  
  1. Don’t post the same question repeatedly in multiple forums. That is very rude. [import]uid: 12108 topic_id: 5358 reply_id: 17876[/import]
  1. Thank you thats just what i was looking for, will try that out tomorrow now and let you know how it goes.

  2. Yeah sorry about that, I did try to delete the other post but no luck. Sorry if that came across me being rude, didn’t mean that in any way.

Kind Regards :stuck_out_tongue: [import]uid: 13946 topic_id: 5358 reply_id: 17880[/import]

Thanks for that but where about should I put that piece of code?
And also is there anything I need to put in like a listener for shoot button (button_fire)? [import]uid: 13946 topic_id: 5358 reply_id: 17987[/import]

When I said “in the button function” I was referring to the event listener. So like where you have functions like button_b:touch but obviously with a new shoot button. [import]uid: 12108 topic_id: 5358 reply_id: 17988[/import]