Android : AndEngine AnimatedSprite do not animate

on Saturday, October 11, 2014


I have a small problem with my AndEngine code. I've created an AnimatedSprite and wanted to animate it but the screen just shows a single image without moving. Any help would be appreciated. This is my code



import org.andengine.engine.Engine;
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.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.util.debug.Debug;

import android.content.Context;

public class ResourceManager {
private static ResourceManager INSTANCE;

public BuildableBitmapTextureAtlas mBitmapTextureAtlas;

public TiledTextureRegion mRunningCatTextureRegion;

ResourceManager() {
// TODO Auto-generated constructor stub
}

public synchronized static ResourceManager getInstance() {
if (INSTANCE == null)
INSTANCE = new ResourceManager();

return INSTANCE;
}

public synchronized void loadGameTextures(Engine pEngine, Context pContext) {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

mBitmapTextureAtlas = new BuildableBitmapTextureAtlas(
pEngine.getTextureManager(), 2048, 2048,
TextureOptions.BILINEAR);

mRunningCatTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createTiledFromAsset(mBitmapTextureAtlas, pContext,
"runningcat.png", 2, 4);

try {
mBitmapTextureAtlas
.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
0, 0, 0));
mBitmapTextureAtlas.load();
} catch (TextureAtlasBuilderException e) {
Debug.e(e);
}

}

public synchronized void unloadGameTextures() {
BuildableBitmapTextureAtlas mBitmapTextureAtlas = (BuildableBitmapTextureAtlas) mRunningManTextureRegion
.getTexture();
mBitmapTextureAtlas.unload();

System.gc();

}
}


and MainActivity class



import java.io.IOException;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
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.AnimatedSprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.ui.activity.BaseGameActivity;

public class MainActivity extends BaseGameActivity {
private final int CAMERA_WIDTH = 720;
private final int CAMERA_HEIGHT = 480;

private AnimatedSprite mRunningCatSprite;

@Override
public EngineOptions onCreateEngineOptions() {
// TODO Auto-generated method stub
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED,
new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT))
.setWakeLockOptions(WakeLockOptions.SCREEN_ON);

}

@Override
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)
throws IOException {
// TODO Auto-generated method stub
ResourceManager.getInstance().loadGameTextures(this.mEngine, this);

pOnCreateResourcesCallback.onCreateResourcesFinished();
}

@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
throws IOException {
// TODO Auto-generated method stub

Scene scene = new Scene();
scene.registerUpdateHandler(new FPSLogger());
scene.setBackground(new Background(0.2f, 0.3f, 0.8f, 0.5f));

mRunningCatSprite = new AnimatedSprite(300, 300,
ResourceManager.getInstance().mRunningCatTextureRegion,
this.getVertexBufferObjectManager());
mRunningCatSprite.animate(new long[] { 200, 200, 200, 200, 200, 200,
200, 200 }, 0, 7, true);
scene.attachChild(mRunningCatSprite);

pOnCreateSceneCallback.onCreateSceneFinished(scene);

}

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

}

}


Is there any problem with my code?


0 comments:

Post a Comment