0

我认为这对你们来说是一个简单的问题(我只是这个行业的新手):
所以,在我基于 android java 的游戏中,我有这个球,并且..
我想检测用户的移动。
如果用户将设备向右倾斜,球将向右侧滚动
如果用户将设备向左倾斜,球将向左侧滚动。

所以,简而言之:
如何将球“连接”到设备运动(仅在 x 轴 - 左右)

谢谢,
sock.socket

4

1 回答 1

1

首先,声明您的传感器和传感器管理器

public Sensor mySensor;
private SensorManager mySensorManager;

然后根据自己的喜好初始化变量。我选择让我的传感器延迟正常,并将我的精度灵敏度设置为高。

mySensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mySensor = mySensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mySensorManager.registerListener(this, mySensor, SensorManager.SENSOR_DELAY_NORMAL, SensorManager.SENSOR_STATUS_ACCURACY_HIGH);

现在您将需要一个 onSensorChanged 函数,该函数将在您的传感器每次感应到变化时运行。在此功能中,您可以为您的球设置动画。

@Override
public void onSensorChanged(SensorEvent event) {
    x = event.values[0];

    // The x value in the accelerometer is the 0th index in the array

    // Now you may do what you wish with this x value, and use it to move your ball 
    }
}
于 2016-06-13T05:55:49.653 回答