我的僵尸不会发现自己.

she*_*iHD 2 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();
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

我的僵尸类收集信息如下:

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);
}
}
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是它将相对于一个数字移动(因此,它确实相对于某些东西移动)但是,不是正确的事情.

一只忙碌的猫

logcat告诉我这个:

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

谁能帮我理解我做错了什么?整个项目都在这里.

ΦXo*_*a ツ 5

这里不正确:

if(!zoms.equals(null)){
Run Code Online (Sandbox Code Playgroud)

如果你试图检查zoms没有指向空引用,那么

if(zoms != null ){
Run Code Online (Sandbox Code Playgroud)

  • 即使它没有解决你的问题,也是错的,所以做什么ΦXocę웃Пepeúpaツ建议 (4认同)
  • @sheepiiHD也许欣赏人们试图免费帮助你的事实?:) (3认同)