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

2016/3/4

Change position, but don't turn image

Bassusour Bassusour

2016/3/4

#
I have seen quite a few people with the same problem, but the solutions doesn't apply to me. That is, because i got the randomTurn(); method
1
2
3
4
5
6
7
public void randomTurn()
      {
        if(Greenfoot.getRandomNumber(100)<10)
         {
         turn(Greenfoot.getRandomNumber(90)-45);
        }
    }
So, how do I keep the image still, and not turn, when the instance does?
ElNo ElNo

2016/3/4

#
Hi, your question doesn't really explain what you want to achieve! But I guess you want to move your Actor across the world by randomly changing direction but without rotating the image? If that's your goal then I have this following code for you:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class NoTurn extends Actor
{
    private int myRotation = 0;
    private int stepWidth = 10;
 
    public void act()
    {
        randomTurn();
        moveStep();
    }   
 
    public void randomTurn()
    {
        if(Greenfoot.getRandomNumber(100)<10)
        {
            myRotation = myRotation + (Greenfoot.getRandomNumber(90)-45);
            myRotation = myRotation%360;
            if(myRotation < 0)
            {
                myRotation = 360 + myRotation;
            }         
        }
    }   
     
    public void moveStep()
    {
        int dx = (int)Math.round(Math.cos((double)myRotation)*stepWidth);
        int dy = (int)Math.round(Math.sin((double)myRotation)*stepWidth);
        setLocation(getX()+dx, getY()+dy);
    }
}
The actor will move more or less in the direction of myRotation. The acuracy depends on your cell-size (smaller ist better) and the stepWidth (bigger is better). If you only want give your Actor an rotation-attribut that changes without turning the image and that you can get from your actor with a getter-method (just like getRotation()), you can use the code from above, skip the method moveStep() and add this getter-method:
1
2
3
4
public int getMyRotation()
{
    return myRotation;
}
I hope something of this meets your intends.
Bassusour Bassusour

2016/3/5

#
Thanks for your reply, ElNo I honestly do not understand your code, and it also really, pardon my words, fucks up the movement of my class. And to make matters worse, the image still turns. But i do appreciate your reply
Super_Hippo Super_Hippo

2016/3/5

#
Try out this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
private int rotation = 0;
 
public void act()
{
    setRotation(rotation);
    //move, turn and do whatever
    setRotation(0);
}
 
public void turn(int rot)
{
    rotation = (rotation+rot+360)%360; //probably the +360 is not needed here
    setRotation(rotation);
}
 
//if you need it
public int getRotation()
{
    return rotation();
}
The idea is to save the rotation. At the beginning of the act method, the actor rotates to the saved rotation and in the end, it rotates back to 0°.
You need to login to post a reply.