使用 AndEngine 加速传感器定向
我正在使用 Andengine 并使用 SenorEvents 使精灵随着加速度计移动。
但由于这段代码的某种原因,在平板电脑上,如果我处于制作游戏的横向模式,它的行为就像处于纵向模式一样。
我做错了什么吗?在普通的手机设备上,它可以正常工作,
这就是我所拥有的。
sensorManager = (SensorManager) this.getSystemService(Arcade_MainGame.SENSOR_SERVICE);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
synchronized (this) {
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
accellerometerSpeedX = (int)event.values[1];
accellerometerSpeedY = (int)event.values[0];
break;
}
}
}
I am using Andengine and using SenorEvents to make a sprite move with the accelerometer.
But for some reason with this code, on tablet if i am in landscape mode which the game is made in, it acts as if it is in portrait mode.
Am i doing anything wrong? On a regular cell phone device, it works correctly
Here is what i have.
sensorManager = (SensorManager) this.getSystemService(Arcade_MainGame.SENSOR_SERVICE);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
synchronized (this) {
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
accellerometerSpeedX = (int)event.values[1];
accellerometerSpeedY = (int)event.values[0];
break;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在平板电脑和某些智能手机上,默认模式是横向模式,因此当您声明游戏使用横向模式时,在平板电脑上这是纵向模式。您所需要做的就是检查默认方向,如果存在横向,则将 X 与 Y 切换。以下是如何检查它的主题
检查 Android 手机上的方向
On tablets and some smartphones default mode is landscape so when you declare that your game uses landscape then on tablets this is portrait mode. All you need to do is check default orientation and switch X with Y if there is landscape. Here is topic how to check it
Check orientation on Android phone
在您的代码中,event.values[1] 应该为“Y”,而 event.values[0] 的值为“X”(您将它们放错了位置)。
In your code, event.values[1] is supposed to be "Y" while event.values[0] have the value for "X" (you have them misplaced).