กล้องสั่นไหว AndEngine

ฉันพยายามค้นหาวิธีที่ดีที่สุดในการเคลื่อนย้ายกล้องโดยการลากฉากพื้นหลัง ฉันยังไม่พบแหล่งข้อมูลใด ๆ ที่จะช่วยเหลือได้ ฉันค่อนข้างใหม่กับการเขียนโปรแกรม 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;
}

ขณะนี้โค้ดนี้จะสร้างมุมมองกล้องที่จำกัด และเมื่อฉันพยายามทดสอบและลากพื้นหลัง สไปรท์จะสั่น ฉันได้ลองสร้างเกณฑ์เพื่อให้การเคลื่อนไหวน้อยเกินไปจะไม่ทำให้เกิดการลาก แต่สำหรับเกณฑ์ที่จะไม่สั่นไหว สไปรท์เริ่มจะกระตุกเล็กน้อย

หากมีวิธีที่ดีกว่าในการดำเนินการนี้หรือแหล่งข้อมูลบางอย่างที่ฉันยังไม่เคยเห็นมาก่อนเพื่อให้สามารถแพนกล้องได้ดีขึ้น การขยับไปในทิศทางที่ถูกต้องจะได้รับการชื่นชมอย่างมาก


person Mnazanda    schedule 03.08.2015    source แหล่งที่มา
comment
คุณรู้ไหมว่ามีเมธอด camera.setChaseEntity(entiy) ที่ทำหน้าที่ไล่วัตถุ?   -  person Łukasz Motyczka    schedule 03.08.2015
comment
ฉันเคยได้ยินเรื่องนั้นแต่ไม่รู้ว่าจะใช้ได้กับสถานการณ์นี้หรือไม่ นั่นสามารถใช้เพื่อไล่ตามการเคลื่อนไหวลากได้หรือไม่?   -  person Mnazanda    schedule 03.08.2015
comment
คุณลองเปลี่ยนความเร็วของกล้องแล้วหรือยัง? เปลี่ยน 400, 400 เป็นประมาณ 1500, 1500   -  person Łukasz Motyczka    schedule 04.08.2015
comment
ฉันพยายามแล้ว แต่มันก็ไม่ได้ผล ฉันพบวิธีแก้ปัญหาและโพสต์ไว้ ขอบคุณสำหรับข้อเสนอแนะ!   -  person Mnazanda    schedule 04.08.2015


คำตอบ (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);


        }
    }

ฉันทำให้มันซับซ้อนเกินไป

person Mnazanda    schedule 04.08.2015