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

2012/4/25

Getting the rotation of another actor

thefutureisnow thefutureisnow

2012/4/25

#
Hello world, I'm kind of new to Greenfoot, I started 6 weeks ago. I'm working on a personal project at the moment, but I encountered a problem. In my game there's a tank actor (tank1) and there's a bullet actor (bullet1), I need to make the bullet actor take the same rotation as that of the tank. Between the quotes is an example of something I've tried: " Tank1 tank1; setRotation(tank1.getRotation()); " When I compile it, it doesn't give me any errors, but when I start the program it gives me an error on the second line you see there. Please post a comment if you know how to make the bullet get the rotation of the tank. Thanks!
danpost danpost

2012/4/25

#
What exactly does the error message say? Where are you calling the Bullet constructor from and how are you calling it? Was the code you tried in the Bullet constructor?
ttamasu ttamasu

2012/4/25

#
So far all your code shows is an local variable tank1 of class Tank1 whose default value is null (or not created yet). When you use tank1.getRotation() you should get a null pointer error message. Normally when you "shoot" a bullet you create an instaance of the bullet in the Tank class .... something like a fire() method. private void fire() Bullet bullet1 = new Bullet() getWorld().addObject(bullet1, getX(),getY()); bullet1.setRotation(getRotation()); } or something like that
davmac davmac

2012/4/25

#
Your first mistake is not reading the error message (or at least, in thinking it's not important enough to post here). Those two lines, together, could never compile. I assume you've got more stuff in between them; Post some more. Show how you initialize 'tank1'. (By the way, when you want to post code, use the 'code' option).
thefutureisnow thefutureisnow

2012/4/25

#
Sorry I didn't give enough info, I did say I'm kinda new to this. The problem is fixed now, thanks ttamasu! But yes, you were all right, I should have put some more info. The error message was a null point error, I completely forgot about putting that in. Thanks anyways, it works now. Next time I'll remember to put more information in the discussion. Thanks!
davmac davmac

2012/4/25

#
Sorry I didn't give enough info, I did say I'm kinda new to this.
It's no problem, and the fact that you're new to this is why I gave you some advice about how to ask questions :) In general, the more information, the better. It shows that you're willing to put some effort in, and then people will be more willing to try and help you.
You need to login to post a reply.