0
我正在使用AndEngine爲Android平臺製作遊戲。我需要檢測一個圓形體是否接觸圓形精靈。爲此,我正在計算兩者的中心和半徑,並使用pythag來確定它們是否確實感人。但是,當我嘗試獲取身體的x座標時,我總是收到nullpointerexception。每次觸摸屏幕和精靈創建時,spriteNum都會增加。任何想法爲什麼?我的代碼:爲什麼我的身體返回null?
public class grow implements Runnable{
Sprite sp;
Body body[] = new Body[100];
public grow(Sprite sprite){
sp = sprite;
}
@Override
public void run() {
float radf, rads; //fill radius/stationary radius
float[] fill; //fillx/stationaryx
float fx=0, fy=0, sx, sy;
while(down){
//Log.e("Tag","Running thread. Down = "+Boolean.toString(down));
yourSprite[spriteNum].setScale(scale += 0.1);
fill = yourSprite[spriteNum].getSceneCenterCoordinates();
fill[0]=fx;
fill[1]=fy;
radf=yourSprite[spriteNum].getHeightScaled()/2;
Log.e("Fill X before for",Float.toString(fx));
if(spriteNum>0)
for(int x=0;x<spriteNum;x++){
rads=yourSprite[x].getHeightScaled()/2;
sx = body[x].getWorldCenter().x; //Null pointer exception
sy = body[x].getWorldCenter().y;
if(Math.sqrt(Math.pow((fx-sx),2)+Math.pow((fy-sy),2))<(radf+rads))
down = false;
Log.e("Fill x",Float.toString(fx));
Log.e("Stat x",Float.toString(sy));
}
try {
Thread.sleep(75);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
body[spriteNum] = PhysicsFactory.createCircleBody(mPhysicsWorld, yourSprite[spriteNum], BodyType.DynamicBody, FIXTURE_DEF);
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(yourSprite[spriteNum], body[spriteNum], true, true));
if(body[0]!=null)
Log.e("Body created","not null"); //This says it's not null, but when I did the same thing just inside of my for() loop and it did say it was null
}