Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Bullets?  (Read 2578 times)
0 Members and 1 Guest are viewing this topic.
Offline Quentin

Senior Newbie




Big newb


« Posted 2012-03-09 02:35:06 »

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 Cheesy

Thanks
Offline RylandAlmanza

Junior Member


Medals: 3



« Reply #1 - Posted 2012-03-09 02:56:12 »

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. Smiley
Offline zngga
« Reply #2 - Posted 2012-03-09 03:02:59 »

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

My code never has bugs... it just develops unexpected features!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline RylandAlmanza

Junior Member


Medals: 3



« Reply #3 - Posted 2012-03-09 03:10:24 »

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. Smiley
Offline Quentin

Senior Newbie




Big newb


« Reply #4 - Posted 2012-03-09 03:15:43 »

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?
Offline zngga
« Reply #5 - Posted 2012-03-09 03:18:00 »

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.

My code never has bugs... it just develops unexpected features!
Offline RylandAlmanza

Junior Member


Medals: 3



« Reply #6 - Posted 2012-03-09 03:27:52 »

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.
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:
1  
import java.util.ArrayList;

and in the class code:
1  
ArrayList<Bullet> bullets;

and in the constructor:
1  
bullets = new ArrayList<Bullet>();

Then, whenever the player presses space:
1  
bullets.add(new Bullet());

and if you have a render function or something, you would have something like this in the render function:
1  
2  
3  
for (int i=0; i < bullets.size(); i++) {
    bullets.get(i).render();
}


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?
Most people use multiple classes. Smiley
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2012-03-09 03:46:31 »

Or simply:
1  
2  
3  
//update everything
for(Bullet b : bullets)
    b.update();


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

Offline RylandAlmanza

Junior Member


Medals: 3



« Reply #8 - Posted 2012-03-09 03:48:04 »

Or simply:
1  
2  
3  
//update everything
for(Bullet b : bullets)
    b.update();


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

I've learned something new and beautiful today!
Offline zngga
« Reply #9 - Posted 2012-03-09 03:50:08 »

Or simply:
1  
2  
3  
//update everything
for(Bullet b : bullets)
    b.update();


1  
2  
3  
//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!

My code never has bugs... it just develops unexpected features!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline evilfrenchguy

Junior Member


Medals: 3



« Reply #10 - Posted 2012-03-09 15:13:41 »

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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (15 views)
2014-08-01 22:53:16

CogWheelz (15 views)
2014-08-01 22:51:43

CopyableCougar4 (18 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

Resources for WIP games
by CogWheelz
2014-08-01 18:19:50

List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!