2012-01-08 73 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) 
     { 
     }