【问题标题】:Shaky Camera Pan AndEngine摇晃的相机平移和引擎
【发布时间】:2015-08-04 23:11:09
【问题描述】:

我一直在尝试通过拖动背景场景来找到移动相机的最佳方式。我还没有找到任何可以提供帮助的资源。我对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;
}

目前,此代码将产生有限的相机视图,当我尝试测试和拖动背景时,精灵会抖动。我已经尝试创建一个阈值,因此太小的运动不会触发阻力。但是为了让它不抖动的阈值,精灵开始有点跳动。

如果有更好的方法来解决这个问题或者我还没有看到一些资源来更好地平移相机,我们将不胜感激朝正确方向轻推。

【问题讨论】:

  • 你知道有一个camera.setChaseEntity(entiy)方法负责追物吗?
  • 我听说过,但我不知道它是否适用于这种情况。可以用来追逐拖动运动吗?
  • 您是否尝试过更改相机的速度?将 400、400 更改为 1500、1500
  • 我试过了,但没用。我找到了解决方案并发布了它。不过感谢您的建议!

标签: android camera andengine


【解决方案1】:

经过一些尝试和寻找答案,我发现这是行之有效的。将部分 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);


        }
    }

我太复杂了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多