我在如何在我的2d平台上实现A *寻路时遇到了一些困难。
基本上我现在拥有的是玩家后面的敌人动画精灵。问题是当敌人与障碍物(平台)发生碰撞时,它将一直停留在那里直到玩家移动。 这是我现在的代码:
physicsWorld.registerPhysicsConnector(new PhysicsConnector(this, bodyEnemy, true, false) {
@Override
public void onUpdate(float pSecondsElapsed) {
super.onUpdate(pSecondsElapsed);
double dx = Player.bodyPlayer.getWorldCenter().x - bodyEnemy.getWorldCenter().x;
double dy = Player.bodyPlayer.getWorldCenter().y - bodyEnemy.getWorldCenter().y;
double length = Math.sqrt(dx * dx + dy * dy);
double accelerationX = dx / length;
double accelerationY = dy / length;
bodyEnemy.setTransform(new Vector2((bodyEnemy.getWorldCenter().x + ((float) accelerationX / 32)),
(bodyEnemy.getWorldCenter().y + ((float) accelerationY / 32))), 0);
float spd = selectedLevel * 0.1f;
if (spd >= 1.0f)
spd = 1.0f;
if (Player.bodyPlayer.getWorldCenter().x - bodyEnemy.getWorldCenter().x < 0) {
bodyEnemy.setLinearVelocity(new Vector2(bodyEnemy.getLinearVelocity().x, -spd));
} else {
bodyEnemy.setLinearVelocity(new Vector2(bodyEnemy.getLinearVelocity().x, spd));
}
if (Player.bodyPlayer.getWorldCenter().y - bodyEnemy.getWorldCenter().y < 0) {
bodyEnemy.setLinearVelocity(new Vector2(bodyEnemy.getLinearVelocity().y, -spd));
} else {
bodyEnemy.setLinearVelocity(new Vector2(bodyEnemy.getLinearVelocity().y, spd / 2));
}
}
});
如何让我的敌人找到绕过障碍物的方法,并继续追逐玩家?