hi all,
Having achieved (with great help) exploding enemy planes I have turned my attention toward enemy planes firing bullets at the player’s plane.
I figured doing a coin flip random number generator would do the trick (1 = shoot, 2 = don’t shoot).
I got this:
In case you are wondering, yes I printed the random number outcomes to the console and this is more or less 50/50. I tried a 1 out 5 and while some gaps did open up it really wasn’t much of an improvement .
I also got error messages:
ERROR:GAMESYS: Sprite could not be created since the buffer is full (128). Increase the ‘sprite.max_count’ value in game.project
ERROR:GAMESYS: Collision object could not be created since the buffer is full (128). Increase the ‘physics.max_collision_object_count’ value in game.project
These can obviously be fixed by changing project settings but that just means postponing the issue,
So obviously not a worthy idea.
So back to the virtual drawing board.
Should I go with a limited number of enemy bullets at time (like 6 or so total, seeing as the number of enemy planes on screen at any one time is seldom more than 3)?
What about allowing each enemy plane just one or two bullet object at a time?
What if the enemy plane only shoot when the player plane is lined up with it ? That means the player has to risk being shot at to shoot down an enemy.
What about giving the individual enemy bullets a time to live and only allowing a new bullet to be fired at the end of that?
Any advice or guidance is greatly appreciated.
Thank you all.
regards
Frank
1 Like
The basic mechanics for these kind of thing is a cool-down timer:
when the plane fire, you set a self.cooldown value.
Then in the update you decrease this value.
When it is <=0,
the plane is allowed to fire again => there you place your firing code mechanic.
You can also track a state “can_fire =true/false” if other mechanics need to know it.
Protip: when timer is detected <=0, you should set it to 0, to avoid the value to continue going down forever…
Now you can make the initial value of the timer as a go.property of your plane, and create planes that fires faster or slowlier.
2 Likes
First, may I commend you on your choice to make a 194X game - I’m making one too!
You can use Defold timers for this. You might have something like the following simple example which will fire 3 bullets 0.5 seconds apart. You can change those numbers to whatever suits:
local function fire(self)
-- create bullet here (factory.create)
end
function init(self)
local num_shots = 3
local delay = 0.5
for n = 1, num_shots do
timer.delay(n * delay, false, fire)
end
end
If you want the plane to keep shooting, you can simply do this instead:
local function fire(self)
-- create bullet here (factory.create)
end
function init(self)
timer.delay(0.5, true, fire)
end
3 Likes
And another thing is deleting (go.delete()) the bullets when they go off screen (go.get_world_position().y < 0 or you can have a collider at the bottom of your screen with logic that removes the bullets on collision) or after some time (timer.delay()). Otherwise the bullets will last indefinitely so you’ll eventually get these errors that you shared.
1 Like
Are you deleting the bullets after they hit the player or miss him and keep moving forward?
2 Likes
Hi all,
Thank you all very much for the input and advice.
I am deleting sprites as soon as they are comfortably off-screen.
Basically all the scripts for all sprites except the Player’s plane have an update function that looks like this:
`function update(self, dt)
local p = go.get_position()
p.y = p.y - speed * dt
if p.y < -20 then
go.delete()
end
go.set_position(p)
end
This is pretty much an exact cut-and-paste from the original Side Scroller tutorial. The 'y’s and 'x’s have been changed and the ‘speed *dt’ is added instead of subtracted to make bullets go up instead of down.
The “ERROR:GAMESYS:” errors are not because I wasn’t deleting sprites. They happened because I was creating sprites much, much faster than they could be gotten rid of by exiting the screen.
Just an FYI it takes a second or two to generate the ERROR:GAMESYS: messages when the limit is set to 128. With it set to 256 its more like five seconds, 1024 seems to stop the errors but at that point the frames-per-second seems to be dropping to a point where animation is no longer fast enough to be perceived as such.
Thank you all for your suggestions and support. I will work to implement the code you have provided and see what looks (and plays) best.
Regards
Frank
if it takes 2 secs to reach the sprite limit, it is possible that you are spawning multiple bullet on top of each other. Setting a flag like is_bullet_spawned and setting it false right after you have spawned a bullet would make sure you spawn one bullet at a time.
1 Like