我一直试图通过拖动背景场景找到移动相机的最佳方法。我还没有找到任何资源来帮助你。我是android编程和使用andEngine的新手。
到目前为止,这是我的代码
public class MainActivity extends SimpleBaseGameActivity{
private static final int CAMERA_WIDTH = 800;
private static final int CAMERA_HEIGHT = 480;
private SmoothCamera mSmoothCamera;
private BitmapTextureAtlas mBitmapTextureAtlas;
private TextureRegion mFaceTextureRegion;
private Sprite face;
private float lastXmove;
private float lastYmove;
@Override
public EngineOptions onCreateEngineOptions() {
mSmoothCamera = new SmoothCamera(0, 0, CAMERA_WIDTH-200, CAMERA_HEIGHT-200, 400, 400, 2);
mSmoothCamera.setBounds(0f,0f,CAMERA_WIDTH, CAMERA_HEIGHT);
cameraLastX = mSmoothCamera.getCenterX();
cameraLastY = mSmoothCamera.getCenterY();
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mSmoothCamera);
}
@Override
public void onCreateResources() {
this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 28, 247, TextureOptions.BILINEAR);
this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "tower.png", 0, 0);
this.mBitmapTextureAtlas.load();
}
@Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
final float centerX = (CAMERA_WIDTH-100 - this.mFaceTextureRegion.getWidth()) / 2;
final float centerY = (CAMERA_HEIGHT-100 - this.mFaceTextureRegion.getHeight()) / 2;
face = new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager()) {
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
return true;
}
};
scene.attachChild(face);
scene.registerTouchArea(face);
scene.setTouchAreaBindingOnActionDownEnabled(true);
scene.setOnSceneTouchListener(new IOnSceneTouchListener() {
@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
if (pSceneTouchEvent.isActionDown()) {
lastXmove = pSceneTouchEvent.getX();
lastYmove = pSceneTouchEvent.getY();
}
if (pSceneTouchEvent.isActionMove()) {
float differenceX = pSceneTouchEvent.getX() - lastXmove;
float differenceY = pSceneTouchEvent.getY() - lastYmove;
mSmoothCamera.setCenter(mSmoothCamera.getCenterX() - differenceX, mSmoothCamera.getCenterY() - differenceY);
lastXmove = pSceneTouchEvent.getX();
lastYmove = pSceneTouchEvent.getY();
}
if (pSceneTouchEvent.isActionUp()) {
}
return true;
}
});
return scene;
}
目前此代码将生成有限的摄像机视图,当我尝试测试并拖动背景时,精灵会抖动。我已经尝试创建一个阈值,因此太小的移动不会触发拖动。但是为了使它不动摇的门槛,精灵开始有点跳跃。
如果有更好的方法可以解决这个问题或某些资源,我还没有看到更好地平移相机,那么在正确的方向上轻推将会非常感激。
答案 0 :(得分:0)
通过一点点游戏并寻找答案,这就是我发现的有效方法。将onSceneTouchEvent函数的一部分更改为:
if (pSceneTouchEvent.isActionDown()) {
// Obtain the initial touch coordinates when the scene is first pressed
mInitialTouchX = pSceneTouchEvent.getX();
mInitialTouchY = pSceneTouchEvent.getY();
}
if(pSceneTouchEvent.isActionMove()){
if (!touchSpriteLock && !zoomLock) {
// Calculate the offset touch coordinates
final float touchOffsetX = mInitialTouchX - pSceneTouchEvent.getX();
final float touchOffsetY = mInitialTouchY - pSceneTouchEvent.getY();
// Apply the offset touch coordinates to the current camera coordinates
mCamera.setCenter(mCamera.getCenterX() + touchOffsetX, mCamera.getCenterY() + touchOffsetY);
}
}
我过度复杂了