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

2016/3/10

How to make a "bullet" fire in a certain direction

Sacramor Sacramor

2016/3/10

#
Imagine a "Binding of Isaac" type of game. My character moves and changes images based on direction. How do I make bullets fire in the direction he is facing? I know how to create the bullet itself, but the only way I can think to make each bullet fire in direction independent of eachother would be to make 4 different bullet classes, each moving in a different direction. Could someone please recommend a more efficient way of doing this?
danpost danpost

2016/3/10

#
Set the direction of the bullet when you create it in the class of the character:
Bullet bullet = new Bullet(); // creates the bullet object
bullet.setRotation(getRotation()); // sets the direction of the bullet
getWorld().addObject(bullet, getX(), getY()); // adds the bullet into the world
bullet.move(20); // initial adjustment of bullet location
The last line is not absolutely necessary; I find that often, when one actor creates another one, the spawned actor is not always placed at the same location as the one that spawned it. Moving the bullet a bit gets it to start slightly off the center of the character. You can even turn 90 degrees move some, then turn back to adjust along the other axis.
You need to login to post a reply.