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创建的2 png(一个是57x36,另一个是58x31)。
答案 0 :(得分:6)
如果找不到文件,则会抛出java.io.FileNoFoundException
- 因此没有其他可能性,因为引擎无法找到图像。
确保您的图片位于:assets/gfx/
,而不只是位于名为gfx/
的文件夹中。
答案 1 :(得分:3)
确保项目中有资源文件夹,并且从 BuildPath 选项(项目选项 - 您可以通过右键单击项目浏览器访问),从“源”选项卡中按“添加文件夹”按钮并检查资产用于导入编译过程的文件夹。
gfx
文件夹必须位于assets文件夹中。如果没有此类文件夹且您的所有图像都是资源文件夹,则不必设置BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/")
我是Android的新手,如果我遇到错误,我会尽快找到正确的可能性。