#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    65
    Rep Power
    2

    Change bullets! help plz-java-


    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.

    Code:
    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.
    Code:
    if(f)
    {
        bulletName ++;  //go to next bullet type
    }
    now to draw the bullets.
    Code:
    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
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,614
    Rep Power
    1945
    How is Bullet created?
    For example, does each Bullet have its own paint function and bulletName variable?
    Where (which class) do you trigger/read the key events?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    65
    Rep Power
    2
    Originally Posted by MrFujin
    How is Bullet created?
    For example, does each Bullet have its own paint function and bulletName variable?
    Where (which class) do you trigger/read the key events?

    i have a class called "Bullet.java" this class just paint one bullet(oval).
    but the only way Bullet class get call if user hit 'space' key.

    in main i set up variables.
    Code:
    boolean space = false;
    when user hit space bar than
    Code:
    space = true;
    the trigger/read the key event is in main. that works fine.
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,614
    Rep Power
    1945
    If you change the colour, how does the existing bullet(s) know about the previous colour? to get this effect, each bullet have to know about their own colour, by saving the colour in the bullet class.
    Does the Bullet class have its own bulletname variable?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    65
    Rep Power
    2
    Originally Posted by MrFujin
    Does the Bullet class have its own bulletname variable?
    ahh thanks man. got it to working

IMN logo majestic logo threadwatch logo seochat tools logo