1

我正在构建一个动态壁纸,我现在想要的只是一个简单的向右移动的精灵,仅此而已。我没有精灵的动画。壁纸加载,我看到了精灵,但它只是不动。我已经实现了 PathModifer 和 0 个错误,我已经检查了我的代码以查看大量示例和帖子,但我无法弄清楚它为什么没有移动。

这是我的整个班级:

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.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.ui.livewallpaper.BaseLiveWallpaperService;
import org.andengine.input.sensor.acceleration.AccelerationData;
import org.andengine.input.sensor.acceleration.IAccelerationListener;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.util.debug.Debug;
import org.andengine.util.modifier.ease.EaseSineInOut;

public class LiveWallpaperService extends BaseLiveWallpaperService implements IAccelerationListener{

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

    private static int CAMERA_WIDTH = 480;
    private static int CAMERA_HEIGHT = 854;

    private Scene mScene;

    private BitmapTextureAtlas grass;
    private ITextureRegion yourTextureRegion;

    private BitmapTextureAtlas house;
    private ITextureRegion houseRegion;

    private BitmapTextureAtlas cloud1;
    private ITextureRegion cloud1Region;

    @Override
    public EngineOptions onCreateEngineOptions() {
      Camera mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
              EngineOptions options = new EngineOptions(true,
                      ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(
                              CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);
              return options;

     }

    @Override
    public void onCreateResources(
            OnCreateResourcesCallback pOnCreateResourcesCallback)
            throws Exception {
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

        this.grass = new BitmapTextureAtlas(this.getTextureManager(), 1024, 512, TextureOptions.DEFAULT);
        yourTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.grass, this, "bglwp.png", 0, 0);
        this.mEngine.getTextureManager().loadTexture(this.grass);

        this.cloud1 = new BitmapTextureAtlas(this.getTextureManager(), 256, 128, TextureOptions.DEFAULT);
        cloud1Region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.cloud1, this, "clld1.png", 0, 0);
        this.mEngine.getTextureManager().loadTexture(this.cloud1);

        this.house = new BitmapTextureAtlas(this.getTextureManager(), 512, 512, TextureOptions.DEFAULT);
        houseRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.house, this, "front.png", 0, 0);
        this.mEngine.getTextureManager().loadTexture(this.house);


        pOnCreateResourcesCallback.onCreateResourcesFinished();


    }

    @Override
    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws Exception {
        final float x = CAMERA_WIDTH / 2 - yourTextureRegion.getWidth() /2;
        final float y = CAMERA_HEIGHT / 2 - yourTextureRegion.getHeight() /2;
        final float xx = CAMERA_WIDTH / 2 - houseRegion.getWidth() /2;
        final float yy = CAMERA_HEIGHT / 2 - houseRegion.getHeight() /2;

         mScene= new Scene();
         mScene.setBackground(new Background(0.3294117f, 0.788235f, 0.909803f));
         Sprite backgroundSprite = new Sprite(x - 80, y + 171, this.yourTextureRegion, getVertexBufferObjectManager());
         mScene.attachChild(backgroundSprite);

         Sprite foregroundSprite = new Sprite(xx + 10, yy + 80, this.houseRegion, getVertexBufferObjectManager());
         mScene.attachChild(foregroundSprite);

         Sprite cloud1Sprite = new Sprite( 100, 100, this.cloud1Region, getVertexBufferObjectManager());

         final Path path = new Path(2).to(200, 200).to(20, 20);

         cloud1Sprite.registerEntityModifier(new LoopEntityModifier(
                 new PathModifier(5, path, null, new IPathModifierListener() {
                @Override
                public void onPathStarted(final PathModifier pPathModifier, final IEntity pEntity) {
                    Debug.d("onPathStarted");
                }

                @Override
                public void onPathWaypointStarted(PathModifier pPathModifier,
                        IEntity pEntity, int pWaypointIndex) {

                }

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

                }

                @Override
                public void onPathFinished(PathModifier pPathModifier,
                        IEntity pEntity) {
                }
                 }, EaseSineInOut.getInstance())));

         mScene.attachChild(cloud1Sprite);
         pOnCreateSceneCallback.onCreateSceneFinished(mScene);
         }

    @Override
    public void onPopulateScene(Scene pScene,
            OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAccelerationAccuracyChanged(AccelerationData pAccelerationData) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAccelerationChanged(AccelerationData pAccelerationData) {
        // TODO Auto-generated method stub

    }
}

任何人的帮助将不胜感激,谢谢。

4

1 回答 1

0

您的代码有效。它在我的设备上运行。问题似乎在于它的移动太小而且太慢而无法轻易注意到。尝试将持续时间从 30 更改为 5。

new PathModifier(5, path, null, new IPathModifierListener();

我还将路径坐标间隔更多,以使移动更明显。

 final Path path = new Path(2).to(200, 200).to(0, 0);

现在它显然在移动。

这是我的完整课程:

package com.example.andenginetestbed;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

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.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.adt.io.in.IInputStreamOpener;
import org.andengine.util.color.Color;
import org.andengine.util.debug.Debug;
import org.andengine.util.modifier.ease.EaseSineInOut;

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 android.os.Bundle;
import android.view.Menu;

public class TestBed extends SimpleBaseGameActivity {

    // ===========================================================
    // Constants
    // ===========================================================

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

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

    private ITexture mTexture;
    private ITextureRegion mFaceTextureRegion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_test_bed);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.test_bed, menu);
        return true;
    }

    @Override
    public EngineOptions onCreateEngineOptions() {
        final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

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

    @Override
    public void onCreateResources() {
        try {
            this.mTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
                @Override
                public InputStream open() throws IOException {
                    return getAssets().open("tank.png");
                }
            });

            this.mTexture.load();
            this.mFaceTextureRegion = TextureRegionFactory.extractFromTexture(this.mTexture);
        } catch (IOException e) {
            Debug.e(e);
        }
    }
    ArrayList<Sprite> placedSprites = new ArrayList<Sprite>();
    @Override
    protected Scene onCreateScene() {
        this.mEngine.registerUpdateHandler(new FPSLogger());

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

        /* Calculate the coordinates for the face, so its centered on the camera. */
        final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
        final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;

        final Sprite bigTank = new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
        scene.attachChild(bigTank);

        final Sprite smallTank = new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
        smallTank.setScaleCenter(smallTank.getWidth()/2, smallTank.getHeight()/2 );
        smallTank.setScale(0.667f);
        smallTank.setRotationCenter(smallTank.getWidthScaled() / 2,
        smallTank.getHeightScaled() / 2);
        smallTank.setRotation(45);
        scene.attachChild(smallTank);

         final Sprite cloud1Sprite = smallTank;

         final Path path = new Path(2).to(200, 200).to(0, 0);

         cloud1Sprite.registerEntityModifier(new LoopEntityModifier(
                 new PathModifier(10, path, new IPathModifierListener() {
                @Override
                public void onPathStarted(final PathModifier pPathModifier, final IEntity pEntity) {
                    Debug.d("onPathStarted");
                }

                @Override
                public void onPathWaypointStarted(PathModifier pPathModifier,
                        IEntity pEntity, int pWaypointIndex) {

                }

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

                }

                @Override
                public void onPathFinished(PathModifier pPathModifier,
                        IEntity pEntity) {
                }
                 }, EaseSineInOut.getInstance())));
        return scene;
    }

}
于 2013-06-21T21:28:08.083 回答