android - 在 AndEngine 中绘制路径以移动图像或 Sprite

标签 android path andengine

您好,我是 Android 游戏开发的新手,我使用 AndEngine 我需要绘制一条路径,但是有一个问题 image

看图片。我需要的路径就像对象应该从屏幕底部移动到屏幕中间的第一个路径。但是当我绘制我的路径时,它移动到屏幕中间但又回到第二张图片中的第二点。图片正在通过路径移动,但它就像第二张图片。我需要它作为第一张图片。它应该停在第三点,应该再次从第一点开始。

我的代码如下

package com.example.sss;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.PathModifier;
import org.andengine.entity.modifier.PathModifier.IPathModifierListener;
import org.andengine.entity.modifier.PathModifier.Path;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.RepeatingSpriteBackground;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import   org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.source.AssetBitmapTextureAtlasSource;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;

/**
 * (c) 2010 Nicolas Gramlich
 * (c) 2011 Zynga
  *
 * @author Nicolas Gramlich
* @since 13:58:48 - 19.07.2010
 */
public class MainActivity extends SimpleBaseGameActivity {
// ===========================================================
// Constants
// ===========================================================

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

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

private RepeatingSpriteBackground mGrassBackground;

private BitmapTextureAtlas mBitmapTextureAtlas;
private TiledTextureRegion mPlayerTextureRegion;

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

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

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


public EngineOptions onCreateEngineOptions() {
    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);
}


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

    this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 128, 128);
    this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "player.png", 0, 0, 3, 4);
    this.mGrassBackground = new RepeatingSpriteBackground(CAMERA_WIDTH, CAMERA_HEIGHT, this.getTextureManager(), AssetBitmapTextureAtlasSource.create(this.getAssets(), "gfx/background_grass.png"), this.getVertexBufferObjectManager());
    this.mBitmapTextureAtlas.load();
}


public Scene onCreateScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());

    final Scene scene = new Scene();
    final Scene scene_one = new Scene();
    scene.setBackground(this.mGrassBackground);




    /* Calculate the coordinates for the face, so its centered on the camera.     */
    final float centerX = (CAMERA_WIDTH - this.mPlayerTextureRegion.getWidth()) / 2;
    final float centerY = (CAMERA_HEIGHT - this.mPlayerTextureRegion.getHeight()) / 2;
    System.out.println("aaaaaa"+centerX+"asdadas");

    /* Create the sprite and add it to the scene. */
    final AnimatedSprite player_one = new AnimatedSprite(centerX, centerY, 48, 64, this.mPlayerTextureRegion, this.getVertexBufferObjectManager());

    final Path path_one =  new Path(5).to(CAMERA_WIDTH - 58, 10).to(10, 10).to(100, CAMERA_HEIGHT/2f);//to(10, 10).to(10, CAMERA_HEIGHT - 74).to(CAMERA_WIDTH - 58, CAMERA_HEIGHT - 74).to(CAMERA_WIDTH - 58, 10).to(10, 10);
    final AnimatedSprite player = new AnimatedSprite(centerX, centerY, 48, 64, this.mPlayerTextureRegion, this.getVertexBufferObjectManager());
    final Path path = new Path(5).to(CAMERA_WIDTH - 58, 10).to(10, 10).to(10, CAMERA_HEIGHT/2f);


    player.registerEntityModifier(new LoopEntityModifier(new PathModifier(5, path, null, new IPathModifierListener() {

        public void onPathStarted(final PathModifier pPathModifier, final IEntity pEntity) {

        }


        public void onPathWaypointStarted(final PathModifier pPathModifier, final IEntity pEntity, final int 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:
                    //scene.setVisible(false);
                    player.animate(new long[]{200, 200, 200}, 9, 11, true);
                    break;
            }
        }


        public void onPathWaypointFinished(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {



        }


        public void onPathFinished(final PathModifier pPathModifier, final IEntity pEntity) {
            System.out.println("tileesdfsdfsdfgfdgdfsee"+player.getCurrentTileIndex() );
        }
    })));
















player_one.registerEntityModifier(new LoopEntityModifier(new PathModifier(5, path_one,    null, new IPathModifierListener() {

        public void onPathStarted(final PathModifier pPathModifier, final   IEntity pEntity) {

        }


        public void onPathWaypointStarted(final PathModifier pPathModifier,   final IEntity pEntity, final int 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);

                         System.out.println("insideeeeeeeeeeeeeeeeeeeeeeeee");
                    scene.detachChild(player_one);
                    scene.attachChild(player);
                    break;
                case 3:
                    player.animate(new long[]{200, 200, 200}, 9, 11, true);
                    break;
            }
        }


        public void onPathWaypointFinished(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {



        }


        public void onPathFinished(final PathModifier pPathModifier, final IEntity pEntity) {
            System.out.println("tileesdfsdfsdfgfdgdfsee"+player.getCurrentTileIndex() );
        }
    })));






    scene.attachChild(player);

    return scene;
}

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

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

请帮助我。提前致谢

最佳答案

如果你有三个点,你应该在你的 Path 构造函数中指定它:

final Path path = new Path(3).to(CAMERA_WIDTH - 58, 10).to(10, 10).to(10, CAMERA_HEIGHT/2f);

代替

final Path path = new Path(5).to(CAMERA_WIDTH - 58, 10).to(10, 10).to(10, CAMERA_HEIGHT/2f);

关于android - 在 AndEngine 中绘制路径以移动图像或 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11741078/

相关文章:

android - sprite碰撞检测中心区域sprite

android - 如何检查 Android 中的 Internet 连接是否有 wifi 连接?

java - Android 开机接收器崩溃

git - 如何获取包含子模块的 Git 存储库中所有文件的绝对路径?

android - 不同屏幕尺寸 Andengine Android 上的 Sprite 尺寸

android - 在非 Activity 类中使用传感器,Android/Andengine

android - 服务在 Activity 被销毁时停止

android - 如何使用子项实现可扩展的android抽屉导航?

python - 如何在Python中找到可执行文件的位置?

java - 如何使用Java的目录流仅获取目录内的文件/子目录而不是其他子目录