While trying to calculate the function between a touch point (dx, dy), i am receiving an error when calling the calculation function.
Here is my move function:
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) is another point where an object is right now on the screen.
Here is my calculation function:
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;
}
}
My 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;
}
And here is my thread:
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();
}
}
}
}
I am getting the error where specified in the first block of code.
Thanks!
if you get a Null Pointer , I see you never initialized your
Movement getRatioadd it to your Constructor:
and Fill in any parameter you need for your Movement…
You Can also make a
staticMethod inside your Movement like this:and use it like this:
your
getPath(...)function has nothing to do with it.EDIT:
Cannot Really help, because the lack of info. Only a few Notes…
AVOID
Try it like
Then