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

Report as inappropriate.

ledzep
ledzep presents ...

2012/12/23

Aero Shooting Planes (HELP)

How do I make my airplane shoot in the direction I'm facing? I used setRotation (from a tutorial on YT), but it only rotates the bullet.
I'm a complete begginer and I would really appreciate some help.

3432 views / 20 in the last 7 days

Tags: game with-source

open in greenfoot
Your browser does not support the canvas tag.
ManiacalPenguinManiacalPenguin

2012/12/24

the bullet should have a move method instead of a setLocation(), and use this as the shooting code: 1. Bullet b = new Bullet(); 2. getWorld().addObject(b , getX(), getY()); 3. b.setRotation(getRotation);
ledzepledzep

2012/12/24

Thanks a lot! The plane is shooting now but the bullet is traveling in the wrong direction - it shoots from the propeller and goes out trough the back. How do I invert that?
LeaderchickenLeaderchicken

2012/12/24

You actually use the setLocation() Method to move your "strel" so the Rotation of te Object has no effect. You should change your Strel act() to move(speed) instead of setRotation(....) then it will follow the right direction. You also should take a look abount some Methods like setRotation(getRotation) it will set the Rotation to the amount it already had.
LeaderchickenLeaderchicken

2012/12/24

with move(speed) it actually works but your code throws some Exceptions
LeaderchickenLeaderchicken

2012/12/24

For coding style it would be better if you have one class for your planes because they will actually do the same (you can set the image with the contructor) in your checkBoundaries() method you should do all the boundarie tests in one if() . In your code you get IllegalStateExceptions because the method doesnt stop after you removed the strel out of the world. If the strel isnt in the world getY() or getX() throw this.
LeaderchickenLeaderchicken

2012/12/24

Oh and dont forget Greenfoot standard is that setRotation(0) faces your plane to the right so the face off the image should be drawn towards the right(just use a programm like photophiltre to mirror the image).
MatheMagicianMatheMagician

2012/12/24

@leaderchicken, if you look above, ledzep has already implemented the move() method and he already is using the setrotation methods. @ledzep, to answer your question about changing the direction of the bullet, just set move(-speed); instead of move(speed). Also, about the errors mentioned by leaderchicked, you can easily fix them following these steps: first, remove the checkBoundaries(); method from your act() method so it looks like this: public void act() { move(-speed); setRotation(getRotation()); destroyEnemies(); } second, change your destroyEnemies() code to this: public void destroyEnemies() { //"Enemy" can be any class that you want the Strel to destroy. Actor enemy = getOneIntersectingObject(Avion2.class); if(enemy !=null) { getWorld().removeObject(enemy); getWorld().removeObject(this); } else checkBoundaries(); } what I have done here is added an else block so the computer asks itself "have i hit anything?" if not, then he checks to see if he is at the end of the world. Also, on some of the walls, your bullet is not removing itself, so change your checkboundaries() code to this: public void checkBoundaries() { if(getX() > getWorld().getWidth() - 2) { getWorld().removeObject(this); } else if(getX() < 1) { getWorld().removeObject(this); } else if(getY() > getWorld().getHeight() - 2) { getWorld().removeObject(this); } else if(getY() < 1) { getWorld().removeObject(this); } } This might seem like a lot, but really I have barely changed your code.
ledzepledzep

2012/12/27

This works great, except it returns an error when the bullet hits any of the sides. When it hits the top or bottom it disappears as it should. I don't know why it works for Height and not for Width. "java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed." Thank you!
ledzepledzep

2012/12/27

Just one more thing. How do I now make the other aeroplane shoot (Avion2)? I tried adding the same method streljaj(); as with the first aeroplane, but it doesnt work.
to fix it shooting out of the back, inplace of b.setRotation(getRotation); use this b.setRotation(getRotation + 180); now it should shoot forward.

See all comments

Want to leave a comment? You must first log in.

Who likes this?

No votes yet.