0

So, I have been following tutorials on a Slick API 2D Game Java tutorial, and I got the basics of how to use the API. But then, when I was playing around and trying to make a game. I tried to implement a bullet/firing system into my 2D space shooter game. And I can't seem to find a way to do it!

I've tried looking around on Google and YouTube, but it's not helping at all...! All my game does right now is move a ship right to left. I want to be able to make it so that the a bullet-like object is fired every time the space bar is pressed. I am just not sure how to go about it... I'm hoping someone can explain it simply to a new programmer !

tckmn
  • 57,719
  • 27
  • 114
  • 156
user1975231
  • 45
  • 1
  • 8
  • 1
    Although there will probably be someone that can answer your question (I haven't worked with engines), I recommend getting a grasp on Java first - via some [tutorials](http://docs.oracle.com/javase/tutorial/). – A--C Jan 14 '13 at 02:04
  • 1
    While looking to other's for help can be useful for you, it would be better to learn how to do it yourself so you can understand the concepts fully. – syb0rg Jan 14 '13 at 02:06

2 Answers2

5

I don't know much about Slick, but the idea behind a "bullet system" isn't that bad.

Unless you want instant hits when they fire, which it doesn't seem you do, the general idea behind what you need to do goes like this.

First, listen for a space bar press. When this happens, create a new "bullet" object.

Give this object a movement direction and speed, and then gradually move it in that direction.

While moving it, also detect if it crosses path with an enemy. If it does, then remove the bullet and kill the enemy.

That's a very basic idea of what you should be looking to do.

AlienHoboken
  • 2,750
  • 20
  • 23
  • Wouldn't this only fire one bullet at a time? Or am I wrong? I'm not too sure as I have seen, the ArrayList used in others "firing systems" before... So, I guess what I'm asking is would creating a new Bullet object, allow for several bullets to be fired at a time? – user1975231 Jan 14 '13 at 02:13
  • Well, 'as long as Space bar is pressed, create Bullet' then you can control the interval of each bullet, and limit them (munition) – Jeremy Dicaire Jan 14 '13 at 02:13
  • Like Jeremy said, you can detect if the space bar is continuously being held and keep generating bullets as it is. Also, each time the space bar is pressed, generate a new bullet. You can use an arraylist, or any other container, to organize them so that you can do operations like movement and hit detection on them en masse. – AlienHoboken Jan 14 '13 at 02:34
1

Assuming you're using polling for input, you'll need to add a check to your update method for the spacebar. If the spacebar is pressed, then add a new instance of Bullet to an array of bullets and pass the initial x, y, and velocity in the constructor.

Your Bullet class may look something like:

public class Bullet
{
    public static float VELOCITY;
    private Vector2f position;

    public Bullet(float x, float y, float velocity)
    {
        position = new Vector2f(x, y);

        VELOCITY = velocity;
    }

    public void update(float delta, boolean vertical)
    {
        if(vertical)
        {
            y += VELOCITY * delta;
        }
        else
        {
            x += VELOCITY * delta;
        }
    }
}

You'll also want to call the update method for the bulelts in your update method. Do this with something like:

for(Bullet bullet : bullets)
{
    bullet.update(delta, true);
}
Zach Latta
  • 3,251
  • 5
  • 26
  • 40