使用AndEngine运行简单代码

时间:2013-05-20 14:46:13

标签: java android andengine

我最近开始学习AndEngine,但我遇到了麻烦。我正在学习有关基础知识的教程,我想加载一个png image,但它给了我错误,项目失败了

以下是源代码

public class MainActivity extends BaseGameActivity {
    protected static final int WIDTH = 800;
    public static final int HEIGHT = 460;
    BitmapTextureAtlas playerTexture;
    ITextureRegion playerTexureRegion;
    Scene scene;

    @Override
    public EngineOptions onCreateEngineOptions() {
        Camera mCamera = new Camera(0, 0, WIDTH, HEIGHT);
        EngineOptions engine = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(WIDTH, HEIGHT), mCamera);

        return engine;
        // TODO Auto-generated method stub

    }

    @Override
    public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception {
        // TODO Auto-generated method stub
        loadsfx();
        pOnCreateResourcesCallback.onCreateResourcesFinished();
    }

    private void loadsfx() {
    // TODO Auto-generated method stub
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("assets/");

        playerTexture = new BitmapTextureAtlas(getTextureManager(),64,64);
        playerTexureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(playerTexture, this, "fred1.png",0,0);
        playerTexture.load();
    }

    @Override
    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws Exception {
        // TODO Auto-generated method stub
        this.scene = new Scene();
        this.scene.setBackground(new Background(0,125,58));

        pOnCreateSceneCallback.onCreateSceneFinished(this.scene);

    }

    @Override
    public Engine onCreateEngine(EngineOptions pEngineOptions) {
        // TODO Auto-generated method stub
        return super.onCreateEngine(pEngineOptions);
    }

    @Override
    public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
        // TODO Auto-generated method stub

        Sprite sPlayer = new Sprite(WIDTH / 2, HEIGHT / 2, playerTexureRegion, this.mEngine.getVertexBufferObjectManager());
        sPlayer.setRotation(45.0f);
        this.scene.attachChild(sPlayer);
        pOnPopulateSceneCallback.onPopulateSceneFinished();
    }

这是LogCat所说的

05-20 18:11:28.110: E/AndEngine(6616): at android.content.res.AssetManager.openAsset(Native Method)
05-20 18:11:28.110: E/AndEngine(6616): at android.content.res.AssetManager.open(AssetManager.java:315)
05-20 18:11:28.110: E/AndEngine(6616): at android.content.res.AssetManager.open(AssetManager.java:289)
05-20 18:11:28.110: E/AndEngine(6616): at org.andengine.opengl.texture.atlas.bitmap.source.AssetBitmapTextureAtlasSource.onLoadBitmap(AssetBitmapTextureAtlasSource.java:86)
05-20 18:11:28.110: E/AndEngine(6616): at org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas.writeTextureToHardware(BitmapTextureAtlas.java:154)
05-20 18:11:28.110: E/AndEngine(6616): at org.andengine.opengl.texture.Texture.loadToHardware(Texture.java:137)
05-20 18:11:28.110: E/AndEngine(6616): at org.andengine.opengl.texture.TextureManager.updateTextures(TextureManager.java:254)
05-20 18:11:28.110: E/AndEngine(6616): at org.andengine.engine.Engine.onDrawFrame(Engine.java:613)
05-20 18:11:28.110: E/AndEngine(6616): at org.andengine.opengl.view.EngineRenderer.onDrawFrame(EngineRenderer.java:105)
05-20 18:11:28.110: E/AndEngine(6616): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1363)
05-20 18:11:28.110: E/AndEngine(6616): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1118)

请帮助我,我非常希望了解引擎,但它不起作用。

1 个答案:

答案 0 :(得分:1)

BitmapTextureAtlasTextureRegionFactory.setAssetBasePath(path);

“setAssetBasePath”从“assets”目录设置资产路径。 在您的代码中,精灵图像的完整路径必须是“/assets/assets/fred1.png”。 如果sprite“/assets/fred1.png”的路径只是删除BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("assets/");