我的角色是演员,当我向左或向右移动时,我给他一个动作等等。
LEFT
和UP
确实有效。我可以看到,因为我的debugmode向我显示了MapArray中char的当前位置。 (绘制rectangel的颜色)
如果我正在呼叫DOWN
和RIGHT
,它会走UP
或LEFT
,但我确信我的方向正确。如果我向右走,他确实走了,如果我点击它,他也走了!我希望你能告诉我他们为什么走错了方向!
这是迈克德的举动,我将动作设置为演员:
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);
}
这是一张如果我向右走,它的样子。他站在绿色的田野左边,现在是在正确的领域。但图片不存在。
这里是char的绘制:
@Override
public void draw(SpriteBatch batch, float parentAlpha) {
sprite.setPosition(this.getX(), this.getY());
sprite.draw(batch);
}
我真的不知道错误在哪里。希望你能帮忙!
答案 0 :(得分:1)
你忘了给RIGHT,UP和DOWN案例添加休息时间。
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;
}
}