2016-07-16 172 views
3

我在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(); 
    } 


} 

}

+0

是因为我创建了僵尸类中的玩家类的参考。但是我已经在我的游戏课中完成了这项工作,我将该玩家添加到了游戏中? – Phill

回答

2

这是因为你的敌人类有一个选手对象与你游戏类有一个完全不同的玩家对象。所以你的玩家在敌人阶级的位置永远是一样的。因此,在游戏课中进行敌人AI运动会更合乎逻辑。你的玩家或敌人职业应该只是持有角色的位置,方向或加载图像,不管它是敌人还是玩家。

我建议为敌人和玩家分类创建父类,因为它们都具有相似的特征。

+0

希望这有助于! – Sak6lab