AndEngine:使用加速计移动视差背景

发布于 2025-01-07 18:57:31 字数 1069 浏览 0 评论 0原文

我想在倾斜手机时移动视差背景(恒定速度)。 视差背景正在移动,但速度变慢并且会闪烁。 也许有人有一个很好的例子或者知道出了什么问题?

部分代码:

public class TestPhysicsActivity extends SimpleBaseGameActivity  implements IAccelerationListener{
...
 private static final int CAMERA_WIDTH = 720;
 private static final int CAMERA_HEIGHT = 480;
 final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
 final ScrollableParallaxBackground background = new ScrollableParallaxBackground(0, 0, 0, camera);
....    

  @Override  
  public void onLoadResources() {  
  this.enableAccelerometerSensor(this);  
  }  

 ....
@Override
 public void onAccelerationAccuracyChanged(final AccelerationData pAccelerationData) {
 }
 @Override
 public void onAccelerationChanged(final AccelerationData pAccelerationData) {
  final Vector2 gravity = Vector2Pool.obtain(pAccelerationData.getX(), pAccelerationData.getY());
  this.mPhysicsWorld.setGravity(gravity);
  Vector2Pool.recycle(gravity);
 background.setParallaxValue(this.mEngine.getSecondsElapsedTotal()*pAccelerationData.getX());

 }

}

谢谢

I want to move a parallax background (constant speed) when i tilt the phone.
The parallax background is moving, but to slow and it flickers.
Maybe somebody has a good example or knows what's wrong?

Some parts of the code:

public class TestPhysicsActivity extends SimpleBaseGameActivity  implements IAccelerationListener{
...
 private static final int CAMERA_WIDTH = 720;
 private static final int CAMERA_HEIGHT = 480;
 final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
 final ScrollableParallaxBackground background = new ScrollableParallaxBackground(0, 0, 0, camera);
....    

  @Override  
  public void onLoadResources() {  
  this.enableAccelerometerSensor(this);  
  }  

 ....
@Override
 public void onAccelerationAccuracyChanged(final AccelerationData pAccelerationData) {
 }
 @Override
 public void onAccelerationChanged(final AccelerationData pAccelerationData) {
  final Vector2 gravity = Vector2Pool.obtain(pAccelerationData.getX(), pAccelerationData.getY());
  this.mPhysicsWorld.setGravity(gravity);
  Vector2Pool.recycle(gravity);
 background.setParallaxValue(this.mEngine.getSecondsElapsedTotal()*pAccelerationData.getX());

 }

}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回梦 2025-01-14 18:57:31

要检测手机倾斜,请使用方向信息 (onOrientationChanged),而不是加速度。此方法将获取 OrientationData ,您可以从中直接询问手机围绕每个轴的倾斜度:

@Override public void onOrientationChanged(OrientationData pOrientationData) {
    pOrientationData.getYaw() //compass
    pOrientationData.getPitch() //one orientation axis
    pOrientationData.getRoll() //other orientation axis
}

这些方法返回的值以度为单位。我已经实现了一个完全倾斜控制的游戏,仅使用来自此方法的信息,因此我知道它非常适合该目的。

另请记住更改传感器以使用 enableOrientationSensor(this) 而不是 enableAccelerationSensor

For detecting phone tilting use orientation info (onOrientationChanged), not acceleration. This method will get OrientationData from which you can directly ask the tilt of the phone around each axis:

@Override public void onOrientationChanged(OrientationData pOrientationData) {
    pOrientationData.getYaw() //compass
    pOrientationData.getPitch() //one orientation axis
    pOrientationData.getRoll() //other orientation axis
}

Values returned by these methods are in degrees. I have implemented a full tilt-controlled game with information from only this method so I know it works just fine for that purpose.

Also remember to change the sensor enabling to use enableOrientationSensor(this) instead of the enableAccelerationSensor.

且行且努力 2025-01-14 18:57:31

我建议你在这里看看这个项目
http://www.andengine .org/forums/tutorials/moving-a-sprite-with-the-accelerometer-sensor-t1223.html

这个使用加速计传感器来移动精灵,您可以得到一个想法并更改它,以便它影响您的背景,请检查论坛的第二页,我在其中发布了适用于当前版本的 andEngine 的修改后的代码[该项目不使用物理]

I suggest you take a look at this project here
http://www.andengine.org/forums/tutorials/moving-a-sprite-with-the-accelerometer-sensor-t1223.html

this one uses the accelerometer sensor to move a sprite , you can get an idea and change it so it affects the background for you, check the 2nd page in the forum where I posted a modified code that works on current versions of andEngine [that project doesn't use physics]

转角预定愛 2025-01-14 18:57:31

确保 mEngine.getSecondsElapsedTotal() 中的数据正确。根据我的经验,AndEgine 中的时间跟踪有些可疑。

Make sure that the data from mEngine.getSecondsElapsedTotal() is correct. From my experience, something fishy is going on with time tracking in AndEgine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文