AndEngine:使用加速计移动视差背景
我想在倾斜手机时移动视差背景(恒定速度)。 视差背景正在移动,但速度变慢并且会闪烁。 也许有人有一个很好的例子或者知道出了什么问题?
部分代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要检测手机倾斜,请使用方向信息 (
onOrientationChanged
),而不是加速度。此方法将获取 OrientationData ,您可以从中直接询问手机围绕每个轴的倾斜度:这些方法返回的值以度为单位。我已经实现了一个完全倾斜控制的游戏,仅使用来自此方法的信息,因此我知道它非常适合该目的。
另请记住更改传感器以使用
enableOrientationSensor(this)
而不是enableAccelerationSensor
。For detecting phone tilting use orientation info (
onOrientationChanged
), not acceleration. This method will getOrientationData
from which you can directly ask the tilt of the phone around each 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 theenableAccelerationSensor
.我建议你在这里看看这个项目
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]
确保 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.