为什么有时坐标是相等的,有时它们不是?

时间:2018-03-23 10:04:04

标签: java android equals

我的游戏有问题。有时候玩家会碰到敌人他失去了HP。但有时候不是。我不知道为什么这样工作。敌人和玩家是坐标类对象。

//HP LOST
    if(player.equals(enemy1.getEnemy())|| player.equals(enemy2.getEnemy())|| player.equals(enemy3.getEnemy())|| player.equals(enemy4.getEnemy())|| player.equals(enemy5.getEnemy())){
        HP = HP-1;
        Toast.makeText(c,"HP -1",Toast.LENGTH_SHORT).show();
        if(HP==0){
            currentGameState = GameState.Lost;
        }
    }

我的坐标班:

 public class Coordinate {
    private int x;
    private int y;


    Coordinate(int x, int y) {
        this.x = x;
        this.y = y;
    }


    int getX() {
        return x;
    }

    void setX(int x) {
        this.x = x;
    }

    int getY() {
        return y;
    }

    void setY(int y) {
        this.y = y;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Coordinate that = (Coordinate) o;

        if (getX() == that.getX()) if (getY() == that.getY()) return true;
        return false;
    }
}

我在代码中找不到任何错误。请帮忙。敌人类将Coordinate对象作为变量。

敌人类:

class Enemy {
    private Random random = new Random();
    private String enemyOrient ="";
    private ArrayList<Coordinate> walls;
    private Coordinate enemy;

    Enemy(ArrayList<Coordinate> walls ){
        this.walls = walls;
        int randomWall = random.nextInt(walls.size()-1);
        this.enemy = new Coordinate(walls.get(randomWall).getX(),walls.get(randomWall).getY());
    }

    void enemysMechanic(){
        for(int i = 1; i< GameEngine.GameWidth; i++){
            if(enemy.equals(walls.get(i))){
                enemyOrient="UP";
            }
        }
        for(int i = GameEngine.GameWidth +1; i< GameEngine.GameWidth *2; i++){
            if(enemy.equals(walls.get(i))){
                enemyOrient="DOWN";
            }
        }
        for(int i = GameEngine.GameWidth *2+1; i< GameEngine.GameWidth *2+ GameEngine.GameHeight; i++){
            if(enemy.equals(walls.get(i))){
                enemyOrient="LEFT";
            }
        }
        for(int i = GameEngine.GameWidth *2+ GameEngine.GameHeight +1; i< GameEngine.GameWidth *2+ GameEngine.GameHeight + GameEngine.GameHeight; i++){
            if(enemy.equals(walls.get(i))){
                enemyOrient="RIGHT";
            }
        }
        switch (enemyOrient){
            case "UP":
                UpdateEnemy(0,1,enemy);
                if(enemy.getY()== GameEngine.GameHeight -1) changeEnemy();
                break;
            case "DOWN":
                UpdateEnemy(0,-1,enemy);
                if(enemy.getY()==1) changeEnemy();
                break;
            case "LEFT":
                UpdateEnemy(1,0,enemy);
                if(enemy.getX()== GameEngine.GameWidth -1) changeEnemy();
                break;
            case "RIGHT":
                UpdateEnemy(-1,0,enemy);
                if(enemy.getX()==1) changeEnemy();
                break;
        }
    }

    private void UpdateEnemy(int x, int y, Coordinate enemy){
        enemy.setX(enemy.getX() +x);
        enemy.setY(enemy.getY() +y);
    }

    Coordinate getEnemy() {
        return enemy;
    }

    private void changeEnemy(){
        int randomWall = random.nextInt(walls.size()-1);
        enemy = new Coordinate(walls.get(randomWall).getX(),walls.get(randomWall).getY());
    }
}

0 个答案:

没有答案