如何在AndEngine中创造飘落的雪花?

时间:2012-01-06 13:15:48

标签: android andengine

我有这段代码:

public Scene onLoadScene() {
    Random randomGenerator = new Random();
    pX = randomGenerator.nextInt(CAMERA_WIDTH);
    Sprite snow = new Sprite (pX, 1, 30, 30, mTextureSnowRegion);
    scene.getLastChild().attachChild(snow);
    return scene;
}

我正在努力降雪。我试图使用MoveModifier,但没有任何工作。 请帮忙。

4 个答案:

答案 0 :(得分:3)

答案 1 :(得分:1)

public Scene onLoadScene() {
Random randomGenerator = new Random();
pX = randomGenerator.nextInt(CAMERA_WIDTH);
Sprite snow = new Sprite (pX, 1, 30, 30, mTextureSnowRegion);
scene.getLastChild().attachChild(snow);
return scene;

这看起来不错。您只需添加MoveYModifer而不是MoveModifier。

而且你也应该在AndEngine中使用GenericPool,因为它使用了大量的内存来继续创建新的Sprite实例并附加它。当精灵离开屏幕时也不要忘记分离它。

看看这个

答案 2 :(得分:1)

在我的游戏中使用此粒子代码创建雪。我的游戏使用800x480相机。

    final RectangleParticleEmitter particleEmitter = new RectangleParticleEmitter(184.0f,44.0f,340,60);
    final ParticleSystem particleSystem = new ParticleSystem(particleEmitter, 100, 200, 360, this.mParticleTextureRegion);


    particleSystem.addParticleInitializer(new ColorInitializer(1, 1, 1));
    particleSystem.addParticleInitializer(new AlphaInitializer(0));
    particleSystem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
    particleSystem.addParticleInitializer(new VelocityInitializer(-200, 200, -200, 200));
    particleSystem.addParticleInitializer(new RotationInitializer(0.0f, 360.0f));

    particleSystem.addParticleModifier(new ScaleModifier(1.0f, 1.2f, 0, 5));
    particleSystem.addParticleModifier(new ColorModifier(1, 0.98f, 1, 0.96f, 1, 0.82f, 0, 3));
    particleSystem.addParticleModifier(new ColorModifier(1, 1, 0.5f, 1, 0, 1, 4, 6));
    particleSystem.addParticleModifier(new org.anddev.andengine.entity.particle.modifier.AlphaModifier(0, 1, 0, 1));
    particleSystem.addParticleModifier(new org.anddev.andengine.entity.particle.modifier.AlphaModifier(1, 0, 5, 6));
    particleSystem.addParticleModifier(new ExpireModifier(3, 6));

答案 3 :(得分:1)

我正在使用类似的粒子系统设置为@UncleIstvan。

final BatchedPseudoSpriteParticleSystem particleSystem = new BatchedPseudoSpriteParticleSystem(
    new RectangleParticleEmitter(CAMERA_WIDTH / 2, CAMERA_HEIGHT, CAMERA_WIDTH, 1),
    2, 5, 100, mSnowParticleRegion, 
    this.getVertexBufferObjectManager()
  );
particleSystem.setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE);
particleSystem.addParticleInitializer(new VelocityParticleInitializer<Entity>(-3, 3, -20, -40));
particleSystem.addParticleInitializer(new AccelerationParticleInitializer<Entity>(-3, 3, -3, -5));
particleSystem.addParticleInitializer(new RotationParticleInitializer<Entity>(0.0f, 360.0f));
particleSystem.addParticleInitializer(new ExpireParticleInitializer<Entity>(10f));
particleSystem.addParticleInitializer(new ScaleParticleInitializer<Entity>(0.2f, 0.5f));


particleSystem.addParticleModifier(new AlphaParticleModifier<Entity>(6f, 10f, 1.0f, 0.0f));

scene.attachChild(particleSystem);

但是我为每个粒子添加了一个实体修饰符:

particleSystem.addParticleInitializer(new RegisterXSwingEntityModifierInitializer<Entity>(10f, 0f, (float) Math.PI * 8, 3f, 25f, true));

它需要一个自定义粒子初始化器。在初始化程序中,我为每个粒子注册了一个新的修饰符:

@Override
public void onInitializeParticle(Particle<T> pParticle) {
        pParticle.getEntity().registerEntityModifier(
                new PositionXSwingModifier(mDuration, 
                        mFromValue, mToValue, 
                        mFromMagnitude, mToMagnitude));

}

最后一部分是使用增长的正弦波来创建摆动运动的修改器(某些部分被忽略):

public class PositionXSwingModifier extends SingleValueSpanEntityModifier {

  public PositionXSwingModifier(float pDuration, float pFromValue, float pToValue,
        float pFromMagnitude, float pToMagnitude) {
    // fromValue is usually 0
    // toValue means how many times will the sine wave oscillate
    // every 2pi is full sin wave
    super(pDuration, pFromValue, pToValue);
    mFromMagnitude = pFromMagnitude;
    mToMagnitude = pToMagnitude;
  }

  @Override
  protected void onSetValue(IEntity pItem, float pPercentageDone, float pValue) {
    // current magnitude based on percentage
    float currentMagnitude = mFromMagnitude + (mToMagnitude - mFromMagnitude) * pPercentageDone;
    // current sine wave value
    float currentSinValue = (float) Math.sin(pValue);
    // change the x position of the flake
    pItem.setX(mInitialX + currentMagnitude * currentSinValue);
  }

}

部分基于我的问题:https://gamedev.stackexchange.com/questions/56475/how-to-simulate-feather-fall-in-box2d

你可以获得完整的代码和APK来试用here