java - 我的僵尸不会找到自己。

标签 java android imageview runnable

这听起来很哲学不是吗?

无论如何,我有一个相当复杂的问题。

我的 main_activity 类像这样收集所有的僵尸:

//Testing Runnable (used to compare the first zombie with the player)
private Runnable updateLocations = new Runnable(){
    @Override
    public void run(){
        try {
            while(true) {
                image_player.getLocationInWindow(pLoc);
                Zombie zoms = zombieCollection.next();
                if(!zoms.equals(null)){
                    zoms.getZombieImage().getLocationInWindow(zLoc);
                }
                System.out.println("Zombie: x = " + zLoc[0] + "; y = " + zLoc[1]);
                System.out.println("Player: x = " + pLoc[0] + "; y = " + pLoc[1]);
                Thread.sleep(500);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
};

我的僵尸类像这样收集信息:

public class Zombie{

float X, Y;
int Width, Height;
Direction fdirc;
ImageView zImage;
Player player;

boolean dead;
int[] zLoc;


public Zombie(ImageView zImage, Player player){
    zLoc = new int[2];
    zImage.getLocationOnScreen(zLoc);

    this.zImage = zImage;
    this.X = zLoc[0];
    this.Y = zLoc[1];
    this.Width = zImage.getWidth();
    this.Height = zImage.getHeight();
    this.fdirc = Direction.EAST;
    this.player = player;
    this.dead = false;

    Thread thread = new Thread(this.startZombieChase);
    thread.start();
}

public ImageView getZombieImage(){
    return zImage;
}
private Runnable startZombieChase = new Runnable() {
    @Override
    public void run() {
        try {
            while(!dead) {
                moveTowardsPlayer();

                Thread.sleep(10);
                updateZombie.sendEmptyMessage(0);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
};
private Handler updateZombie = new Handler(Looper.getMainLooper()) {
    public void handleMessage(android.os.Message msg) {

        /** Because the zombie should always be on top! **/
        zImage.getLocationOnScreen(zLoc);
        zImage.bringToFront();
        zImage.setX(X);
        zImage.setY(Y);

    }
};

private void moveTowardsPlayer(){
    int player_x = player.getPosition()[0];
    int player_y = player.getPosition()[1];

    l("Where is it in zombie class : player - " + player_x + " " + player_y + "zombie  - " + X + " " + Y);

    float compareX = player_x - (int)X;
    float compareY = player_y - (int)Y;



    // Y is closer, so we're moving horizontally.
    if(Math.abs(compareX) < Math.abs(compareY)){
        //Moving North
        if(player_y > Y){
            Y+=1;
        }
        //Moving South
        else if(player_y < Y){
            Y-=1;
        }
    }
    // X is closer, so we're moving vertically.
    else{
        //Moving East
        if(player_x > X){
            X+=1;
        }
        //Moving West
        else if(player_x < X){
            X-=1;
        }

    }
}
public void l(Object string){
    System.out.println("Log - " + string);
}
}

我遇到的问题是它会相对于一个数字移动(所以,它确实相对于某物移动)但是,这不是正确的事情。

a busy cat

logcat 告诉我这个:

  • 它在僵尸类中的什么位置:玩家 - 750 451 僵尸 - 750 451
  • 它在 main_activity 中的位置:player - 750 451 zombie - 792 619

谁能帮助我理解我做错了什么? 整个项目位于here .

最佳答案

离开 Brainz 的僵尸一定是生病的僵尸。我们不能那样做,现在可以吗?

要拥有一个将僵尸移动到非僵尸的函数,您可以使用一个函数,但该函数使用的变量不是参数,因此很难找出它们的来源。我会选择这样的东西:(这有点冗长,但清楚地表明正在发生的事情)

/*
 * Function to update the position of the Zombie, aka walk to the Player.
 * @player_pos       - Where's the Brainz at?
 * @zombie_pos       - Where am I?
 * Might want to build it overloaded with an option for the speed.
 *
 * @return           - We return the new Zombie pos.
 */
private double [] moveTowardsPlayer(double [] player_pos, double [] zombie_pos) {
    // To make sure we don't override the old position, we copy values. (Java stuff)
    double [] player_pos_old = player_pos.clone();
    double [] zombie_pos_old = zombie_pos.clone();

    // Let's get the new X pos for the Zombie
    double left_or_right = player_pos_old[0] - zombie_pos_old[0]; // pos number is right, neg is left
    double zombie_pos_new_x;
    if (left_or_right > 0) { // Right
        zombie_pos_new_x = player_pos_old[0] + zombie_speed;
    } else { // Left - this way we make sure we are always getting nearer to the Brainz.
        zombie_pos_new_x = player_pos_old[0] - zombie_speed;
    }

     // TODO: do the same for the Y pos.

    // Bring it together
    double [] zombie_pos_new = {zombie_pos_new_x, zombie_pos_new_y};

    // One step closer to the Brainz!
    return zombie_pos_new;
}

然后像这样使用:

double [] zombie_pos = moveTowardsPlayer([2, 2], [5, 4]);
this.X = zombie_pos[0]; // I'd advice to keep these together as one var.
this.Y = zombie_pos[1]; // But it's your game.

然后找出僵尸何时获得 Brainz(或子弹)

关于java - 我的僵尸不会找到自己。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40959050/

相关文章:

java - 从列名包含空格的 SQLite 中读取数据

android - 如何在 linearLayout 中将 View 置于最前面

java - 如何检测(读取中的关闭输入)

Java-将字符串值转换为整数

java - 如何使用 poi 从 ms word(.doc) 读取格式化文本作为 html 文本?

java - 递归搜索Java中的目录

java - Android 中的 BitmapFactory null 问题

android - 初始化 Cordova 时出错 : Class not found

android - 为什么我的 ImageView 使用 scaleType=fitXY 没有填满整个屏幕?

android - ImageView 显示 PNG 但不显示 JPEG