如何使用 AndEngine (Android) 移动精灵对象

2024-04-27

我正在使用 andengine 在 android 中开发游戏。我在精灵中放置了一个对象,例如

 this.mTexture = new Texture(32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
            this.mFaceTextureRegion = TextureRegionFactory.createFromAsset(this.mTexture, this, "gfx/bike.png", 0, 0);
---- and i place like 
Sprite bikeSprite= new Sprite(20, 50, this.mFaceTextureRegion);

我想在 j2me sprite 中使用这个 sprite

bikeSprite.move(--);我如何在安卓中做到这一点。我不想使用 setPosition 。


public class EntityModifierExample extends BaseExample {
    // ===========================================================
    // Constants
    // ===========================================================

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

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

    private Camera mCamera;
    private Texture mTexture;
    private TiledTextureRegion mFaceTextureRegion;

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

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

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

    @Override
    public Engine onLoadEngine() {
        this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
        return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
    }

    @Override
    public void onLoadResources() {
        this.mTexture = new Texture(64, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        this.mFaceTextureRegion = TextureRegionFactory.createTiledFromAsset(this.mTexture, this, "gfx/face_box_tiled.png", 0, 0, 2, 1);

        this.mEngine.getTextureManager().loadTexture(this.mTexture);
    }

    @Override
    public Scene onLoadScene() {
        this.mEngine.registerUpdateHandler(new FPSLogger());

        final Scene scene = new Scene();
        scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));

        final int centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
        final int centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;

        final Rectangle rect = new Rectangle(centerX + 100, centerY, 32, 32);
        rect.setColor(1, 0, 0);

        final AnimatedSprite face = new AnimatedSprite(centerX - 100, centerY, this.mFaceTextureRegion);
        face.animate(100);
        face.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

        final LoopEntityModifier entityModifier =
            new LoopEntityModifier(
                    new IEntityModifierListener() {
                        @Override
                        public void onModifierStarted(final IModifier<IEntity> pModifier, final IEntity pItem) {
                            EntityModifierExample.this.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EntityModifierExample.this, "Sequence started.", Toast.LENGTH_LONG).show();
                                }
                            });
                        }

                        @Override
                        public void onModifierFinished(final IModifier<IEntity> pEntityModifier, final IEntity pEntity) {
                            EntityModifierExample.this.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EntityModifierExample.this, "Sequence finished.", Toast.LENGTH_LONG).show();
                                }
                            });
                        }
                    },
                    1,
                    new ILoopEntityModifierListener() {
                        @Override
                        public void onLoopStarted(final LoopModifier<IEntity> pLoopModifier, final int pLoop, final int pLoopCount) {
                            EntityModifierExample.this.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EntityModifierExample.this, "Loop: '" + (pLoop + 1) + "' of '" + pLoopCount + "' started.", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                        @Override
                        public void onLoopFinished(final LoopModifier<IEntity> pLoopModifier, final int pLoop, final int pLoopCount) {
                            EntityModifierExample.this.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EntityModifierExample.this, "Loop: '" + (pLoop + 1) + "' of '" + pLoopCount + "' finished.", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    },
                    new SequenceEntityModifier(
                            new RotationModifier(1, 0, 90),
                            new AlphaModifier(2, 1, 0),
                            new AlphaModifier(1, 0, 1),
                            new ScaleModifier(2, 1, 0.5f),
                            new DelayModifier(0.5f),
                            new ParallelEntityModifier(
                                    new ScaleModifier(3, 0.5f, 5),
                                    new RotationByModifier(3, 90)
                            ),
                            new ParallelEntityModifier(
                                    new ScaleModifier(3, 5, 1),
                                    new RotationModifier(3, 180, 0)
                            )
                    )
            );

        face.registerEntityModifier(entityModifier);
        rect.registerEntityModifier(entityModifier.clone());

        scene.attachChild(face);
        scene.attachChild(rect);

        return scene;
    }

    @Override
    public void onLoadComplete() {

    }

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

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

这段代码是从示例项目 Andengine https://github.com/nicolasgramlich/AndEngineExamples/. 参考原代码 https://github.com/nicolasgramlich/AndEngineExamples/blob/GLES2/src/org/andengine/examples/EntityModifierExample.java.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 AndEngine (Android) 移动精灵对象 的相关文章

随机推荐