我无法合并触摸事件和加速度计来移动SurfaceView
上的对象。
我在下面的行中收到 NullPointerException :
player = new Player((SensorManager)mContext.getSystemService (Context.SENSOR_SERVICE), BitmapFactory.decodeResource(getResources(), R.drawable.helicopter), 65, 25, 3);
这确实给了我“分配给itselfe”的提示:
this.mContext = mContext;
我想这可能是问题的根源,但我仍然不知道如何处理我的问题。
这是从SurfaceView
扩展的类。
public class GamePanel extends SurfaceView implements SurfaceHolder.Callback{
private MainThread thread;
private Background bg;
public static final int MOVESPEED = -5;
public static final int WIDTH = 856;
public static final int HEIGTH = 480;
private Player player;
Context mContext;
public GamePanel(Context context){
super(context);
getHolder().addCallback(this);
this.mContext = mContext;
thread = new MainThread(getHolder(), this);
setFocusable(true); //damit kann das ding Events handeln! WOW!!
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
bg = new Background(BitmapFactory.decodeResource(getResources(), R.drawable.grassbg1));
player = new Player((SensorManager)mContext.getSystemService(Context.SENSOR_SERVICE),BitmapFactory.decodeResource(getResources(), R.drawable.helicopter), 65, 25, 3);
thread.setRunning(true);
thread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
while(retry){
try{thread.setRunning(false);
thread.join();
}catch(InterruptedException e){e.printStackTrace();}
retry = false;
}
}
@Override
public boolean onTouchEvent (MotionEvent event){
if(event.getAction()==MotionEvent.ACTION_DOWN){
if(!player.getPlaying()){
player.setPlaying(true);
}
else{
player.setUp(true);
}
return true;
}
if(event.getAction()==MotionEvent.ACTION_UP){
player.setUp(false);
return true;
}
return super.onTouchEvent(event);
}
public void update(){
if(player.getPlaying()) {
bg.update();
player.update();
}
}
@Override
public void draw(Canvas canvas){
final float scaleFactorX = getWidth()/(WIDTH*1.f);
final float scaleFactorY = getHeight()/(HEIGTH*1.f);
if(canvas != null){
final int savedState = canvas.save();
canvas.scale(scaleFactorX, scaleFactorY);
bg.draw(canvas);
player.draw(canvas);
canvas.restoreToCount(savedState);
}
}
这是运动或简单物理学的“玩家”类。
public class Player extends GameObject implements SensorEventListener{
private Bitmap spritesheet;
private int score;
private float accX, accY;
private double dya;
private boolean up;
private boolean playing;
private Animation animation = new Animation();
private long startTime;
private SensorManager sensorManager;
public Player(SensorManager sensorManager, Bitmap res, int w, int h, int numFrames){
x = 100;
y = GamePanel.HEIGTH/2;
dy = 0;
score = 0;
height = h;
width = w;
Bitmap[] image = new Bitmap[numFrames];
spritesheet = res;
for(int i = 0; i < image.length; i++){
image[i] = Bitmap.createBitmap(spritesheet, i*width, 0, width, height);
}
animation.setFrames(image);
animation.setDelay(10);
startTime = System.nanoTime();
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
accX = (sensorEvent.values[1]);
accY = (sensorEvent.values[2]);
}
@Override
public void onAccuracyChanged(Sensor sensor, int i){
//not needed
}
public void setUp(boolean b){up = b;}
public void update(){
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
animation.update();
if(up){
dy = (int)(dya-=1.1);
}
else{
dy = (int)(dya+=1.1);
}
if(dy>14)dy = 14;
if(dy<-14)dy = -14; // dies begrenzd die maximale geschwindigkeit
y += dy+(10*accY);
dy = 0;
}
public void draw(Canvas canvas){
canvas.drawBitmap(animation.getImage(),x,y,null);
}
public int getScore(){return score;}
public boolean getPlaying(){return playing;}
public void setPlaying(boolean b){playing = b;}
public void resetDYA(){dya = 0;}
public void resetScore(){score = 0;}
答案 0 :(得分:0)
你的问题可能就是你的游戏面板&#34;您使用this.mContext = mContext;
的构造函数,但我认为您应该使用this.mContext = context;