BitmapTextureAtlasTextureRegionFactory.createFromAsset 崩溃
几个月前我看过 andengine,并且成功地利用它做了一些东西。 现在我下载了最新版本,但我遇到了最简单的事情崩溃。这是我的代码
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)。
i have took a look to andengine a few months ago, and i managed to make something out of it.
Now i downloaded the newest version, and i'm having a crash for the simplest thing. Here my code
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
// ===========================================================
}
(Usually it is well indented)
The logCat error is
Failed loading Bitmap in AssetBitMapTextureAtlasSource. Asset path: gfx/play.png
java.io.FileNoFoundException: gfx/play.png
But i put the 2 images in the folder and i added them to the project like i did when i first tried andengine(and it worked)..
So i don't really understand where the problem is..
The images are 2 png created with the gimp(one is 57x36 and the other 58x31).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果找不到文件,则会抛出 java.io.FileNoFoundException 异常 - 因此除了引擎无法找到图像之外,没有其他可能性。
确保您的图像位于:
assets/gfx/
中,而不仅仅是位于名为gfx/
的文件夹中。A
java.io.FileNoFoundException
will be thrown if a file wasn't found - so there is no other possibility than the engine couldn't found the image.Make sure your images are in:
assets/gfx/
and not just in a folder calledgfx/
.确保您的项目中有资产文件夹,并从 BuildPath 选项(项目选项 - 您可以通过右键单击项目资源管理器进行访问),从“源”选项卡按“添加文件夹”按钮并检查要导入的资产文件夹你的编译过程。
gfx
文件夹必须位于assets文件夹中。如果没有这样的文件夹并且所有图像都是资产文件夹,则不必设置 BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/")我是 android 新手,如果出现错误,我会尝试可能性快速找到正确的一个。
Make sure there is assets folder in your project and from BuildPath options(project options-you can access by right-click on your project explorer), from Source Tab press Add Folder button and check assets folder to import your compilation process.
gfx
folder must be in assets folder. If there is no such folder and all your images is assets folder, you don't have to setBitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/")
I am new to android and if I gain errors I try possibilities rapidly to find correct one.