How to set PathModifier's coordinates randomly in start of LoopEntityModifier?

ぃ、小莉子 提交于 2019-12-11 06:48:55

问题


I created a live wallpaper service using AndEngine library. On screen there is a bird Sprite that flying repeatedly from the left to right. I'm using LoopEntityModifier and PathModifier for the solution. The bird is coded to start randomly on Y-position everytime it shows up from the left screen.

The code is like this:

public class MyLiveWallpaperService extends BaseLiveWallpaperService {
    private AnimatedSprite birdSprite;
    ...

    public Scene onLoadScene() {
        ...
        float[] coordY = generateRandomCoordY();  // my custom function to generate random array of Y-coordinates
        Path path = new Path(coordX, coordY);  // set the coordinate to Path object

        // register the modifiers (for the one who is curious, 1st argument of PathModifier is the duration, 
        // but it has nothing to do with the question)

        birdSprite.registerEntityModifier(new LoopEntityModifier(new PathModifier(10, path)));
        ...
    }
}

The problem is the Path's Y-coordinates value cannot be changed anymore when the LoopEntityModifier & PathModifier has run. I want everytime the loop started, I can set the new Path's Y-coordinate value again.


回答1:


I think you can get around this problem by overriding onModifierFinished() and creating a new PathModifier with the changed path. It would look something like this:

public LoopEntityModifier createModifier(Path path) {
    return new LoopEntityModifier(new PathModifier(path)) {
        @Override
        public void onModifierFinished(final IModifier<IEntity> pEntityModifier, final IEntity pEntity) {
            birdSprite.registerEntityModifier(createModifier(path));
        }
    }
}

birdSprite.registerEntityModifier(createModifier());

This only works if onModifierFinished() is called at the end of every loop.



来源:https://stackoverflow.com/questions/10244310/how-to-set-pathmodifiers-coordinates-randomly-in-start-of-loopentitymodifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!