0

在尝试计算接触点(dx、dy)之间的函数时,我在调用计算函数时收到错误消息。

这是我的移动功能:

public class Mice {

public Movement getRatio;
public static float MX;
public static float MY;

public Mice(float x, float y){
    MX = x;
    MY = y;
}

public void move(float dx, float dy){
    float[] ratio = new float[2];
    //below this comment is my error:
    ratio = getRatio.getPath(dx, dy, MX, MY);
    MX++;
    MY = MY + ratio[1];
    GameActivity.mpx = MX;
    GameActivity.mpy = MY;
}

}

(MX, MY) 是对象现在在屏幕上的另一个点。

这是我的计算函数:

public class Movement {

public float[] getPath(float dx, float dy, float mX, float mY){
    float[] ratio = new float[2];
    ratio[0] = 1;
    float a;
    float Ry1;
    float Ry2;
    float Rx1 = 1;
    float Rx2 = 2;
    a = (dy-mY)/(dx-mX);
    Ry1=a*(Rx1-dx)+dy;
    Ry2=a*(Rx2-dx)+dy;
    ratio[1] = Math.abs(Ry1-Ry2);

    return ratio;
}

}

我的 onTouchEvent

@Override
public boolean onTouchEvent(MotionEvent event){

dx = event.getX();
dy = event.getY();


if(event.getAction()==1){
thread = new MiceThread(mice, mview, dx, dy);
thread.start();
}

return true;
}

这是我的线程:

public class MiceThread extends Thread {

private Mice gameMice;
private MiceView gameView;
private float x;
private float y;

public MiceThread(Mice theMice, MiceView theView, float x, float y){
    this.gameMice = theMice;
    this.gameView = theView;
    this.x = x;
    this.y = y;
}

public void run(){
    while(1<2){
        this.gameMice.move(x, y);
        this.gameView.postInvalidate();

        try
        {
            MiceThread.sleep(5);

        }
        catch (InterruptedException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

}

我收到第一个代码块中指定的错误。

谢谢!

4

1 回答 1

2

如果你得到一个空指针,我看到你从来没有初始化你的Movement getRatio

将其添加到您的构造函数中:

public Mice(float x, float y){
MX = x;
MY = y;
getRatio = new Movement();
}

并填写您的运动所需的任何参数...

您还可以static像这样在运动中创建一个方法:

public class Movement {

    public static float[] getPath(float dx, float dy, float mX, float mY){
       ...
    }
}

并像这样使用它:

public void move(float dx, float dy){
    float[] ratio = Movement.getPath(dx, dy, MX, MY);
    MX++;
    MY = MY + ratio[1];
    GameActivity.mpx = MX;
    GameActivity.mpy = MY;
}

你的getPath(...)功能与它无关。

编辑:

无法真正提供帮助,因为缺乏信息。只有几个注释...

避免

if(event.getAction()==1){
  thread = new MiceThread(mice, mview, dx, dy);
  thread.start();
}

试试看

if(event.getAction()==1){
    if (thread =! null){
        //kill Thread First. instead of while(1<2) add a boolean inside MiceThread.run 
        // and set it to false at this point.... 
        // if you don't this Thread may live as long as GC doesn't kill it.
    }
    thread = new MiceThread(mice, mview, dx, dy);
    thread.start();
}

然后

public void run(){
while(someBool){
    this.gameMice.move(x, y);
    //does your Mice View has a reference to this.gameMice???
    //if not update your Mice inside this.gameView....
    // I Cannot tell you how, because I don't know what MiceView is doing...
    //postInvalidate() works ONLY, if Something inside the VIEW had changed...
    this.gameView.postInvalidate();

    try
    {
        MiceThread.sleep(5);

    }
    catch (InterruptedException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
}
于 2012-05-16T15:20:23.390 回答