i making a game. i have a player and i want him to shoot different kind of bullets. i already have created my player class and bullet1 class. i am think of making bullet2 class, and bullet3 class.

than i can make a variable. main start with like this:

String bulletName="bullet1"

if player gets a item2 than


if player get item3 than


and than i can test when bullet to print by

else if(...)

let me know if there is better way to do this.

5 Years
Discussion Span
Last Post by Schol-R-LEA

As pritaeas said, we need to know more about what you're planning on doing. However, as a general piece of advice, I would recommend defining an abstract parent class - let's call it AbstractBullet for now - and deriving the different sub-classes of Bullet from that. If you do that, you can simply override the methods of the parent class, eliminating the need to explicitly test the bullet classes. For example, say you have a class FullMetalBullet and another ExplosiveBullet, then in your AbstractBullet class you might have an abstract method called fireAt(), which defines the trajectory of the bullet, and another strikeTarget(), which defines what happens to whatever it hits. The two sub-classes would then implement their own versions of the two methods, to define what the specific types of bullets do.

This is just some general advice on basic OOP principles; just what you'll want to do will depend on how you want it to behave. You might want to look up the [url=https://www.google.com/search?q=factory+pattern]Factory pattern[/url] as well, since you're going to be dealing with a lot of bullets of different types.

Edited by Schol-R-LEA

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.