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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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)

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.

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.

views:1611 update:2011/10/17 8:58:49
corona forums © 2003-2011