Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

i need some help with java. i am creating a game where i have a player and he shoot bullets. u can also change u types of bullet. bullets are just fillOval. and change types of bullets is different colo.

i have created a class bullet. which just create one bullet. than if player hit space bar. than create a bullet and store in arraylist.

if(space)
{
    bulletObject = new Bullet();    //create bullet
    bulletStore.add(bulletObject);  //store in arraylist
}

now to change bullets. i have create a variable call bulletName. if it has a value of 1 than its bullet type is 1. If it has value of 2 than its bullet type is 2.

if user hit 'f' key than change bullet type.

if(f){
    bulletName ++;  //go to next bullet type

}

now to draw the bullets.

pubic void paint(Graphics g)
{
    if(bulletName == 1)
        g.setColor(Color.green); 
   if(bulletName == 2)
        g.setColor(Color.blue);
    if(bulletName == 3)
        g.setColor(Color.red);

     g.fillOval(x,y,width,height);    
}

The problem is that if user hit space bar than it will shoot one bullet. than if user hit 'f' key than it will change color of all bullets on screen.

i want it so that it doesnt change color of bullets on screen. it only change color of bullet that are about to get shoot.change

share|improve this question
add comment

1 Answer 1

I think this is a problem of you thinking in the wrong abstraction level.

Your bulletName variable is in global space (accessible to all methods), right? If it's shared among all Bullets then what else would you expect but it to change all of the Bullets colors?

Objects are nouns (they are things). If you already have a noun (Bullet), why not also give it a noun to describe its representation (a property color) and possibly a verb to change it (a method setColor())?

That way, when you fire a bullet, you get to tell it what color it is then and there - it will be set for that object.

Let's make some pseudocode:

In Bullet, add a property color:

class Bullet {
    private int color;
    public void setColor(int color) {
        this.color = color;
    }
    public int getColor() {
        return this.color;
    }
}

Also retain your bulletName property but rename it to bulletColor. Makes more sense, right? Then to shoot:

if(space)
{
    bulletObject = new Bullet();    //create bullet
    bulletObject.setColor(bulletColor);
    bulletStore.add(bulletObject);  //store in arraylist
}

and to draw (paint is in Bullet, right? Then it has access to color):

pubic void paint(Graphics g)
{
    if(color == 1)
        g.setColor(Color.green);
    // ...
}
share|improve this answer
    
thanks alot darkf, every thing works. just one question. is it a good pratice to put this. in setter and getter methods? –  androidj Apr 9 '13 at 6:28
    
@androidj Yes, usually you will have private instance variables with getters and setters. This is useful to make sure other objects don't use yours the wrong way (and for example you could have setColor() make sure you don't input an invalid color.) –  darkf Apr 9 '13 at 10:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.