0
我想做一個小迷宮遊戲。爲此,我現在有4面牆可以打出一個球。全部是ImageViews。我利用了Rect.intersect
方法。Android的java矩形陣列不起作用
爲了從牆壁上獲得矩形,我使用了ImageView.getHitRect
。矩形然後被存儲在矩形數組中。這似乎都是失敗的地方。當我使用一個矩形的這個陣列getHitRect
我得到以下錯誤的方法:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Rect.set(int, int, int, int)' on a null object reference
這裏是我的代碼
private ImageView ball;
private ImageView wall[] = new ImageView[4];
private TextView text;
private SensorManager sManager;
private int a=300; //x position
private int b=300; //y position
int x=0;
int y=0;
Rect rect[] = new Rect[4];
Rect ballrect = new Rect();
float show = 1;
boolean allowedMovement[]={true,true,true,true};
int maxX=0;
int maxY=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ball = (ImageView) findViewById(R.id.ball);
text = (TextView) findViewById(R.id.info);
wall[0] = (ImageView) findViewById(R.id.wall1);
wall[1] = (ImageView) findViewById(R.id.wall2);
wall[2] = (ImageView) findViewById(R.id.wall3);
wall[3] = (ImageView) findViewById(R.id.wall4);
sManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
//when this Activity starts
@Override
protected void onResume()
{
super.onResume();
/*register the sensor listener to listen to the gyroscope sensor, use the
callbacks defined in this class, and gather the sensor information as quick
as possible*/
sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_FASTEST);
}
//When this Activity isn't visible anymore
@Override
protected void onStop()
{
//unregister the sensor listener
sManager.unregisterListener(this);
super.onStop();
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1)
{
//Do nothing.
}
public void move(int x, int y) {
RelativeLayout.LayoutParams alp = (RelativeLayout.LayoutParams) ball.getLayoutParams();
int maxMovementX=Math.abs(x);
int maxMovenentY=Math.abs(y);
int stepsTakenX=0;
int stepsTakenY=0;
while(maxMovementX>stepsTakenX || maxMovenentY>stepsTakenY) {
hitCheck();
//up 0, down 1, right 2, left 3
if (stepsTakenX < maxMovementX) {
stepsTakenX=stepsTakenX+1;
if (x > 0 && allowedMovement[2] == true) {//right
a = a - 1;
}
if (x < 0 && allowedMovement[3] == true) {//left
a = a + 1;
}
}
if (stepsTakenY < maxMovenentY) {
stepsTakenY=stepsTakenY+1;
if (y > 0 && allowedMovement[1] == true) {//down
b = b - 1;
}
if (y < 0 && allowedMovement[0] == true) {//up
b = b + 1;
}
}
alp.leftMargin = a;
alp.topMargin = b;
ball.setLayoutParams(alp);
}
}
public void hitCheck(){
//up 0, down 1, right 2, left 3
for(int i=0;i<4;i++){
allowedMovement[i]=true;
}
ball.getHitRect(ballrect);
for(int i=0;i<4;i++) {
wall[i].getHitRect(rect[i]);
if (Rect.intersects(rect[i], ballrect)) {
wall[i].setAlpha(show);
float wy = (ballrect.width() + rect[i].width()) * (ballrect.centerY() - rect[i].centerY());
float hx = (ballrect.height() + rect[i].height()) * (ballrect.centerX() - rect[i].centerX());
if (wy > hx) {
if (wy > -hx) {//top
allowedMovement[1] = false;
} else {//left
allowedMovement[2] = false;
}
} else {
if (wy > -hx) {//right
allowedMovement[3] = false;
} else {//bottom
allowedMovement[0] = false;
}
}
}
}
}
它有沒有問題與ballrect但與實體不在陣列中
謝謝,這確實起作用。不知道爲什麼我沒有想到這個... – sateeprikker