使用AndEngine Live Wallpaper扩展程序启动动画

时间:2012-01-08 10:07:03

标签: android animation andengine live-wallpaper

我有这样的代码:

public class SnowFallService extends BaseLiveWallpaperService implements IOnAreaTouchListener{

    // ===========================================================
    // Constants
    // ===========================================================

    private static final int CAMERA_WIDTH = 480;
    private static final int CAMERA_HEIGHT = 800;

    private ArrayList<Sprite> allSnow = new ArrayList<Sprite>();
    // ===========================================================
    // Fields
    // ===========================================================

    private ScreenOrientation screenOrientation;

    private static TextureRegion snowTexture;
    private static TextureRegion backgroundTexture;
    private static Textures texture = null;

    private Scene mScene;

    public org.anddev.andengine.engine.Engine onLoadEngine() {
        return new org.anddev.andengine.engine.Engine(new EngineOptions(true, this.screenOrientation, new FillResolutionPolicy(), new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT)));
    }

    public void onLoadResources() {
        texture = new Textures(this, getEngine());
    }

    public void onUnloadResources() {

    }

    public Scene onLoadScene() {

        final Scene mScene = new Scene();
        backgroundTexture = texture.getBackground();
        mScene.attachChild(new Sprite(0, 0, backgroundTexture));
        snowTexture = texture.getSnowTextureRegion();

        mScene.registerUpdateHandler(new IUpdateHandler() {
            private long lastRaindropAdd = 0;

            @Override
            public void onUpdate(final float pSecondsElapsed) {
                int size = allSnow.size();
                int tmpInt = 0;
                Random randGen = new Random();
                for (int i = 0; i < size; i++) {
                    if (allSnow.get(i) != null){
                        Sprite snow = allSnow.get(i);

                        tmpInt = randGen.nextInt(4);
                        snow.setPosition(snow.getX() + (randGen.nextInt(5) - randGen.nextInt(5)) * randGen.nextInt(3), snow.getY() + tmpInt);

                        if (snow.getY() > CAMERA_HEIGHT || snow.getX() > CAMERA_WIDTH) {
                            synchronized(snow) {
                                size--;
                                allSnow.remove(i);
                                mScene.detachChild(snow);
                            }
                        }
                    }
                }

                tmpInt = randGen.nextInt(5000);

                if (System.currentTimeMillis() - lastRaindropAdd  > tmpInt) {
                    lastRaindropAdd = System.currentTimeMillis();

                    tmpInt = randGen.nextInt(CAMERA_WIDTH);

                    Sprite snow = getRaindrop(tmpInt, 0);
                    allSnow.add(snow);
                    mScene.attachChild(snow);
                }
            }

            @Override
            public void reset() {
            }
        });
        return mScene;
    }

    public void onLoadComplete() {
        // TODO Auto-generated method stub

    }

    public void onPauseGame() {
        // TODO Auto-generated method stub

    }

    public void onResumeGame() {
        // TODO Auto-generated method stub

    }

    public Sprite getRaindrop(float x, float y) {
        return (new Sprite(x, y, snowTexture.deepCopy()));
    }

    @Override
    public boolean onAreaTouched(TouchEvent pSceneTouchEvent,ITouchArea pTouchArea, float pTouchAreaLocalX, float pTouchAreaLocalY) {
        if(pSceneTouchEvent.isActionDown()) {
            // HERE I WANT PLACE CODE, THAT WILL START ANIMATION.
            return true;
        }
        return false;
    }
}

那么如何点击动画呢?我想制作类似小卡通的东西。

2 个答案:

答案 0 :(得分:4)

在注册更新处理程序后的onLoadScene方法中禁用它。

mUpdateHandler.setEnabled(false);

在onAreaTouched方法中启用它。

mUpdateHandler.setEnabled(true);

顺便说一句我认为每次在onUpdate方法中创建Random实例都不是好习惯。

答案 1 :(得分:1)

Here Josh描述了如何覆盖onTouch方法(andEngine无法正确处理livewallpaper的触摸事件,因此您必须自己完成)。简而言之,您所要做的就是覆盖BaseWallpaperGLEngine类中的以下函数(类是andEngine动态壁纸扩展的一部分:

@Override
        public void onTouchEvent (MotionEvent event)
        {
        }