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

2019/9/26

Prevent image automatically turning when turning

1
2
danpost danpost

2019/9/26

#
Try this for performMovement:
protected void performMovement()
{
    if (Greenfoot.isKeyDown(upKey)) {
        localRotation = UP;
        if (canMove()) {
            setLocation(getX(), getY()-1);
        }
    }
    if (Greenfoot.isKeyDown(leftKey)) {
        localRotation = LEFT;
        setRotation(localRotation);
        checkTurn();
        if (canMove()) {
            move(1);
        }
    }
    if (Greenfoot.isKeyDown(downKey)) {
        localRotation = DOWN;
        if (canMove()) {
            setLocation(getX(), getY()+1);
        }
    }
    if (Greenfoot.isKeyDown(rightKey)) {
        localRotation = RIGHT;
        setRotation(localRotation);
        checkTurn();
        if (canMove()) {
            move(1);
        }
    }
}
SushiLlama SushiLlama

2019/9/26

#
Thank you so much! Now everything except moving left works like a charm! When wlaking left with the Player he decides to flip horizontally on every move (First move: Upside down, second move: Normal, third move; Upside down, First move: normal). Since i may have been confused heres my code (hopefully for the last time :)...): protected void performMovement() { if (Greenfoot.isKeyDown(upKey)) { localRotation = UP; if (canMove()) { setLocation(getX(), getY()-1); } } if (Greenfoot.isKeyDown(leftKey)) { localRotation = LEFT; setRotation(localRotation); checkTurn(); if (canMove()) { move(1); } } if (Greenfoot.isKeyDown(downKey)) { localRotation = DOWN; if (canMove()) { setLocation(getX(), getY()+1); } } if (Greenfoot.isKeyDown(rightKey)) { localRotation = RIGHT; setRotation(localRotation); checkTurn(); if (canMove()) { move(1); } } } public void checkTurn() { if (getRotation() %180 == 0 && (getRotation() >= 180)) { getImage().mirrorVertically(); } } Thank You!!!
Super_Hippo Super_Hippo

2019/9/26

#
You’re missing the isTrueLeft part in the checkTurn-method. I mean right now the condition is the same as asking if the rotation is currently 180 (so looking to the left) instead of actually checking if it changed the direction.
SushiLlama SushiLlama

2019/9/26

#
Super_Hippo wrote...
You’re missing the isTrueLeft part in the checkTurn-method. I mean right now the condition is the same as asking if the rotation is currently 180 (so looking to the left) instead of actually checking if it changed the direction.
Hello Super_Hippo, i followed your instructions as good as i could but couldnt figure it out... Can you show me what i missed? protected void performMovement() { if (Greenfoot.isKeyDown(upKey)) { localRotation = UP; if (canMove()) { setLocation(getX(), getY()-1); } } if (Greenfoot.isKeyDown(leftKey)) { localRotation = LEFT; isTrueLeft = true; setRotation(localRotation); checkTurn(); if (canMove()) { move(1); } } if (Greenfoot.isKeyDown(downKey)) { localRotation = DOWN; if (canMove()) { setLocation(getX(), getY()+1); } } if (Greenfoot.isKeyDown(rightKey)) { localRotation = RIGHT; setRotation(localRotation); checkTurn(); if (canMove()) { move(1); } } } public void checkTurn() { if (getRotation()%180 == 0 && (getRotation() >= 180) != isTrueLeft) { getImage().mirrorVertically(); isTrueLeft = !isTrueLeft; } } If you could just adjust the code so that it finally works i would be very thankful!!! Nice to have such a lovely community!
danpost danpost

2019/9/27

#
Use this for checkTurn:
private void checkTurn()
{
    if ((getRotation == 180) != isTurnLeft)
    {
        getImage().mirrorVertically();
        isTurnLeft = !isTurnLeft;
    }
}
SushiLlama SushiLlama

2019/9/27

#
Hello, Im sure you meant getRotation() and isTrueLeft. Sadly this code gave simliar results to before, where the player would be upside down after the first, normal after the third, upside down again after the fifth, normal after seventh turn etc. Im so sorry to bother you, but do you have an idea whats still wrong? If getRotation == 180 (LEFT) it automatically is a left move. The left move always sets the isTrueLeft variable to true. The if clause is always false by that. :( I use isTrueLeft because the Player sometomes seemed like mixing up and left but i would suggest and prefer a solution without this variable. I really have no idea what to so:((( Kind Regards SushiLlama
danpost danpost

2019/9/27

#
You have:
isTrueLeft = true;
in your LEFT key detection block which shouldn't be there. The expression (getRotation() == 180) will only be true if the left key was last used between it and the right key. If that value is not equal to the last known state of being the last between the two, ... != isTrueLeft, then the image needs mirroring and the variable recording the last known state, isTrueLeft, needs changed. The following seems to work just fine:
import greenfoot.*;
import java.util.List;

public class Player extends Actor
{   protected static final int UP = 270;
    protected static final int RIGHT = 0;
    protected static final int DOWN = 90;    
    protected static final int LEFT = 180;
    
    boolean isTrueLeft;
    
    protected String upKey = "up";
    protected String leftKey = "left";
    protected String downKey = "down";
    protected String rightKey = "right";

    protected int localRotation;
    
    public void act() 
    {
        performMovement();
    }
    
    public boolean canMove() {
        return canMove(1);
    }
    
    public boolean canMove(int distance) {
        World myWorld = getWorld();
        int x = getNextX(distance);
        int y = getNextY(distance);

        if (x >= myWorld.getWidth() || y >= myWorld.getHeight() - 1) {
            return false;
        } else if (x < 0 || y < 0) {
            return false;
        }
        List<Rock> steine = myWorld.getObjectsAt(x, y, Rock.class);
        if (steine.isEmpty()) {
            return true;
        }
        return false;
    }
    
    public int getNextX(int distance) {
        int x = getX();
        switch (localRotation) {
            case RIGHT:
                x = x + distance;
                break;
            case LEFT:
                x = x - distance;
                break;
        }
        return x;
    }
    
    public int getNextY(int distance) {
        int y = getY();
        switch (localRotation) {
            case DOWN:
                y = y + distance;
                break;
            case UP:
                y = y - distance;
                break;
        }
        return y;
    }
    
     public void move(int steps){
        if (canMove(steps)){
            super.move(steps);
        }
    }
    
    public void performMovement(){
        if (Greenfoot.isKeyDown(upKey)) {
            localRotation = UP;
            if (canMove()){
                setLocation(getX(), getY()-1);
            }
        } else if (Greenfoot.isKeyDown(leftKey)) {
            localRotation = LEFT;
            setRotation(LEFT);
            checkTurn();
            if (canMove()){
                move(1);
            }
        } else if (Greenfoot.isKeyDown(downKey)) {
            localRotation = DOWN;
            if (canMove()){
                setLocation(getX(), getY()+1);
            }
        } else if (Greenfoot.isKeyDown(rightKey)) {
            localRotation = RIGHT;
            setRotation(RIGHT);
            checkTurn();
            if (canMove()){
                move(1);
            }
        }
    }
    
    public void checkTurn() {
        if ((getRotation() == 180) != isTrueLeft)
        {
            getImage().mirrorVertically();
            isTrueLeft = !isTrueLeft;
        }
    }
}
SushiLlama SushiLlama

2019/9/30

#
Thank you so much!!! Just a week and i already fell in love with this kind community! Solved
You need to login to post a reply.
1
2