2013-04-04 76 views
2

代碼:AndEngine - 精靈不會在不同設備上縮放

這是如何初始化攝像頭。
我',試圖獲得設備的分辨率和基於我設置相機的寬度和高度。

 public class GameActivity extends SimpleBaseGameActivity { 

     private SmoothCamera mCamera; 
     private DisplayMetrics dM; 

     private int CAMERA_WIDTH, CAMERA_HEIGHT; 

     private double ScreenWidth, 
         ScreenHeight, 
         resolutionRatio; 


     public EngineOptions onCreateEngineOptions() { 

      //set Camera 
      getDeviceResolution(); 
      setCamera(); 


     EngineOptions options = new EngineOptions(true, 
      ScreenOrientation.LANDSCAPE_FIXED, 
      new RatioResolutionPolicy((int)this.getCameraWidth(),(int)this.getCameraHeight()), 
      //new FillResolutionPolicy(), 
      mCamera); 
      return options; 

     } 

     private void getDeviceResolution() { 
      dM = new DisplayMetrics(); 
      this.getWindowManager().getDefaultDisplay().getMetrics(dM); 
      this.ScreenWidth = dM.widthPixels; 
      this.ScreenHeight = dM.heightPixels; 
      this.resolutionRatio = this.ScreenWidth/this.ScreenHeight; 

       resolutionRatio = (double)Math.round(resolutionRatio * 100)/100; 

      Log.d("Resolution","ScrennWidth: "+this.ScreenWidth); 
      Log.d("Resolution","ScrennHeight: "+this.ScreenHeight); 
        Log.d("Resolution","Resolution Ratio: " + this.resolutionRatio); 
     } 



    private void setCamera() { 

     if(resolutionRatio == 1.66){ 
      this.setCameraHeight(340); 
      this.setCameraWidth(400); 
     }else if(resolutionRatio == 2.13){ 
      this.setCameraHeight(480); 
      this.setCameraWidth(1024); 
     }else if(resolutionRatio == 1.77){ 
      this.setCameraHeight(720); 
      this.setCameraWidth(1280); 
     }else if(resolutionRatio == 1.5){ 
      this.setCameraHeight(320); 
      this.setCameraWidth(480); 
     }else if(resolutionRatio == 1.67){ 
      this.setCameraHeight(480); 
      this.setCameraWidth(800); 
     }else { 
      this.setCameraHeight((int) this.ScreenHeight); 
      this.setCameraWidth((int) this.ScreenWidth); 
     } 

     // Create a Camera 
     this.mCamera = new SmoothCamera(0,0,(int)getCameraWidth(),(int)getCameraHeight(),100,100,1.0f); 
      mCamera.setZoomFactor(1.0f); 

      mCamera.setBoundsEnabled(true); 
      mCamera.setBounds(0, 0, mCamera.getWidth(), mCamera.getHeight()); 
     } 



     } 

問題是。

我已經在480x320分辨率的設備上玩遊戲; enter image description here

,當我與800X480 enter image description here

分辨率上設備嘗試相同的代碼本人認爲精靈沒有得到較高分辨率的設備縮放。
據我所知,andengine本身就會縮放攝像頭和精靈。

那麼爲什麼精靈不能在這種情況下縮放呢?

我應該怎麼做才能根據分辨率放大精靈?

我也試過FillResolutionPolicy。一樣。

我正在使用andengine和SpriteSheets的TexturePackerExtension。

回答

4

你想要一個Camera與固定的widthheight是由設備屏幕aspectratio計算。將它與FillResolutionPolicy配對,就可以得到你想要的。

注:

AndEngine不規模Sprites你,但你設置Camera使整個Scene出現縮放。

+0

FillResolutionPolicy並修復相機大小組合工作! :d – 2013-04-09 12:00:00

2

即使在您的示例中,AndEngine也會縮放您的Sprites。結果不符合您的預期的原因是您手動設置相機的widthheight(根據分辨率)。

如果你真的想在每一個顯示器上以同樣的方式按比例每一個角色,那麼就應該選擇一個在你創建你的遊戲固定的分辨率。例如,您可以設置每個精靈和每個位置的分辨率爲960x640。

// skip the whole if(resolutionRatio == 1.66){.. part 
this.mCamera = new SmoothCamera(0, 0, 960f, 640f, 100, 100, 1.0f); 

AndEngine然後將縮放相機,以便儘可能多地顯示相機。

public EngineOptions onCreateEngineOptions() { 
     EngineOptions options = new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR, new RatioResolutionPolicy(1.5f), camera; 
     return options; 
} 

1.5f是960x640分辨率的適配縱橫比。 現在你不必再照顧不同的顯示器尺寸。

+0

這並不能解決問題。在不同分辨率的設備上嘗試使用時,相機的左側和右側都會出現白色條紋。 – 2013-04-04 20:19:02

+1

這些條紋是正常的,AndEngine試圖填充大部分的顯示內容而不拉伸圖像(通過保持固定的高寬比)。無論如何,如果你想使用不同的分辨率(比如你通過檢測顯示度量來做你的例子......),你需要手動調整你的Sprites。在您的800x480示例中,顯示卡比480x320示例中顯示的要小,因爲相機較大,但顯示卡保持其原始大小。但是如果你不想縮放和放置每個精靈,使用一個固定的相機尺寸,讓AndEngine完成剩下的工作。 – GameDroids 2013-04-04 20:35:57

+1

另外 - 由於白色條紋通常不受歡迎,因此可以使用應用程序的主題屬性將它們更改爲您選擇的任何顏色,例如黑色。 http://stackoverflow.com/questions/11779679/setting-android-theme-background-color。 更改您的比率ResolutionPolicy也將擺脫酒吧,但取決於您的佈局可能會導致其他顯示問題,因爲一個設備上可見的一些屏幕在另一個設備上不可見。 – 2013-04-04 20:41:40