我的角色是一个 Actor,当我将它向左或向右移动时,我会给他一个 moveTo Action 。
LEFT
和 UP
确实可以正常工作。我可以看到,因为我的 Debug模式显示了 MapArray 中字符的当前位置。 (绘制矩形的颜色)
如果我调用 DOWN
和 RIGHT
它确实会走 UP
或 LEFT
但我确信我是对的与方向。如果我向右点击,他会向上走,如果我向下点击,他也会向上走!
我希望你能告诉我为什么他们走错了方向!
这是我将 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 。但是没有图片。
这里是字符的绘制:
@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/