Search code examples
androidandenginelive-wallpaper

Sprite not moving with PathModifier using Andengine


I am build a live wallpaper and all I would like is for, right now, a simple sprite to move right and that's all. I have no animation for the sprite. The wallpaper loads and I see the sprite but it's just not moving. I've implemented the PathModifer and 0 errors, I've checked my code to numerous examples and posts and I just cannot figure out why it is not moving.

Here is my entire class:

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

    }
}

Any help anyone has would be most appreciated, thank you.


Solution

  • Your code works. It runs on my device. The problem seems to be its moving to little and too slow to easily notice. Try changing your duration from 30 to 5.

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

    I also spaced out the path coordinates more to make the move more noticeable.

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

    Now its clearly moving.

    Here is my full class:

    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;
        }
    
    }