2017-03-02 103 views
1

我正在用AndEngine GLES2創建遊戲。我正在嘗試在活動中加載圖像。但是每次運行應用程序時,它都會顯示出沒有錯誤的黑色方塊。以下是我的代碼:AndEngine顯示沒有錯誤的黑屏

import org.andengine.engine.camera.Camera; 
import org.andengine.engine.options.EngineOptions; 
import org.andengine.engine.options.ScreenOrientation; 
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy; 
import org.andengine.entity.scene.Scene; 
import org.andengine.entity.scene.background.Background; 
import org.andengine.entity.sprite.Sprite; 
import org.andengine.entity.util.FPSLogger; 
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas; 
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory; 
import org.andengine.opengl.texture.region.TextureRegion; 
import org.andengine.ui.activity.SimpleBaseGameActivity; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 

public class MainActivity extends SimpleBaseGameActivity { 




     private static final int CAMERA_WIDTH = 480; 
     private static final int CAMERA_HEIGHT = 640; 

     private BitmapTextureAtlas ourAtlas; 

     private TextureRegion ourCircle; 

     private Sprite circle; 

     @Override 
     public EngineOptions onCreateEngineOptions() { 
       final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); 

       return new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR, 
new RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT), camera); 
     } 

     @Override 
     protected void onCreateResources() { 


       ourAtlas = new BitmapTextureAtlas(this.getTextureManager(),256,512); 

       ourCircle = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.ourAtlas, 
this, "gfx/rectangle.png",0,0); 

     mEngine.getTextureManager().loadTexture(ourAtlas); 
     } 

     @Override 
     protected Scene onCreateScene() { 

       this.mEngine.registerUpdateHandler(new FPSLogger()); 


       final Scene mScene = new Scene(); 


       circle = new Sprite(32, 32, ourCircle, this.getVertexBufferObjectManager()); 
     mScene.attachChild(circle); 
       mScene.setBackground(new Background(1.0f,1.0f,1.0f)); 



       return mScene; 
     } 



} 

該活動完美無缺地運行。但屏幕中間顯示的是黑色方塊。我不明白爲什麼圖像沒有顯示。以下是該活動的截圖。

Click on this link to see the screenshot from my activity

我會喜歡它,如果有人會來看看什麼是錯的。我究竟做錯了什麼?

回答

0

我找到了解決方案。

ourAtlas = new BitmapTextureAtlas(this.getTextureManager(),256,512); 

在上面的行中,我必須將地圖集的寬度和高度更改爲1024,1024,並且它完美地工作。問題出在我的身上。

0

嘗試加載紋理就像這樣:

public void onCreateResources() { 

    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); 


    this.mBuildableBitmapTextureAtlas = new BuildableBitmapTextureAtlas(this.getTextureManager(), 512, 512); 

    this.yourTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBuildableBitmapTextureAtlas, this, "yourimage.png"); 


    try { 

     this.mBuildableBitmapTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0)); 

     this.mBuildableBitmapTextureAtlas.load(); 

    } catch (TextureAtlasBuilderException e) { 

     Debug.e(e); 

    } 

} 

也可以嘗試先設定背景,然後裝上孩子。