简体   繁体   English

如何在Andengine中拖动场景背景?

[英]How to drag Scene background in Andengine?

I recently use Andengine to develop a game. 我最近使用Andengine开发游戏。 A question is how to drag a large Scene background in my android phone? 问题是如何在我的Android手机中拖动大场景背景? Is that anyone to achieve it? 是有人实现它吗?

Here's the TouchDragExample from AndEngine: 这是来自AndEngine的TouchDragExample:

public class TouchDragExample extends SimpleBaseGameActivity {
    // ===========================================================
    // Constants
    // ===========================================================

    private static final int CAMERA_WIDTH = 720;
    private static final int CAMERA_HEIGHT = 480;

    // ===========================================================
    // Fields
    // ===========================================================

    private BitmapTextureAtlas mBitmapTextureAtlas;
    private ITextureRegion mFaceTextureRegion;

    // ===========================================================
    // Constructors
    // ===========================================================

    // ===========================================================
    // Getter & Setter
    // ===========================================================

    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================

    @Override
    public EngineOptions onCreateEngineOptions() {
        Toast.makeText(this, "Touch & Drag the face!", Toast.LENGTH_LONG).show();

        final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

        return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
    }

    @Override
    public void onCreateResources() {
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

        this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.BILINEAR);
        this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box.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 - this.mFaceTextureRegion.getWidth()) / 2;
        final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
        final Sprite 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;
            }
        };
        face.setScale(4);
        scene.attachChild(face);
        scene.registerTouchArea(face);
        scene.setTouchAreaBindingOnActionDownEnabled(true);

        return scene;
    }

    // ===========================================================
    // Methods
    // ===========================================================

    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================
}

You can also move images around using pathmodifiers: 您也可以使用pathmodifiers移动图像:

yourBackgroundSprite.registerEntityModifier(new PathModifier(parameters...));

Or you can set the camera using setChaseEntity() to follow some other entity than the image you want to pan around. 或者,您可以使用setChaseEntity()设置摄像机,使其跟随其他要平移的图像之外的实体。

And finally, take a look at the AutoParallaxBackgroundExample that comes with AndEngine. 最后,看看AndEngine附带的AutoParallaxBackgroundExample。 That'll give you not only an example of a moving background, but an example of multiple parallax layers of background. 这不仅为您提供了移动背景的示例,还为您提供了背景的多个视差层的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM