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

2016/5/14

fire bullet right and left hand

fandyrazzle fandyrazzle

2016/5/14

#
these are my actor -Actor Shooter -Actor Bullet as you can see, Shooter has 2 guns, left and right, and and it be able to turn 360 degree. my problem is, this far i only be able to make bullet fired 1 (from middle of actor ). So how do i fire 2 bullets in front of that 2 guns to wherever Shooter faces to?
xFabi xFabi

2016/5/14

#
1
setLocation(getX();+?,getY();+?)
replace ? with values that make it appear at the guns Edit: this would only work in one direction though (if it was 4 directions it'd be possible as well) Doing that on 360° turning will probably be super-complicated but dan might be able to help here..
SPower SPower

2016/5/14

#
You're going to have to use some trigonometry to fill in those question marks xFabi talked about. But, the sine and cosine functions in Java only take radians, not degrees, as input, so you'll have to convert it first:
1
2
int degrees = getRotation();
double radians = Math.toRadians(degrees);
You can then use Math.sin and Math.cos as needed. Also, as a note to xFabi, you shouldn't put ; between getX() and +something, that wouldn't compile.
danpost danpost

2016/5/14

#
No trigonometry is needed. All that is needed is a turn-move-turn-(move) combination. This should be done when the bullet is added into the world:
1
2
3
4
5
6
7
// example for left-hand gun bullet (coded in Shooter class)
Bullet bullet = new Bullet();
addObject(bullet, getX(), getY());
bullet.turn(-90);
bullet.move(30);
bullet.turn(90);
bullet.move(30);
Just switch the two 'turn' lines for right-hand gun bullets. Replace the '30's with values that work for the size of the image of you shooter. Both 'move' amounts do not have to be the same value.
SPower SPower

2016/5/14

#
I completely forgot you could achieve the same with Greenfoot's turn method. Sorry.
danpost danpost

2016/5/14

#
Oh, yeah. You did say your Shooter turns 360 degrees. You will need to insert the following line into the code at line 4 (before the 'turn-move-turn-move' combination):
1
bullet.setRotation(getRotation());
You need to login to post a reply.