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
SushiLlama SushiLlama

2019/9/26

#
Hello, I tried to make a control for my Player(s). I figured out, how to make turning from right to left or left to right look better by mirroring vertically. Sadly i just canĀ“t figure out how to let the Player look to the right (or front? Matter of perspective... Just the normal rotation when placed in World). This is why i decided to ask for help here. Player is abstract and has two Subclasses namely PlayerOne and PlayerTwo which extend from Player. Heres the Code: public abstract 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; boolean isTrueRight; protected String upKey; protected String leftKey; protected String downKey; protected String rightKey; protected int localRotation; protected int newLocalRotation; /** * Act - do whatever the Player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ 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 turnLeft() { switch (localRotation) { case DOWN: setImageRotation(RIGHT); break; case RIGHT: setImageRotation(UP); break; case UP: setImageRotation(LEFT); break; case LEFT: setImageRotation(DOWN); break; } } public void turnRight() { switch (localRotation) { case DOWN: setImageRotation(LEFT); break; case RIGHT: setImageRotation(DOWN); break; case UP: setImageRotation(RIGHT); break; case LEFT: setImageRotation(UP); break; } } public void setImageRotation(int richtung) { this.localRotation = richtung; switch (richtung) { case DOWN: setRotation(90); break; case RIGHT: setRotation(90); break; case UP: setRotation(-90); break; case LEFT: setRotation(180); break; default: break; } } public void move(int steps){ if(canMove(steps)){ super.move(steps); } } public void performMovement(){ if(Greenfoot.isKeyDown(upKey)) { if(canMove()){ newLocalRotation = UP; setLocation(getX(), getY()-1); localRotation = newLocalRotation; } } else if (Greenfoot.isKeyDown(leftKey)) { if(canMove()){ newLocalRotation = LEFT; checkTurn(); setRotation(LEFT); localRotation = newLocalRotation; move(1); } } else if (Greenfoot.isKeyDown(downKey)) { if(canMove()){ newLocalRotation = DOWN; setLocation(getX(), getY()+1); localRotation = newLocalRotation; } } else if (Greenfoot.isKeyDown(rightKey)) { if(canMove()){ newLocalRotation = RIGHT; isTrueRight = true; checkTurn(); setRotation(RIGHT); localRotation = newLocalRotation; move(1); } } System.out.println(getRotation()); } public void checkTurn() { if (localRotation == 0 && newLocalRotation == 180 && isTrueLeft|| localRotation == 180 && newLocalRotation == 0 && isTrueRight) { getImage().mirrorVertically(); } } } Thanks for any advice given!
danpost danpost

2019/9/26

#
First off, you should only need one boolean to indicate true turn (a boolean has two possible values and if it is not one, then it must be the other). I think you are looking for is something like this:
if ((localRotation > 180) != isTrueLeft)
{
    getImage().mirrorVertically()
    isTrueLeft = !isTrueLeft;
}
SushiLlama SushiLlama

2019/9/26

#
I dont really get what you mean - Sorry. I actually realized that the isTrue Variables See useless. Do you know how to prevent a rotating Image when rotating the Player? Could you maybe adjust performMovement() and checkTurn()?
SushiLlama SushiLlama

2019/9/26

#
isTrueRight/isTrueLeft Variables are useless*
SushiLlama SushiLlama

2019/9/26

#
Like where to put the lines of code?
danpost wrote...
First off, you should only need one boolean to indicate true turn (a boolean has two possible values and if it is not one, then it must be the other). I think you are looking for is something like this:
if ((localRotation > 180) != isTrueLeft)
{
    getImage().mirrorVertically()
    isTrueLeft = !isTrueLeft;
}
danpost danpost

2019/9/26

#
SushiLlama wrote...
I dont really get what you mean - Sorry.
Maybe I don't really know what you want. Please describe how you want your image to look in all four directions (orientation and facing). That is, which direction top of image is pointing and which direction the head is looking toward.
danpost danpost

2019/9/26

#
SushiLlama wrote...
Like where to put the lines of code? << Code Omitted >>
It was to replace code in checkTurn.
SushiLlama SushiLlama

2019/9/26

#
danpost wrote...
SushiLlama wrote...
I dont really get what you mean - Sorry.
Maybe I don't really know what you want. Please describe how you want your image to look in all four directions (orientation and facing). That is, which direction top of image is pointing and which direction the head is looking toward.
I want to have the Player always standing on his feet. (Now he sometimes still ends up being upside down). If he goes to the right - I want him to look to the right. If he goes to the left - I want him to look to the left. If he goes up or down he should not turn in any way and just keep the rotation he had (looking to the right when he walked to the right before the up/down move and looking to the left when he walked to the left before).
danpost danpost

2019/9/26

#
My code was supposed to use >=, not just >; but that does not matter with regards to what you actually want. Okay, maybe it does (I thought I was real close). Try this:
if (getRotation()%180 == 0 && (getRotation() >= 180) != isTrueLeft)
{
    getImage().mirrorVertically();
    isTrueLeft = !isTrueLeft;
}
SushiLlama SushiLlama

2019/9/26

#
danpost wrote...
My code was supposed to use >=, not just >; but that does not matter with regards to what you actually want. Okay, maybe it does (I thought I was real close). Try this:
if (getRotation()%180 == 0 && (getRotation() >= 180) != isTrueLeft)
{
    getImage().mirrorVertically();
    isTrueLeft = !isTrueLeft;
}
Youre a hero!!! Thanks!!! Edit(Somehow i managed to get upside down again by spamming keys for a while, two keys at the same time can result in this unwanted upside down turn) Heres my updated Code: public void performMovement(){ if(Greenfoot.isKeyDown(upKey)) { if(canMove()){ newLocalRotation = UP; setLocation(getX(), getY()-1); localRotation = newLocalRotation; } } else if (Greenfoot.isKeyDown(leftKey)) { if(canMove()){ newLocalRotation = LEFT; isTrueLeft = true; checkTurn(); setRotation(LEFT); localRotation = newLocalRotation; move(1); } } else if (Greenfoot.isKeyDown(downKey)) { if(canMove()){ newLocalRotation = DOWN; setLocation(getX(), getY()+1); localRotation = newLocalRotation; } } else if (Greenfoot.isKeyDown(rightKey)) { if(canMove()){ newLocalRotation = RIGHT; checkTurn(); setRotation(RIGHT); localRotation = newLocalRotation; move(1); } } System.out.println(getRotation()); } public void checkTurn() { if (getRotation()%180 == 0 && (getRotation() >= 180) != isTrueLeft) { getImage().mirrorVertically(); isTrueLeft = !isTrueLeft; } } Thanks!!!
danpost danpost

2019/9/26

#
You have 'else's between all your movement key detection blocks. I would think that would preclude dual key action. The individual code of these blocks may be an issue. I would think you need to setRotation before adjusting image in checkTurn in your left and right key check blocks.
danpost danpost

2019/9/26

#
I think I was closer still on the if statement. The following should work:
if ((getRotation() == 180) != isTrueLeft)
It was using localRotation instead of getRotation() that was the problem earlier.
SushiLlama SushiLlama

2019/9/26

#
danpost wrote...
You have 'else's between all your movement key detection blocks. I would think that would preclude dual key action. The individual code of these blocks may be an issue. I would think you need to setRotation before adjusting image in checkTurn in your left and right key check blocks.
That solved the up and down movement, bud sadly when i go left and then right / right and then left i now get the issue that i had with going up and down before... Im so sorry! I really apreciate your kind help!
SushiLlama SushiLlama

2019/9/26

#
danpost wrote...
I think I was closer still on the if statement. The following should work:
if ((getRotation() == 180) != isTrueLeft)
It was using localRotation instead of getRotation() that was the problem earlier.
Sadly i dont get where ive used localRotation in a wrong way. Anyway the new if statement changed nothing as far as i can tell. My Player now behaves weird when walking in x / -x direction (flips upside and back every few turns in the other direction).
SushiLlama SushiLlama

2019/9/26

#
With this code i managed to get a reliable error in some way. Now (starting looking to the right / +x axis) i walk to the right, and when i make my first turn left i turn upside down. Then it becomes a pattern: Every two turns from either l to r or r to l i flip upside down! Up and Down movement works flawlessly! public void performMovement(){ if(Greenfoot.isKeyDown(upKey)) { if(canMove()){ newLocalRotation = UP; setLocation(getX(), getY()-1); localRotation = newLocalRotation; } } else if (Greenfoot.isKeyDown(leftKey)) { if(canMove()){ newLocalRotation = LEFT; isTrueLeft = true; //setRotation(LEFT); checkTurn(); setRotation(LEFT); localRotation = newLocalRotation; move(1); } } else if (Greenfoot.isKeyDown(downKey)) { if(canMove()){ newLocalRotation = DOWN; setLocation(getX(), getY()+1); localRotation = newLocalRotation; } } else if (Greenfoot.isKeyDown(rightKey)) { if(canMove()){ newLocalRotation = RIGHT; //setRotation(RIGHT); checkTurn(); setRotation(RIGHT); localRotation = newLocalRotation; move(1); } } System.out.println(getRotation()); } public void checkTurn() { //if ((getRotation() == 180) != isTrueLeft) if (NewLocalRotation %180 == 0 && (NewLocalRotation >= 180) != isTrueLeft) { getImage().mirrorVertically(); isTrueLeft = !isTrueLeft; } }
There are more replies on the next page.
1
2