【问题标题】:how to move sprite object using AndEngine (Android)如何使用 AndEngine (Android) 移动精灵对象
【发布时间】:2011-07-14 12:06:02
【问题描述】:

我正在使用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(--);我如何在android中做到这一点。我不想使用 setPosition 。

【问题讨论】:

  • 只需添加一个移动修饰符。下面给你一个例子
  • 引擎作者在新版引擎中去掉了Shape.setVelocity(),有点可惜。现在你必须使用修饰符。

标签: android andengine


【解决方案1】:
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
    // ===========================================================
}

此代码是从samples project Andengine 中提取的。 Reference to the original code.

【讨论】:

    【解决方案2】:

    我会尝试使用 PathModifier。我的游戏也有同样的问题,可以在 AndEngine 示例中找到 PathModifier 示例:

    package org.anddev.andengine.examples;
    
    import org.anddev.andengine.engine.Engine;
    import org.anddev.andengine.engine.camera.Camera;
    import org.anddev.andengine.engine.options.EngineOptions;
    import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
    import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
    import org.anddev.andengine.entity.IEntity;
    import org.anddev.andengine.entity.modifier.LoopEntityModifier;
    import org.anddev.andengine.entity.modifier.PathModifier;
    import org.anddev.andengine.entity.modifier.PathModifier.IPathModifierListener;
    import org.anddev.andengine.entity.modifier.PathModifier.Path;
    import org.anddev.andengine.entity.scene.Scene;
    import org.anddev.andengine.entity.scene.background.RepeatingSpriteBackground;
    import org.anddev.andengine.entity.sprite.AnimatedSprite;
    import org.anddev.andengine.opengl.texture.Texture;
    import org.anddev.andengine.opengl.texture.TextureOptions;
    import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
    import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
    import org.anddev.andengine.opengl.texture.source.AssetTextureSource;
    import org.anddev.andengine.util.Debug;
    import org.anddev.andengine.util.modifier.ease.EaseSineInOut;
    
    import android.widget.Toast;
    
    /**
     * @author Nicolas Gramlich
     * @since 11:54:51 - 03.04.2010
     */
    public class PathModifierExample extends BaseExample {
        // ===========================================================
        // Constants
        // ===========================================================
    
        private static final int CAMERA_WIDTH = 720;
        private static final int CAMERA_HEIGHT = 480;
    
        // ===========================================================
        // Fields
        // ===========================================================
    
        private Camera mCamera;
    
        private RepeatingSpriteBackground mGrassBackground;
    
        private Texture mTexture;
        private TiledTextureRegion mPlayerTextureRegion;
    
        // ===========================================================
        // Constructors
        // ===========================================================
    
        // ===========================================================
        // Getter & Setter
        // ===========================================================
    
        // ===========================================================
        // Methods for/from SuperClass/Interfaces
        // ===========================================================
    
        @Override
        public Engine onLoadEngine() {
            Toast.makeText(this, "You move my sprite right round, right round...", Toast.LENGTH_LONG).show();
            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(128, 128, TextureOptions.DEFAULT);
            this.mPlayerTextureRegion = TextureRegionFactory.createTiledFromAsset(this.mTexture, this, "gfx/player.png", 0, 0, 3, 4);
    
            this.mGrassBackground = new RepeatingSpriteBackground(CAMERA_WIDTH, CAMERA_HEIGHT, this.mEngine.getTextureManager(), new AssetTextureSource(this, "gfx/background_grass.png"));
    
            this.mEngine.getTextureManager().loadTexture(this.mTexture);
        }
    
        @Override
        public Scene onLoadScene() {
    //      this.mEngine.registerUpdateHandler(new FPSLogger());
    
            final Scene scene = new Scene();
            scene.setBackground(this.mGrassBackground);
    
            /* Create the face and add it to the scene. */
            final AnimatedSprite player = new AnimatedSprite(10, 10, 48, 64, this.mPlayerTextureRegion);
    
            final Path path = new Path(5).to(10, 10).to(10, CAMERA_HEIGHT - 74).to(CAMERA_WIDTH - 58, CAMERA_HEIGHT - 74).to(CAMERA_WIDTH - 58, 10).to(10, 10);
    
            /* Add the proper animation when a waypoint of the path is passed. */
            player.registerEntityModifier(new LoopEntityModifier(new PathModifier(30, path, null, new IPathModifierListener() {
                @Override
                public void onPathStarted(final PathModifier pPathModifier, final IEntity pEntity) {
                    Debug.d("onPathStarted");
                }
    
                @Override
                public void onPathWaypointStarted(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {
                    Debug.d("onPathWaypointStarted:  " + pWaypointIndex);
                    switch(pWaypointIndex) {
                        case 0:
                            player.animate(new long[]{200, 200, 200}, 6, 8, true);
                            break;
                        case 1:
                            player.animate(new long[]{200, 200, 200}, 3, 5, true);
                            break;
                        case 2:
                            player.animate(new long[]{200, 200, 200}, 0, 2, true);
                            break;
                        case 3:
                            player.animate(new long[]{200, 200, 200}, 9, 11, true);
                            break;
                    }
                }
    
                @Override
                public void onPathWaypointFinished(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {
                    Debug.d("onPathWaypointFinished: " + pWaypointIndex);
                }
    
                @Override
                public void onPathFinished(final PathModifier pPathModifier, final IEntity pEntity) {
                    Debug.d("onPathFinished");
                }
            }, EaseSineInOut.getInstance())));
            scene.attachChild(player);
    
            return scene;
        }
    
        @Override
        public void onLoadComplete() {
    
        }
    
        // ===========================================================
        // Methods
        // ===========================================================
    
        // ===========================================================
        // Inner and Anonymous Classes
        // ===========================================================
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多