我已经为此奋斗了一段时间,我的菜鸟大脑无法完全解决它。我有一个标准的瓷砖地图,目前使用以下代码在地图上移动我的敌人精灵
-(void) movePlayer:(ccTime)deltaTime {
if (CGPointEqualToPoint(self.position, requestedPosition))
return;
float step = kPlayerSpeed * deltaTime;
float dist = ccpDistance(self.position, requestedPosition);
CGPoint vectorBetweenAB = ccpSub(self.position, requestedPosition);
if (dist <= step) {
self.position = requestedPosition;
[self popPosition];
} else {
CGPoint normVectorBetweenAB = ccpNormalize(vectorBetweenAB);
CGPoint movementVectorForThisFrame = ccpMult(normVectorBetweenAB, step);
if (abs(vectorBetweenAB.x) > abs(vectorBetweenAB.y)) {
if (vectorBetweenAB.x > 0) {
[self runAnimation:walkLeft];
} else {
[self runAnimation:walkRight];
}
} else {
if (vectorBetweenAB.y > 0) {
[self runAnimation:walkDown];
} else {
[self runAnimation:walkUp];
}
}
if (self.position.x > movementVectorForThisFrame.x) {
movementVectorForThisFrame.x = -movementVectorForThisFrame.x;
}
if (self.position.y > movementVectorForThisFrame.y) {
movementVectorForThisFrame.y = -movementVectorForThisFrame.y;
}
self.position = ccpAdd(self.position, movementVectorForThisFrame);
}
}
movePlayer: 由类 updateWithDeltaTime: 方法调用。ivar requestedPosition 也在 updateWithDeltaTime 方法中设置,它基本上从队列中获取下一个要移动到的点。这些点可以在地图上的任何位置,因此如果它们与敌人成对角线方向,则敌人精灵将直接移动到该点。但是我如何更改上面的代码以将运动限制为仅垂直和水平运动,以便敌人的运动沿着对角线路径“楼梯”,走曼哈顿距离(我认为它被称为)。如下图所示... S 是起点 F 是终点,数字是沿其路径的每个中间点,以创建楼梯式对角线运动。最后,我打算能够打开和关闭此行为,
| | | | | | | | | |
| | | | | | | | | |
| |F| | | | | | | |
| |5|4| | | | | | |
| | |3|2| | | | | |
| | | |1|S| | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |