This site requires JavaScript, please enable it in your browser!
Greenfoot back
GrimCreeper312
GrimCreeper312 wrote ...

2013/3/23

Shooting Problem

GrimCreeper312 GrimCreeper312

2013/3/23

#
i have a torrent that fires bullets but when its fires the bullet goes to the side of the torrent. here is the code i used. Normal is the name of the bullet the torrent fires. rotation is a static instance variable of the Normal class
1
2
3
4
5
6
7
8
9
public void fire()
   {
       if(reloadDelayCount >= gunReloadTime) {
            
           getWorld().addObject(new Normal(), getX(), getY());
           Normal.rotation = getRotation();
            
       }
   }
danpost danpost

2013/3/23

#
If your default image of the object that fires the bullet is pointing up, you need to subtract 90 from the rotation for the bullet. However, you should not need a field to hold to rotation in the bullet class. Better would be to just set the rotation after creating the bullet as follows:
1
2
3
4
5
6
7
8
9
public void fire()
{
    if(reloadDelayCount >= gunReloadTime)
    {
        Normal normal = new Normal();
        getWorld().addObject(normal, getX(), getY());
        normal.setRotation(getRotation()-90);
    }
}
GrimCreeper312 GrimCreeper312

2013/3/23

#
thanks
GrimCreeper312 GrimCreeper312

2013/3/23

#
now when i fire it doesn't add the bullet at the end of the barrel but instead it adds it to the center of the turrent i have tried to change the getX() + 5) or getY() + 5) but it hasent worked do u have any ideas
danpost danpost

2013/3/23

#
There are (at least) two ways to resolve this. One is to 'move' the bullet the distance of the barrel after setting its rotation; the other is to 'set the paint order' so the bullet comes out from behind the turrent.
You need to login to post a reply.