java - Actions.moveTo 移动到错误的方向

标签 java android libgdx

我的角色是一个 Actor,当我将它向左或向右移动时,我会给他一个 moveTo Action 。
LEFTUP 确实可以正常工作。我可以看到,因为我的 Debug模式显示了 MapArray 中字符的当前位置。 (绘制矩形的颜色)
如果我调用 DOWNRIGHT 它确实会走 UPLEFT 但我确信我是对的与方向。如果我向右点击,他会向上走,如果我向下点击,他也会向上走!
我希望你能告诉我为什么他们走错了方向!

这是我将 Actions 设置为 Actor 的 move Methode:

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;
    }
}


我是这样调用它的。我希望我发布更多“长”代码没问题!它或多或少只是设置了正确的位置。

    @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);
        }

这是一张图片,如果我向右走,它会是什么样子。他站在绿色 field 的左边,现在在右边的 field 。但是没有图片。
right move

这里是字符的绘制:

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

我真的看不出错在哪里。希望你能帮助!

最佳答案

你忘了给 RIGHT, UP 和 DOWN case 添加 break..

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;
}

关于java - Actions.moveTo 移动到错误的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15999787/

相关文章:

java - libGDX:如何正确地将屏幕加载到内存中?

java - Groovy - 如何在 Groovy 中压缩整个目录?

java - 计算数组中每个元素的出现次数,而不是按排序顺序,顺序将按照问题中的要求进行

java - LibGdx 物理体编辑器

android - 如何通过仅输入特定列的字符串值来删除 SQLite 数据库的整行?

java - 在Calendar Java中将字符串解析为TimeZone ICT

fonts - 带有 AssetManager 的 Libgdx FreeTypeFontGenerator

java - 如何将 POJO bean 绑定(bind)到公共(public)标题页

java - 从 DAO 类中的单个列表中的两个查询获取结果

android - 如何获得可用的 gsm 网络列表?