Android计算错误 - 解析几何

时间:2012-05-16 15:08:49

标签: android arrays math geometry ontouchevent

在尝试计算触摸点(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();
        }

    }
}

}

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

谢谢!

1 个答案:

答案 0 :(得分: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();
    }

}
}