3 - Bullets/firing system: Java 2D side Scrolling Game tutorial

Loading...

Sign in or sign up now!
Alert icon
Upgrade to the latest Flash Player for improved playback performance. Upgrade now or more info.
12,602
Loading...
Alert icon
Sign in or sign up now!
Alert icon

Uploaded by on Dec 24, 2010

This installment of the side scrolling game in Java shows you how to add bullets to your game, which allows the main character to fire in order to hit enemies (to be added next!). This is done through adding another class to the program.

Please let me know if you have any questions and I will be happy to answer them. The code for the video can be found below:

Frame class: http://pastebin.com/rnqMSELn
Board Class: http://pastebin.com/NpPZfPTX
Dude Class: http://pastebin.com/QVNa1aDb
Bullet Class: http://pastebin.com/zhdyasXA

The images can be found here:

bullet.png: http://img401.imageshack.us/i/bulletd.png/
still.png: http://img829.imageshack.us/i/still.png/
test.jpg (background): http://img40.imageshack.us/i/testcjc.jpg/

Thanks and good luck!

-MrJavaHelp

Category:

Education

Tags:

License:

Standard YouTube License

  • likes, 0 dislikes

Link to this comment:

Share to:

Uploader Comments (MrJavaHelp)

  • why is the arrayList static?

  • @liorlolobobo Just so that every other classes has access to it and it is the same array across all classes.

  • @MrJavaHelp can't you just make a method : Bullet[] getBullets() ?

  • @liorlolobobo You can!

    You can do a lot of things differently, but the way I did it seemed the most logical:

    - Creating an array of all bullets.

    - Adding to array as the fire() method is called (user presses space).

    - Checking constantly if any of the bullets in the array have a coordinate greater than 700 (out of screen) and if so, removing it from array (done in actionPerformed method).

    - Lastly, going through whole array and printing each bullet (in paint method).

    Thanks,

    MrJavaHelp

Top Comments

  • he bought a new mic thumbs up for that

  • if i leave space pressed infinite bullets appear :/ what can i do about it?

see all

All Comments (32)

Sign In or Sign Up now to post a comment!
  • @geronimoaris Use a timer with a thread class. and allow the space to only be effectively fired every 30-40 ms.

  • You leave a lot out in the videos, you include it in the source code but, it would be much more professional if you showed everything in the videos.

  • I have been playing around with this and I can't seem to figure out how to make the bullets able to shoot in each of the four directions.

  • @MrJavaHelp How to just draw a pixel??? or a rectangle?

  • Dude, this tutorial you made helped me out SO MUCH for my final project in my game development class! Thank you very much!

  • I dont really understand the purpose of the Array List, is there no other way to do it without the Array List?

  • Control + Shift + F To fix up your code and make it easier to read.

  • @MrSoluther 6:20 He takes about the bullets creation and he uses the character's height divided by 2, this is the figure you need to change.

  • @geronimoaris Add a counter which checks how many bullets are alive and have it increment when a new one is made and decrement when an old one is destroyed. Then have some code check if that counter is equal to the max and don't let it make new bullets if it is.

Loading...

Alert icon
0 / 00Unsaved Playlist Return to active list
    1. Your queue is empty. Add videos to your queue using this button:
      or sign in to load a different list.
    Loading...Loading...Saving...
    • Clear all videos from this list
    • Learn more