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

2018/9/12

How can you let an object rotate continuously without distracting its movement?

Recorsi Recorsi

2018/9/12

#
is that even possible to do?
Super_Hippo Super_Hippo

2018/9/12

#
You can save both the rotation for the movement and the rotation for the continuous rotation in a variable. Then in your act method, you can rotate your object to the rotation for the movement, move and do everything it wants to do and in the end, you save your current rotation in the variable again and set the rotation to the variable for the continuous rotation (and increment it).
Recorsi Recorsi

2018/9/13

#
Super_Hippo wrote...
You can save both the rotation for the movement and the rotation for the continuous rotation in a variable. Then in your act method, you can rotate your object to the rotation for the movement, move and do everything it wants to do and in the end, you save your current rotation in the variable again and set the rotation to the variable for the continuous rotation (and increment it).
I can't really get this to work. The only thing it is doing is "turntowards" and "move". The rotation is always 0 and if I increase it, nothing happens. Is "getRotation" wrong?
TheGoldenProof TheGoldenProof

2018/9/13

#
You will either have to only rotate its image (which i don't remember if you can do) or use setLocation().
danpost danpost

2018/9/14

#
Hippo had the right idea. Save the movement rotation in a field; then save the tumble rotation at the beginning of the act in a variable and apply the movement rotation; then at the end of the act method, re-save the current movement rotation and apply the tumble rotation with additional tumbling:
/** field */
private int mRot = 0; // movement rotation

/** act method */
public void act()
{
    int tRot = getRotation();
    setRotation(mRot);
    // all normal act method (movement) stuff here
    mRot = getRotation();
    setRotation(tRot+10); tumbling
}
Recorsi Recorsi

2018/9/14

#
Thank you :) It rotates now, but doesn't turn towards the spaceship anymore. The turntowards method is in the addedtoworld method. I tried putting it next to the other movement stuff but then it constantly turns into the direction. This should only happen once. I hope it is understandable what i've meant. Here is the code:
protected void addedToWorld(World world)
    {
        Space space = (Space)getWorld();
        Spaceship spaceship = space.getSpaceship();
        if (spaceship.getWorld() == null) return;
        turnTowards(spaceship.getX(), spaceship.getY());
    }
    public void movement()
    {
        int tRot = getRotation(); //tumble rotation
        setRotation(mRot);
        move(6);
        mRot = getRotation();
        setRotation(tRot+10); //tumbling
    }
danpost danpost

2018/9/15

#
Add the following line at the end of the addedToWorld method:
mRot = getRotation();
You need to login to post a reply.