나는 처음으로 자바 게임을 만들고 현재 좀비를 추적하여 플레이어를 추적하려고합니다. 아래 코드는 내 코드입니다. 컨트롤러 클래스를 만들고 링크 된 목록을 사용하여 여러 좀비를 만들었습니다. 그런 다음 좀비가 플레이어를 추적하기 위해 간단한 if 문을 사용하여 좀비 업데이트 방법을 사용합니다. 문제는 좀비가 플레이어의 처음 시작 위치를 추적하지만 이동 위치가 아니라는 점입니다.자바 : 적 플레이어가 자바 게임을 AI로 추적
좀비 update() 메서드에서 나는 System.println (player.getX()); 그리고 그것은 플레이어 위치가 좀비 클래스에서 업데이트되지 않았 음을 보여주기 때문에 시작 위치로만 추적됩니다. 이 문제를 해결하는 방법을 알 수 없습니다. 어떤 도움을 주시면 감사하겠습니다.
좀비 등급 (class) :
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;
}
}
플레이어의 클래스
공용 클래스 플레이어는 엔티티 {
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