Bullets?

Hey, I’m working on my first game. It’s not meant to be anything big, just something to learn from.

I’ve currently got a box to move around the screen with WASD. I want to make it fire a bullet to the right whenever space is hit, but I’m extremely stuck.

I do have a BaseEntity class, which my Player class extends, if that helps.

If anyone needs to see any of my (very nooby) code, just ask :smiley:

Thanks

I would make a bullet class that automatically moves along the x-axis (or whatever direction you want it to go,) every time it’s update method is called. When space is pressed, simply create a new bullet object in front of the square, and it will shoot away from there. You might want to include a direction variable in the bullet’s constructor, so that the bullet know’s where to go. Let me know if I should explain anything in more detail. :slight_smile:

I am assuming this is a side scroller and your bullet will only need to go in one direction. What I would do is have a bullet class that, when updated, just moves in the direction you want. have your player class create this bullet class at its location and add it to an array of some kind. Something like this (pseudo code)

while game is running
update player, bullet, enemy positions
draw player, bullet, enemies
if space bar pressed
create bullet at player position
add bullet to array so it can be updated and drawn
run through all the bullets in array
check for collisions; if bullet has hit an enemy: do damage and null the bullet
if a bullet is off screen: null it

then in the bullets update method move the position, and have a way to flag when the bullet is off screen so you can null it

edit: awe I got ninja’d

Read this thread (it’s short): http://www.java-gaming.org/topics/me-and-my-noobish-brother-are-having-problems-with-scanners/25951/view.html

I got ninja’d there, and from that point on, I was determined to master UprightPath’s short-ish post skill! Unfortunately, the quality wasn’t as good as I would have liked it to be, so your post still wins as best post. :slight_smile:

Thanks everyone, I’ve managed to make a bullet fire from the player, but I can only have one on the screen. When I press the space bar again, the other one disappears and goes infront of the player.

I’m currently desperate for tutorials and stuff, and I’ve found one that’s explained fairly well, but this one does stuff with the main class only. What do you “pros” use? Just one class, or multiple classes like I was using before?

you want to use multiple classes. How are you creating, updating, and drawing the bullet to the screen. That will help us answer you better.

Sounds like you have one bullet variable, and your resetting it whenever the player shoots. You need to keep a list of bullets. I’d use an ArrayList:

import java.util.ArrayList;

and in the class code:

ArrayList<Bullet> bullets;

and in the constructor:

bullets = new ArrayList<Bullet>();

Then, whenever the player presses space:

bullets.add(new Bullet());

and if you have a render function or something, you would have something like this in the render function:

for (int i=0; i < bullets.size(); i++) {
    bullets.get(i).render();
}

[quote=“Quentin,post:5,topic:38403”]
Most people use multiple classes. :slight_smile:

Or simply:


//update everything
for(Bullet b : bullets)
    b.update();


//draw everything
for(Bullet b : bullets)
    b.render();

I’ve learned something new and beautiful today!

Yep! If might as well take advantage of the fact that ArrayLists have iterators!

Since the only reference to each of your bullets is the array list, zngga is right. If you set an object to null, if there are no other variables tied to it, then it qualifies for garbage collection. It’s nice 'cause it removes it from memory making room for more bullets.