2012-09-07 41 views
2

我嘗試開發一個簡單的Flash遊戲,使用doublebuffer繪製動畫。使用doublebufferd畫布的簡單動畫不光滑

重繪時Event.ENTER_FRAME觸發發生。

用於doublebuffering的後備緩衝區是類型的BitmapData的。

動畫是很簡單的:我畫一個20×20像素的位圖與增加X後備緩衝座標,所以它應該從左邊到我的畫布的右側平滑地移動。這基本上可以正常工作,但如果仔細觀察,您會看到這一運動的重大幹擾。這似乎與幀速率沒有關係,因爲它一直超過60幀。順暢移動的中斷對於動畫是不可接受的,但我確信我沒有在雙緩衝中做任何錯誤,嚇得這可能是一個Flash播放器問題或東西...我會很放心,如果不是這種情況

請看看瑞士法郎顯示簡單的動畫:

https://dl.dropbox.com/u/55967135/test.swf

(順便說一句動畫的中斷也不當運動是基於兩個幀之間的時間消失 - 現在每幀其恆定2個像素)

我已經上傳了一個非常輕量級的Flash Builder項目包括整個源代碼上面的SWF: https://dl.dropbox.com/u/55967135/test.zip

public function enterFrame():void    
     { 

      // Calculate the time since the last frame (NOT USED IN THE EXAMPL PROGRAM)     
      var thisFrame : Date = new Date();    
      var dT : Number = (thisFrame.getTime() - lastFrame.getTime())/1000.0;    
      lastFrame = thisFrame;   

      // erase backBuffer 
      backBuffer.fillRect(backBuffer.rect, 0xFFFFFFFF); 


      // set new postion of the small testimage 
      if (this.pos > 600 || this.pos < 0) { 
       this.direction = !this.direction; 
      } 

      // increase/decrease vertical position 
      if (this.direction) { 
       this.pos += 2; 
      } else { 
       this.pos -= 2; 
      } 

      //trace(pos); 
      // draw small test image at postion "offset" 
      backBuffer.copyPixels( this.testGraphic.bitmap.bitmapData, 
             this.testGraphic.bitmap.bitmapData.rect, 
             new Point(pos, 0.0));    
     } 

的enterFrame事件()是我的課GraphicsController,它處理doublebuffering的方法。

 public function enterFrame(event:Event):void 
      { 
       GraphicsController.Instance.enterFrame();  
       myCanvas.graphics.clear(); 
       myCanvas.graphics.beginBitmapFill(GraphicsController.Instance.backBuffer, null, false, false);        
       myCanvas.graphics.drawRect(0, 0, this.width, this.height);        
       myCanvas.graphics.endFill(); 
       stage.invalidate(); 
      } 

幫助將不勝感激

謝謝

回答

1

奔跑完美流暢,我在Chrome,但我知道,你所描述的效果:它是由應用程序enterFrame事件(事件)方法啓動。我在Firefox的一個應用程序中體驗過它,但我使用顯示列表而不是位圖數據。我的問題是因爲將顯示對象移動了不到一個像素,閃光燈對此並不滿意。只要我將對象縮放到0.99就固定了。

你可以嘗試繪圖出DT和看它是否是穩定的。它應該是16.6666(或0.0166),因爲如果它正在波動,那麼以60fps的速度運行,則會導致輸入框事件被推回。

這可能是太有用:http://www.craftymind.com/2008/04/18/updated-elastic-racetrack-for-flash-9-and-avm2/

1

首先,你的後備緩衝區是罰款,是,你只是鎖定()之前對其進行更新,然後解鎖()它,你已經做了更新後。接下來,爲什麼要重繪圖形,何時可以使用Bitmap對象呢?所以,你保留GraphicsController.enterFrame()按原樣將在開頭和方法的代碼年底backBuffer.lock();backBuffer.unlock();電話,你在你的初始化階段,將在enterFrame事件通話過程中有自動更新使用一個位圖對象。應用程序的enterFrame()中的其他所有內容都將變得無用。

基本上我說的是將您的畫布從Shape類型更改爲類型位圖,該位圖將被硬連接到GraphicsController的backBuffer並將自動更新。