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

2013/3/26

Need help with creating a rotating turret.

Griffon305 Griffon305

2013/3/26

#
Hello! I am trying to create a turret for an asteroid game that can rotate 180 degrees. I figured out how to make it rotate, but need to come up with a statement that will stop the turret from rotating over 90 degrees in either direction, any suggestions?
JetLennit JetLennit

2013/3/26

#
could you supply the code you have made so far?
Griffon305 Griffon305

2013/3/26

#
I recently started learning greenfoot, so it is pretty basic: public void act() { checkKeyPress(); } /** * This will check to see if a key has been pressed. If the left arrow is pressed, the turret * will rotate counterclockwise. If the right arrow is pressed, the turret will turn clockwise. */ public void checkKeyPress() { if (Greenfoot.isKeyDown("left")) { turn(-5); } if (Greenfoot.isKeyDown("right")) { turn(5); } }//end method And then this is what I thought I could start to work with for creating the limit: public int getRotation()
danpost danpost

2013/3/26

#
You just need a double check for each out of bounds range:
1
2
3
int rot = getRotation();
if (rot < 180 && rot > 90) rot = 90;
if (rot > 180 && rot < 270) rot = 270;
Apply this code immediately after any change in the rotation of the turret.
Griffon305 Griffon305

2013/3/26

#
Thank you!
JetLennit JetLennit

2013/3/26

#
I am sorry that i arrived late... i am glad danpost helped you
danpost danpost

2013/3/26

#
There is another way to handle it which would make the code a bit easier. Rotate the image of the turret 180 degrees and set the initial rotation of the actor to 180 degrees. Then your 'checkKeyPress' method could be:
1
2
3
4
5
public void checkKeyPress()
{
    if (Greenfoot.isKeyDown("left") && getRotation()>90) turn(-5);
    if (Greenfoot.isKeyDown("right") && getRotation()< 270) turn(5);
}
You may have to set the rotation of any bullets from the turret to 180 degrees more than you have it now.
Griffon305 Griffon305

2013/3/26

#
Awesome! For the level I am at, the second approach works much better for me, thanks again!
JetLennit JetLennit

2013/3/26

#
are you going to post the game on the greenfoot website?
Griffon305 Griffon305

2013/3/26

#
I suppose, but its nowhere near finished, probably not for another month or so, I don't get much time to work on it during the day. By the way, danpost, when I set the initial rotation of the actor to 180 degrees, I can set that up in the constructor, correct?
danpost danpost

2013/3/26

#
Yes. Either 'turn(180);' or 'setRotation(180);' can be used in the constructor. You may want to read the following before continuing. As a third alternative, you could save the rotation of the turret in an instance field. This would, however, result in a two step process in the 'if' blocks as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// add instance field
private int rotation = 0;
// the method
public void checkKeyPress()
{
    if (Greenfoot.isKeyDown("left") && rotation >-90)
    {
        turn(-5);
        rotation -= 5;
    }
    if (Greenfoot.isKeyDown("right") && rotation < 90)
    {
        turn(5);
        rotation += 5;
    }
}
With this, there is no initial rotating of the image or the actor; and the rotation of the bullet can remain as is. In fact, you can initially set 'rotation' to any value (adjusting the '90' and '-90' values by adding the initial value of 'rotation'). Setting it to the value of the initial rotation a bullet would travel would be an added plus; as you can then just set the rotation of any new bullet to the current value of 'rotation', as any adjustment would already be made. Hope I am not confusing you.
Griffon305 Griffon305

2013/3/27

#
I get most of what you're saying, I like this third option, while I am glad I can understand most of what is going on, I am bummed I can't figure it out for myself. The only part I am not sure about is the following code (the bolded part): turn(5) rotation += 5; Is that line of code suppose to be a mutator for the instance field? If not, what's its purpose? Also, do you have an email I can contact you at? If it won't be a bother for your, I will have more questions as I work on this. Thanks again for your help!
JetLennit JetLennit

2013/3/27

#
@Griffon305 1. that sets rotation to be it plus five. 2. you could start a new forum discussion
danpost danpost

2013/3/27

#
The statement 'rotation += 5;' is the same thing as 'rotation = rotation + 5;. Since we are turning the actor plus five degrees, we want our rotation field to adjust by that same amount so we know the new bullet rotation value.
You need to login to post a reply.