AndEngine显示黑屏,没有错误

时间:2017-03-02 01:23:29

标签: image load sprite andengine

我正在使用AndEngine GLES2创建游戏。我正在尝试在活动中加载图像。但每次我运行应用程序时,它只显示小黑方,没有错误。以下是我的代码:

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

如果有人想看看哪里出错了,我会很高兴。我做错了什么?

2 个答案:

答案 0 :(得分: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);

    }

}

还尝试先设置背景,然后附上孩子。

答案 1 :(得分:0)

我找到了解决方案。

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

在上面的一行中,我必须将地图集的宽度和高度更改为1024,1024,它完美地起作用。问题出在我的身上。