bemeyer
bemeyer

Reputation: 6221

Actions.moveTo moves into wrong direction

My character is an Actor and i give him a moveTo action when i move it to the left or right and so on.
The LEFT and UP does work right. I can see that because my debugmode shows me the current position of the char inside of the MapArray. (draws the color of the rectangel)
If i am calling the DOWNand RIGHT it does walk UP or LEFT but i am sure that i am right with the direction. If i klick right he does walk up and if i click down he also does walk up!
I hope you can tell me Why they are walking into the wrong direction!

Here is the move Methode where i sett the Actions to the Actor:

private void move(Status direction) {
    switch (direction) {
    case LEFT:
        this.clearActions();
        this.addAction(Actions.sequence(
                Actions.moveTo((getX() - Config.BLOCK_SIZE), getY(), speed),
                new RunnableAction() { //to know when hes done to get the next move
                    public void run() {
                        moveDone = true;
                    }
                }));
        break;
    case RIGHT:
        this.clearActions();
        this.addAction(Actions.sequence(
                Actions.moveTo((getX() + Config.BLOCK_SIZE), getY(), speed),
                new RunnableAction() {
                    public void run() {
                        moveDone = true;
                    }
                }));
    case DOWN:
        this.clearActions();
        this.addAction(Actions.sequence(
                Actions.moveTo(getX(), (getY() - Config.BLOCK_SIZE), speed),
                new RunnableAction() {
                    public void run() {
                        moveDone = true;
                    }
                }));
    case UP:
        this.clearActions();
        this.addAction(Actions.sequence(
                Actions.moveTo(getX(), (getY() + Config.BLOCK_SIZE), speed),
                new RunnableAction() {
                    public void run() {
                        moveDone = true;
                    }
                }));
    default:
        break;
    }
}


And here is how i call it. I hope its fine that i post a bit more "long" code! Its more or less just setting the right positions.

    @Override
        public void act(float delta) {
            super.act(delta);
            if (moveDone) {
                if (screen.pad.isTouched()) {
                    // check in which direction is the touchcontroller
                    if (screen.pad.getKnobPercentX() < 0
                            && Math.abs(screen.pad.getKnobPercentY()) < Math
                                    .abs(screen.pad.getKnobPercentX())) {
                        // checkt if the |x|>|y|
                        if (checkNextMove(Status.LEFT)) {
                            this.status = Status.LEFT;
                            move(Status.LEFT);
                            this.screen.map.mapArray[(int) (this.mapPos.x)][(int) this.mapPos.y] = Config.EMPTYPOSITION;
                            this.screen.map.mapArray[(int) (this.mapPos.x - 1)][(int) this.mapPos.y] = Config.CHARSTATE;
                            this.mapPos.x--;
                            moveDone = false;
                        }
                    } else if (screen.pad.getKnobPercentX() > 0
                            && Math.abs(screen.pad.getKnobPercentY()) < Math
                                    .abs(screen.pad.getKnobPercentX())) {

                        if (checkNextMove(Status.RIGHT)) {
                            move(Status.RIGHT);
                                                    this.status = Status.RIGHT;
                            this.screen.map.mapArray[(int) (this.mapPos.x)][(int) this.mapPos.y] = Config.EMPTYPOSITION;
                            this.screen.map.mapArray[(int) (this.mapPos.x + 1)][(int) this.mapPos.y] = Config.CHARSTATE;
                            this.mapPos.x++;
                            moveDone = false;
                        }
                    } else if (screen.pad.getKnobPercentY() > 0) {
                        if (checkNextMove(Status.DOWN)) {
                            this.status = Status.DOWN;
                            move(Status.DOWN);
                            this.screen.map.mapArray[(int) (this.mapPos.x)][(int) this.mapPos.y] = Config.EMPTYPOSITION;
                            this.screen.map.mapArray[(int) (this.mapPos.x)][(int) this.mapPos.y + 1] = Config.CHARSTATE;
                            this.mapPos.y++;
                            moveDone = false;
                        }
                    } else {
                        if (checkNextMove(Status.UP)) {
                            this.status = Status.UP;
                            move(Status.UP);
                            this.screen.map.mapArray[(int) (this.mapPos.x)][(int) this.mapPos.y] = Config.EMPTYPOSITION;
                            this.screen.map.mapArray[(int) (this.mapPos.x)][(int) this.mapPos.y - 1] = Config.CHARSTATE;
                            this.mapPos.y--;
                            moveDone = false;
                        }
                    }
                } else {
                    setIdle();
                }
            } 
            // methode from the absctract to change sprites
            updateSprite(delta);
        }

Here is a picture how it does look if i walk right. He stood left of the green field and now is in the right field. But the picture is not there.
right move

Here the draw of the char:

@Override
    public void draw(SpriteBatch batch, float parentAlpha) {
        sprite.setPosition(this.getX(), this.getY());
        sprite.draw(batch);
    }

i really dont see where the mistake is. hope you can Help!

Upvotes: 2

Views: 184

Answers (1)

Elior
Elior

Reputation: 3266

you forgot to add break to RIGHT, UP and DOWN cases..

private void move(Status direction) {
switch (direction) {
case LEFT:
    this.clearActions();
    this.addAction(Actions.sequence(
            Actions.moveTo((getX() - Config.BLOCK_SIZE), getY(), speed),
            new RunnableAction() { //to know when hes done to get the next move
                public void run() {
                    moveDone = true;
                }
            }));
    break;
case RIGHT:
    this.clearActions();
    this.addAction(Actions.sequence(
            Actions.moveTo((getX() + Config.BLOCK_SIZE), getY(), speed),
            new RunnableAction() {
                public void run() {
                    moveDone = true;
                }
            }));
     break;
case DOWN:
    this.clearActions();
    this.addAction(Actions.sequence(
            Actions.moveTo(getX(), (getY() - Config.BLOCK_SIZE), speed),
            new RunnableAction() {
                public void run() {
                    moveDone = true;
                }
            }));
     break;
case UP:
    this.clearActions();
    this.addAction(Actions.sequence(
            Actions.moveTo(getX(), (getY() + Config.BLOCK_SIZE), speed),
            new RunnableAction() {
                public void run() {
                    moveDone = true;
                }
            }));
     break;
default:
    break;
}

}

Upvotes: 1

Related Questions