这是我的SurfaceView类。每次从另一个正在侦听方向传感器的类调用onMove函数时,它会在屏幕上写入一个球。这使得球可以控制移动手机。
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class SurfaceView extends View {
Paint paint = new Paint();
Point point;
public SurfaceView(Context context) {
super(context);
paint.setColor(Color.GREEN);
paint.setAntiAlias(true);
setBackgroundResource(R.drawable.level_1);
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawCircle(point.x, point.y, 5, paint);
}
//paints the ball in the center of the screen
public void initialiceScreen (float x, float y) {
point = new Point();
point.x = x/2;
point.y = y/2;
invalidate();
}
//on every call sets a new position for the ball
public void onMove (float x, float y) {
point.x += (10*x);
point.y += (10*y);
invalidate();
}
}
class Point {
float x, y;
@Override
public String toString() {
return x + ", " + y;
}
}
我在背景中设置了一个模拟电路的图像,目标是将球保持在电路中。
如何检测球的位置是否超出赛道? 保存所有使电路复合的坐标并将它们与球的实际位置进行比较? (这将是非常缺乏的?)