我在java中第一次創建遊戲,我目前正試圖讓我的殭屍跟蹤並跟隨玩家。下面是我的代碼。我創建了一個控制器類,並使用鏈表創建多個殭屍。然後在我的殭屍更新方法中,我使用簡單的if語句來爲殭屍追蹤玩家。問題在於殭屍會追蹤玩家的初始起始位置,而不是他移動的位置。在殭屍更新()方法中,我使用了System.println(player.getX());它表明玩家的位置並沒有在殭屍課上更新,所以這就是爲什麼他們只追蹤到它的起始位置。我無法弄清楚如何解決這個問題。任何幫助,將不勝感激。java:敵方玩家跟蹤AI的Java遊戲
ZOMBIE類:
public class Zombie extends Entity{
Animation dwn,up, left, right;
BufferedImage north, south, east, west;
Sprites sprites = new Sprites();
Player player = new Player(100,100);
Rectangle boundsZombie;
String direction = "";
public Zombie(int x, int y){
super(x,y);
sprites.create();
boundsZombie = new Rectangle(0,0, 32, 32);
}
public void update(){
if(player.getX() > x){
x = x + 1;
}
if(player.getX() < x){
x = x - 1;
}
if(player.getY() > y){
y = y + 1;
}
if(player.getY() < y){
y = y - 1;
}
System.out.println(player.getX());
}
public Rectangle getBounds(){
return new Rectangle((int) x, (int) y, 32, 32);
}
public void render(Graphics g){
g.drawImage(sprites.zombieNorth, x, y, null);
//g.setColor(Color.red);
//g.fillRect((int) x + boundsZombie.x, (int) y + boundsZombie.y, boundsZombie.width, boundsZombie.height);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
Player類
公共類播放器擴展實體{
Animation dwn,up, left, right;
BufferedImage north, south, east, west;
Sprites sprites = new Sprites();
KeyManager keyManager = new KeyManager();
Rectangle boundsPlayer;
//Controller c;
boolean movUp, movDwn, movRight, movLeft;
String direction = "";
public Player(int x, int y){
super(x,y);
sprites.create();
dwn = new Animation(100, sprites.player_down);
up = new Animation(100, sprites.player_up);
left = new Animation(100, sprites.player_left);
right = new Animation(100, sprites.player_right);
north = sprites.playerNorth;
south = sprites.playerSouth;
east = sprites.playerEast;
west = sprites.playerWest;
boundsPlayer = new Rectangle(0,0, 32, 32);
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public Rectangle getBounds(){
return new Rectangle((int) x, (int) y, 32, 32);
}
/**
* player tick method, used to move the player
*/
public void update(){
if(Game.getKeyManager().up){
y -= 3;
direction = "north";
}
if(Game.getKeyManager().down){
y += 3;
direction = "south";
}
if(Game.getKeyManager().left){
x -= 3;
direction = "west";
}
if(Game.getKeyManager().right){
x += 3;
direction = "east";
//System.out.println(x);
}
}
public int getX() {
return (int)x;
}
public int getY() {
return (int)y;
}
public String getDirection(){
return direction;
}
/**
* method used to return players facing direction sprite
* @return
*/
public BufferedImage getPlayerDirection(){
if(direction == "north"){
return north;
}
else if(direction == "south"){
return south;
}
else if(direction == "east"){
return east;
}
else{
return west;
}
}
public void render(Graphics g){
g.drawImage(getCurrentAnimationFrame(), (int) x, (int) y, null);
//g.setColor(Color.red);
//g.fillRect((int) x + boundsPlayer.x, (int) y + boundsPlayer.y, boundsPlayer.width, boundsPlayer.height);
}
/**
* method used to return the bufferedImage of current frame
* @return
*/
public BufferedImage getCurrentAnimationFrame(){
if(Game.getKeyManager().right == true){
return right.getCurrentFrame();
}
else if(Game.getKeyManager().left == true){
return left.getCurrentFrame();
}
else if(Game.getKeyManager().up == true){
return up.getCurrentFrame();
}
else if(Game.getKeyManager().down == true){
return dwn.getCurrentFrame();
}
else {
return getPlayerDirection();
}
}
}
是因爲我創建了殭屍類中的玩家類的參考。但是我已經在我的遊戲課中完成了這項工作,我將該玩家添加到了遊戲中? – Phill