我是AndEngine的新手,我打算做一个游泳鱼游戏。 我需要随机生成水母和鲨鱼作为鱼类需要避免的敌人。
我的问题是每当我运行它时,会出现多个水母而不是只有一只水母正在移动,因为我希望它能够被动画化。 我在网上看到了一个代码并将其应用到我的代码中,在这里:
// Spawning jellyfish, triggers every 5 seconds
private void createJellyfishSpawnTimeHandler() {
TimerHandler spriteTimerHandler;
float mEffectSpawnDelay = 5f;
spriteTimerHandler = new TimerHandler(mEffectSpawnDelay, true, new ITimerCallback() {
@Override
public void onTimePassed(TimerHandler pTimerHandler) {
addJellyfish();
}
});
getEngine().registerUpdateHandler(spriteTimerHandler);
}
// Loading jellyfish resources
private void loadJellyfishResources(){
this.animatedBitmapTextureAtlas = new BuildableBitmapTextureAtlas(this.getTextureManager(), 512, 256,TextureOptions.NEAREST);
this.jellyfishTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.animatedBitmapTextureAtlas, this, "jellyfish_tiled.png", 4, 1);
try {
this.animatedBitmapTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 1));
this.animatedBitmapTextureAtlas.load();
} catch (TextureAtlasBuilderException e) {
Debug.e(e);
}
jellyfishToBeAdded = new LinkedList<Sprite>();
}
// adds a jellyfish at a random location and let it move along the x-axis
private void addJellyfish() {
// for location
int x = (int) (camera.getWidth() + jellyfishTextureRegion.getWidth());
int y = rand.nextInt(high-low) + low;
jellyfishTarget = new AnimatedSprite(x, y, jellyfishTextureRegion.deepCopy(), this.getVertexBufferObjectManager());
jellyfishTarget.animate(250);
scene.attachChild(jellyfishTarget);
int actualDuration = 15;
MoveXModifier mod = new MoveXModifier(actualDuration, jellyfishTarget.getX(), -jellyfishTarget.getWidth());
jellyfishTarget.registerEntityModifier(mod.deepCopy());
jellyfishToBeAdded.add(jellyfishTarget);
}