2011-12-10 223 views
2

我有一個使用Android畫布創建的動態壁紙。經過測試,我覺得有必要利用OpenGL的力量,所以我正在試驗AndEngine。我想知道如何實現以下目標。AndEngine更新屏幕

我有填滿整個屏幕,與許多較小的位圖浮在上面(不是動畫運動)

到目前爲止,我有這樣的背景圖像的背景圖像:

@Override 
public void onLoadResources() 
{ 
    mtexture = new Texture(1024, 1024, TextureOptions.BILINEAR); 
    TextureRegionFactory.setAssetBasePath("gfx/"); 
    mtextureRegion = TextureRegionFactory.createFromResource(mtexture , this, R.drawable.background1, 0, 0); 
    this.mEngine.getTextureManager().loadTexture(this.mtexture); 
} 

@Override 
public Scene onLoadScene(){ 
    final Scene scene = new Scene(1); 

    Sprite background = new Sprite(0, 0, CAMERA_WIDTH*2, CAMERA_HEIGHT, mtextureRegion) 
    SpriteBackground sb = new SpriteBackground(background); 
    scene.setBackground(sb); 
    scene.setBackgroundEnabled(true); 
    return scene; 
} 

這適用於背景,但我需要移動精靈。

在我的畫布的代碼,我做了以下更新位置的移動物體的&物理和畫在畫布上每隔幾毫秒

 private final Runnable drawScreen = new Runnable() { 
     public void run() { 
      drawFrame(); 
     }}; 

-

void drawFrame() { 
     final SurfaceHolder holder = getSurfaceHolder(); 

     Canvas c = null; 
     try { 
      c = holder.lockCanvas(); 
      if (c != null) { 
        //draw 
      } 
     } finally { 
      if (c != null) holder.unlockCanvasAndPost(c); 
     } 

     mHandler.removeCallbacks(drawScreen); 
     mHandler.postDelayed(drawScreen, 10); 
    } 

什麼是適當的方式在AndEngine上做到這一點?我是否使用相同的代碼並替換openGL調用?

我看看GLEngine,我應該發送Runnables到GlThread隊列嗎?

編輯 - 我想我找到了答案...一個UpdateHandler。但是,我怎樣才能通知處理程序的更新(即調用onUpdate方法)。如果我定時處理Handler,如果我經常打電話,會發生什麼情況,是否會建立一系列請求?

回答

3

首先,不要使用構造函數Scene(int),它已被棄用。改爲使用Scene()

正確,您應該使用更新處理程序。 您可以創建一個UpdateHandler,然後將其註冊到您的場景:

scene.registerUpdateHandler(mUpdateHandler); 

這樣,在mUpdateHandler.onUpdate方法的代碼每次執行現場更新(每一幀)。你不要手動調用它。如果你想停止它,請致電:

scene.unregisterUpdateHandler(mUpdateHandler); 

所以,onUpdate方法是總是在UpdateThread執行,所以你可以肯定,你可以做實體想要有任何改變。所以你可以四處移動,你想要的精靈等等......

順便說一下,爲什麼背景的寬度是CAMERA_WIDTH*2?這意味着只顯示你的精靈的左半部分。如果你不打算移動相機,那麼右半部分將不會顯示。