所以我试图从pacman制作鬼(小指)。但是我对这个动作有问题。所以我使用getX()和getY()来确定pacman的位置,以及幽灵应该去哪里。
然而,当pacman是例如EAST并且鬼魂已经走向WEST时,问题就出现了。然后,当幽灵检测到pacman X或Y时,它会立即变为180.
这是代码,你不需要阅读所有内容,它是类似的。我试图涵盖每一个案例。
import ch.aplu.jgamegrid.*;
import java.awt.Color;
import java.util.*;
public class Ghost extends Actor
Set<Location.CompassDirection> random2SubDirs = new HashSet<Location.CompassDirection>();
Location pakLocation = pakman.wherePakman();
Location.CompassDirection dir = Location.EAST;
if (pakLocation.getX() > this.getLocation().getX()){
dir = Location.EAST;
}
else if (pakLocation.getX() < this.getLocation().getX()){
dir = Location.WEST;
}
else if (pakLocation.getY() > this.getLocation().getY()){
dir = Location.SOUTH;
}
else if (pakLocation.getY() < this.getLocation().getY()){
dir = Location.NORTH;
}
if (pakLocation.getX() == this.getLocation().getX()){
if((pakLocation.getY() > this.getLocation().getY())){
dir = Location.SOUTH;
else if((pakLocation.getY() < this.getLocation().getY())){
dir = Location.NORTH;
}
else if (pakLocation.getY() == this.getLocation().getY()){
if((pakLocation.getX() > this.getLocation().getX())){
dir = Location.EAST;
else if((pakLocation.getX() < this.getLocation().getX())){
dir = Location.WEST;
}
if (pakLocation.getY() > this.getLocation().getY() && pakLocation.getX() < this.getLocation().getX()){
//southwest
random2SubDirs.add(Location.SOUTH);
random2SubDirs.add(Location.WEST);
int n = randomNumberGen.nextInt(2);
System.out.println("southwest " + n);
switch(n){
case 0 : dir = Location.SOUTH;
case 1 : dir = Location.WEST;
}
no180Turn(dir);
}else if(pakLocation.getY() > this.getLocation().getY() && pakLocation.getX() > this.getLocation().getX()){
//southeast
random2SubDirs.add(Location.SOUTH);
random2SubDirs.add(Location.EAST);
int n = randomNumberGen.nextInt(2);
System.out.println("southeast " + n);
switch(n){
case 0 : dir = Location.SOUTH;
case 1 : dir = Location.EAST;
}
}else if(pakLocation.getY() < this.getLocation().getY() && pakLocation.getX() < this.getLocation().getX()){
//northwest
random2SubDirs.add(Location.NORTH);
random2SubDirs.add(Location.WEST);
int n = randomNumberGen.nextInt(2);
System.out.println("northwest " + n);
switch(n){
case 0 : dir = Location.NORTH;
case 1 : dir = Location.WEST;
}
}else if(pakLocation.getY() < this.getLocation().getY() && pakLocation.getX() > this.getLocation().getX()){
//northeast
random2SubDirs.add(Location.NORTH);
random2SubDirs.add(Location.EAST);
int n = randomNumberGen.nextInt(2);
System.out.println("northeast " + n);
switch(n){
case 0 : dir = Location.NORTH;
case 1 : dir = Location.EAST;
}
}
if (fleeing){
dir = oppositeDir(dir);
}
this.setDirection(dir);
if (canMove(dir)){
lastDir = dir;
move();
}else {randomMode = 10;
System.out.println("now it is random");
}
}
它是在IF还是我应该为此制作另一种方法?
非常感谢任何帮助。
答案 0 :(得分:0)
不是使用枚举的指示(我假设NORTH
,EAST
...是什么),为什么不只计算方向?你可以这样做:
int xDifference = pakLocation.getX() - this.getLocation.getX();
它为您提供沿着x轴从幽灵到pakman的距离,并包括指示方向的符号(在x坐标的情况下为东或西)。
然后,要仅获取方向,请使用signum
:
int xDirection = Math.signum(xDifference);
将返回1(向东),-1(向西)或0(不要改变横向位置)。然后,如果您需要调整速度,请将xDirection
乘以您希望幽灵移动的数量&#34; tick&#34;。然后只需复制上面的2行来处理y轴。
这无疑是完美的,但我认为它允许更容易调试。这不会直接解决您的问题,但应该使运动逻辑更简单,这可能有所帮助。
如果有人发现逻辑中存在任何问题,请发表评论,然后我会对其进行更新。