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

2013/11/7

Direction of another object

payner2013 payner2013

2013/11/7

#
hi, ok, so i have a rocket that shoots bullets and i have got the bullets to fire from the rockets exact point in the world however, they all travel right at the speed of 10. how can i make them fire in the direction of the rocket? any help will be great. thank you in advance.
Zamoht Zamoht

2013/11/7

#
Put a direction in the constructor:
public Bullet(int rotation)
{
    setRotation(rotation);
}
Then when you add the bullet you should use the rotation of the rocket. So in the rocket class you can get it by using getRotation(). So if you add the bullet from the Rocket class you would create a new bullet using the following code: Bullet bullet = new Bullet(getRotation());
payner2013 payner2013

2013/11/7

#
excellent thank you very much :)
payner2013 payner2013

2013/11/7

#
the only problem is now, it wont fire the bullet...?
danpost danpost

2013/11/7

#
Create the bullet as follows:
Bullet bullet = new Bullet(); // create the bullet
bullet.setRotation(getRotation()); // set its rotation
getWorld().addObject(bullet, getX(), getY()); // add to world at rocket location
bullet.move((getWidth()-bullet.getWidth())/2); // move to front of rocket
payner2013 payner2013

2013/11/8

#
ahhh ok that works now thank you
You need to login to post a reply.