How to fire bullet from moving object and keep respawning, at object?

Hi, this is my first game. Okay, so I’m trying to shoot a bullet from my moving fighter(moves with left and right buttons I made) and have it keep respawning at fighters head no matter where he is on x axis with 1 sec between each spawn.
I have already figured out how to have the bullet stay on my fighters head and shoot up and made it to where if it hits a bouncing ball I put in there, they both disappear. My problem is I only have one shot and after I shoot and miss my fighter doesnt die anymore when the ball hits him. Oh yea and the bullet eventually drops back down. Is there a code that will make it go away after like 2 secs?
I know my code is probs very noob-ish so forgive. What I think there is a big problem with is the spawning codes I tried to put in after lines 120… I’ve just spent hours and hours researching and trying to figure out what I have done wrong. and thank you… a lot. sorry for making this so long and tedious.

[blockcode]

local physics = require(“physics”)
physics.start()
physics.setGravity(0, 9.8)

physics.setDrawMode(“hybrid”)

local background = display.newImage(“background2.jpg”)

–ball
local ball = display.newImage(“ball4.gif”)
ball.x = 75
ball.y = 100
physics.addBody(ball, { density = 0, friction = 0, bounce = .96, radius = 45})
ball.xScale = .3
ball.yScale = ball.xScale
ball.type = “ball”

–fighter
local fighter = display.newImage (“fighter.png”)
fighter.x = 400
fighter.y = 320
physics.addBody(fighter, { density = 1000000, friction = 100000})
fighter.isFixedRotation = true
local floor = display.newImage(“floor3.png”)
floor.y = display.contentHeight - floor.stageHeight/2
floor.x = 428
floor.xScale = 1.8
floor.yscale = 1.7

local rect2 = display.newRect(10, 30, 10, 2)
rect2.x = floor.x + -363
rect2.y = floor.y - 30
rect2:setFillColor(0, 0, 0)
physics.addBody(rect2, “static”, { density = 9999, bounce = 0, friction = 1})

local rect = display.newRect(100, 30, 3000, 2)
rect.x = floor.x + 300
rect.y = floor.y - 28
rect:setFillColor(0, 0, 0)
physics.addBody(rect, “static”, { bounce = 0, friction = 1})
–shoot button
local shoot = display.newImage (“shoot.png”)
shoot.x = 650
shoot.y = 452

–left button
local left = display.newImage (“left4.png”)
left.x = 90
left.y = 452

–right button
local right = display.newImage (“right2.png”)
right.x = 220
right.y = 452

local motionx = 0
local speed = 10

local function stop (event)
if event.phase ==“ended” then

motionx = 0
end
end

Runtime:addEventListener(“touch”, stop )

local function movefighter (event)
fighter.x = fighter.x + motionx
end

Runtime:addEventListener(“enterFrame”, movefighter)

function left:touch()
motionx = -speed
fighter.xScale = -1;
end
left:addEventListener(“touch”,left)

function right:touch()
motionx = speed
fighter.xScale = 1
end
right:addEventListener(“touch”,right)

–hides annoying status bar
display.setStatusBar( display.HiddenstatusBar )

–SPAWNING?
local bullet = display.newImage( “bullet.png” )
physics.addBody(bullet, {“static”, density = 990000, friction = 499000, radius = 6})
physics.setGravity(bullet, 0, -1)
bullet.isVisible = false
bullet.isBullet = true
bullet.x = fighter.x
bullet.y = fighter.y - 10

–SOMETHING WRONG here
local function spawnBullet()
local bullet = display.newImage( “bullet.png” )
physics.addBody(bullet, {“static”, density = 990000, friction = 499000, radius = 6})
physics.setGravity(bullet, 0, -1)
bullet.isVisible = false
bullet.isBullet = true
bullet.x = fighter.x
bullet.y = fighter.y - 10
end

local function movebullet (event)
bullet.x = bullet.x + motionx
end

function shoot:touch()
shoot.xScale= 2
shoot.yScale= 2
bullet.xScale= .7
bullet.yScale= 2
bullet.isVisible = true
transition.to(bullet, { time=1000, alpha=1.0, y=(-2000) } )
transition.to(bullet, { time=50, delay=1000, alpha=0} )
timer.performWithDelay( 1000, spawnBullet, 1 )

end
shoot:addEventListener(“touch”,shoot)
Runtime:addEventListener(“enterFrame”, movebullet)
–collision
function bullet:collision(e)
if(e.phase == “began”) then
if(e.other.type == “ball”) then

self:removeSelf()
self = nil

ball:removeSelf()
ball = nil

elseif(e.phase == “ended”) then

if(e.other.type == “ball”) then

—sound goes here

end

end
end
end

bullet:addEventListener(“collision”, bullet)

–collision2
function fighter:collision(f)
if(f.phase == “began”) then
if(f.other.type == “ball”) then

self:removeSelf()
self = nil

bullet.removeSelf()
bullet = nil

elseif(f.phase == “ended”) then

if(f.other.type == “ball”) then

—sound here

end

end
end
end

fighter:addEventListener(“collision”, fighter)
[/blockcode]
[import]uid: 92628 topic_id: 15671 reply_id: 315671[/import]

I included all the extra code because when I first started and I was looking through forums the long codes ppl posted helped me out, so hopefully this can help someone who is more of a beginner than me, if there is such that exists. [import]uid: 92628 topic_id: 15671 reply_id: 57834[/import]

Reply:

Why not try to have your bullet detection have a timer, and when it reaches a specific time, remove the object?

You could probably have this:

timer.performWithDelay(2000, removeBullet(bulletObj));

local function removeBullet(e)

e:selfRemove();

end

*Note that above is just speculation, I’m not sure if it will work, but you can give it a try. [import]uid: 33082 topic_id: 15671 reply_id: 57903[/import]