1

嗨,我是新来的(我只编程了几个月,所以我的掌握是不稳定的)日食,我正在尝试创建一个游戏,其中玩家的化身随着加速度计移动,他们必须躲避屏幕上的东西(从自上而下的视角)。我已经创建了一个游戏屏幕类来绘制头像、背景和敌人,并且我正在尝试为加速度计创建一个类来告诉游戏屏幕将头像移动到哪里。目前我有一个类,其中包含扩展 AndroidGame 框架的加速度计代码,但由于它不是一个活动并且没有 onCreate,我不确定如何设置加速度计。有没有办法做到这一点,或者我只是把这一切都错了?

这是我到目前为止所拥有的(显然不完整)。

import edu.austincc.testlearnand.SplashLoadingScreen;
import edu.austincc.framework.Screen;
import edu.austincc.testlearnand.Assets;
import edu.austincc.framework.implementation.AndroidGame;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class BuffaloStampede extends AndroidGame implements SensorEventListener {
TextView textView;
SensorManager manager;
double[] gravity = new double[3];
public double[] linear_acceleration = new double[3];

boolean firstTimeCreate = true;

@Override
public Screen getInitScreen() {

    if (firstTimeCreate) {
        Assets.loadBS(this);
        firstTimeCreate = false;
    }

    return new SplashLoadingScreen(this);

}

@Override
public void onBackPressed() {
    getCurrentScreen().backButton();
}


@Override
public void onResume() {
    super.onResume();

    Assets.theme.play();

}

@Override
public void onPause() {
    super.onPause();
    Assets.theme.pause();
    manager.unregisterListener(this);

}







public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    textView = new TextView(this);

    manager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
    if (manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() == 0) {
        textView.setText("No accelerometer installed");
    } else {
        Sensor accelerometer = manager.getSensorList(
                Sensor.TYPE_ACCELEROMETER).get(0);
        if (!manager.registerListener(this, accelerometer,
                SensorManager.SENSOR_DELAY_NORMAL)) {
            textView.setText("Couldn't register accelerometer listener");
        }
    }
    setContentView(textView);
}

@Override
public void onSensorChanged(SensorEvent event) {
    // alpha is calculated as t / (t + dT)
    // with t, the low-pass filter's time-constant
    // and dT, the event delivery rate

    final double alpha = 0.8;

    gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
    gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
    gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];

    linear_acceleration[0] = event.values[0] - gravity[0];
    linear_acceleration[1] = event.values[1] - gravity[1];
    linear_acceleration[2] = event.values[2] - gravity[2];

}

public double[] getGravity() {
    return gravity;
}

public double[] getLinear_acceleration() {
    return linear_acceleration;
}

public void setGravity(double[] gravity) {
    this.gravity = gravity;
}

public void setLinear_acceleration(double[] linear_acceleration) {
    this.linear_acceleration = linear_acceleration;
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // nothing to do here
}
}
4

1 回答 1

2

在您的加速度计类中创建一个带有 Context 参数的构造函数,并使用此参数来实例化 SensorManager。

public MyAccelerometerClass implements SensorEventListener
{
     private SensorManager sensorManager;

     public MyAccelerometerClass(Context context)
     {
          sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); 
     }

     // codes 
 }   
于 2013-05-07T23:03:12.737 回答