2011-09-13 66 views
0

幾個月前,我看了一下andengine,並且設法做出了一些東西。 現在我下載了最新版本,並且我正在爲最簡單的事情發生崩潰。在這裏我的代碼BitmapTextureAtlasTextureRegionFactory.createFromAsset crash

package francesco.mygame; 

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



public class mainMenu extends BaseGameActivity 
{ 

// =========================================================== 

// Constants 

// =========================================================== 
static final int CAMERA_WIDTH = 480; 

static final int CAMERA_HEIGHT = 320; 



//private static final String TAG = "Main Menu"; 


// =========================================================== 

// Fields 

// =========================================================== 

protected Camera mCamera; 
protected BitmapTextureAtlas mTexture; 
protected TextureRegion mPlayTexture; 
protected Sprite mPlaySprite; 
protected Sprite mQuitSprite; 
protected TextureRegion mQuitTexture; 
protected Scene mMainScene; 

// =========================================================== 

// Constructors 

// =========================================================== 



// =========================================================== 

// Getter & Setter 

// =========================================================== 



// =========================================================== 

// Methods for/from SuperClass/Interfaces 

// =========================================================== 



@Override 

public void onLoadComplete() 
{ 

// TODO Auto-generated method stub 


} 



@Override 

public Engine onLoadEngine() 
{ 

    this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); 

    return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera)); 

} 



@Override 

public void onLoadResources() 
{ 
    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); 

    this.mTexture = new BitmapTextureAtlas(64, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA); 
    this.mPlayTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mTexture, this, "play.png", 0, 0); 
    this.mQuitTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mTexture, this, "quit.png", 0, 40); 

    this.mEngine.getTextureManager().loadTexture(this.mTexture); 

} 



@Override 

public Scene onLoadScene() 
{ 

    this.mEngine.registerUpdateHandler(new FPSLogger()); 
    this.mMainScene = new Scene(); 
    this.mMainScene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f)); 

    int play_x = (CAMERA_WIDTH + this.mPlayTexture.getWidth())/2 ; 
    int play_y = (CAMERA_HEIGHT + this.mPlayTexture.getHeight())/2 - this.mPlayTexture.getHeight(); 
    this.mPlaySprite = new Sprite(play_x, play_y, this.mPlayTexture); 

    int quit_x = (CAMERA_WIDTH + this.mQuitTexture.getWidth())/2 ; 
    int quit_y = (CAMERA_HEIGHT + this.mQuitTexture.getHeight())/2 + this.mQuitTexture.getHeight(); 
    this.mQuitSprite = new Sprite(quit_x, quit_y, this.mQuitTexture); 

    this.mMainScene.attachChild(mPlaySprite); 
    this.mMainScene.attachChild(mQuitSprite); 

    return this.mMainScene; 

} 



// =========================================================== 

// Methods 

// =========================================================== 



// =========================================================== 

// Inner and Anonymous Classes 

// =========================================================== 

} 

(通常它是很好縮進)

的logcat的錯誤是

在AssetBitMapTextureAtlasSource無法載入位圖。資產路徑:GFX/play.png

java.io.FileNoFoundException:GFX/play.png

但我把2個圖像的文件夾中,我加入他們的項目像我一樣,當我第一次嘗試andengine(和它的工作).. 所以我不真正明白問題所在。

圖片是用gimp(一個是57x36和其他58x31)創建的2 png。

回答

6

如果找不到文件,則會拋出java.io.FileNoFoundException - 因此除了引擎找不到圖像外,沒有其他可能性。

確保您的圖像位於:assets/gfx/,而不僅僅是在名爲gfx/的文件夾中。

+1

-.-「明顯的錯誤..我真的覺得很蠢..謝謝.. –

3

確保有資產的文件夾在您的項目,並從的Buildpath選項(項目選項,你可以在你的項目資源管理器中右鍵單擊訪問),從源選項卡按添加文件夾按鈕,檢查資產文件夾導入編譯過程。

gfx文件夾必須位於資產文件夾中。如果沒有這樣的文件夾和所有圖像是資產文件夾,您不必設置BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/")

我是新來的android,如果我得到錯誤,我嘗試快速找到正確的可能性。