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: 5359 reply_id: 305359[/import]

Have you checked out the bullet sample in the Samples directory bundled with Corona? [import]uid: 11393 topic_id: 5359 reply_id: 17885[/import]

Yeah i have looked at the bullet sample, i re-created it to get a better understanding.
But i just cant figure out how to get to spawn from the centre of the spaceship.

[import]uid: 13946 topic_id: 5359 reply_id: 17918[/import]

If you need to place an object relative to other objects, you need to use setreferencepoint : http://developer.anscamobile.com/reference/index/objectsetreferencepoint

Is this what you need?

[lua]ship:setreferencepoint( display.CenterReferencePoint ) – Set coordinate handle to centre of ship
bullet:setreferencepoint( display.CenterReferencePoint ) – Set coordinate handle to centre of bullet
bullet.x = ship.x – Use ship coordinates
bullet.y = ship.y[/lua]

That will set the bullets starting position to be exactly dead centre of the ship - inside the ship in fact.

If you know which direction you were firing you could also use left/right/bottom/top setreferencepoint for the bullet to take into account it’s width/height.
[import]uid: 11393 topic_id: 5359 reply_id: 17921[/import]

Thanks mate :smiley:

One more thing, I still cant figure out to shoot the bullet up the .y axis when the “button_fire” is touched.
Any idea? Is it something to do with move the bullet over time or frame?

Thanks for your help,
best. [import]uid: 13946 topic_id: 5359 reply_id: 18114[/import]

In order for something to move on screen it requires either transition or an event update (like enterFrame).

For example: transition.to( bullet, {time=1000, y=100} ) will move an image object called bullet to coordinate y=100 in 1 second. You call this function once - fire and forget.

Alternatively, if you had an event that was firing to update bullet position you would write inside the event function bullet.y = bullet.y + 10. This would keep pushing bullet up by 10 pixels every time the event function is called.

[EDIT: There are a couple of “games” classes in the Code Exchange section which have lots of predefined functions useful for games. Your best bet is to start there so you won’t be writing these functions and a framework from scratch] [import]uid: 11393 topic_id: 5359 reply_id: 18115[/import]