2012-01-08 39 views
2

我有这样的代码:开始动画与AndEngine动态壁纸extansion

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; 
    } 
} 

因此,如何在点击开始动画?我想制作一些像小卡通的东西。

+0

你想要什么类型的动画?你在这里没有任何动画精灵。 – Jong 2012-01-08 17:30:51

+0

我在OnUpdate方法中移动精灵。 – Divers 2012-01-10 06:16:22

回答

4

在你onLoadScene方法

mUpdateHandler.setEnabled(false); 

而且在onAreaTouched方法启用它。

mUpdateHandler.setEnabled(true); 

顺便说一句,我认为这不是很好的做法,创建随机例如每时间在onUpdate方法。

1

Here Josh描述了如何重写onTouch方法(并且引擎不能正确处理livewallpaper的触摸事件,因此您必须自己完成)。在几句话,你所要做的就是覆盖BaseWallpaperGLEngine类下面的函数(类是andEngine动态壁纸延伸的一部分:注册更新处理程序禁用后

@Override 
     public void onTouchEvent (MotionEvent event) 
     { 
     }