2012-05-16 20 views
0

嘗試計算觸點(dx,dy)之間的函數時,我在調用計算函數時收到錯誤。Android計算錯誤 - 解析幾何

這裏是我的移動功能:

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(); 
     } 

    } 
} 

} 

我得到其中第一塊指定的錯誤的代碼。

謝謝!

+0

什麼錯誤? – kosa

+0

'我得到的錯誤,其中......'完全無奈的答案......添加完整的logcat –

+0

錯誤是(Logcat): 05-16 18:04:10.105:E/AndroidRuntime(19380):java.lang .NullPointerException – arielschon12

回答

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(...)功能無關,用它做。

編輯:

不能真的幫助,因爲缺乏信息。只有少數注...

AVOID

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(); 
    } 

} 
} 
+0

修復了這個錯誤,但屏幕上的對象仍然沒有移動。在這裏,再次看看我的問題。我添加了我的線程和onTouch事件。 – arielschon12

+0

你想爲任何MotionEvent.ACTION_UP創建一個新的鼠標線程?而且你不殺舊的。你的'MiceView'應該從哪裏知道,'Mice'已經被移動了?你沒有在MiceView內做任何事情......此外,考慮這是一個新問題(在一個新主題內),因爲我的文章回答你的第一個問題 –

+0

你能編輯你的答案嗎?我真的不明白在這裏做什麼.. :( – arielschon12