其他分享
首页 > 其他分享> > android-AndEngine背景显示为三角形

android-AndEngine背景显示为三角形

作者:互联网

我是AndEngine的新手,我正尝试在this tutorial之后实现河内游戏.
将背景图片插入gfx文件夹,并设置所有onCreateResources代码和onCreateScene代码之后,我尝试运行该应用程序,我所看到的只是一个代表我的背景图片的三角形,如您在this image中看到的.

这是我的代码:

    final int CAMERA_WIDTH = 480;
    final int CAMERA_HEIGHT = 800;

    public EngineOptions onCreateEngineOptions() {
        myCamera = new Camera(800, 480, CAMERA_WIDTH, CAMERA_HEIGHT);

        return new EngineOptions(false, ScreenOrientation.PORTRAIT_SENSOR,
                new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
                myCamera);
    }
    public ITextureRegion texture;

    public void onCreateResources(
            OnCreateResourcesCallback pOnCreateResourcesCallback)


        throws Exception {
            try {

                // 1 - Set up bitmap textures
                ITexture backgroundTexture = new BitmapTexture(
                        this.getTextureManager(), new IInputStreamOpener() {
                            public InputStream open() throws IOException {
                                return getAssets().open("gfx/background.png");
                            }
                        });
     // 2 - Load bitmap textures into VRAM
                backgroundTexture.load();
    // 3 - Set up texture regions
                this.mBackgroundTextureRegion = TextureRegionFactory
                        .extractFromTexture(backgroundTexture);

         }

public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
            throws Exception {

        // 1 - Create new scene
        final Scene scene = new Scene();
        Sprite backgroundSprite = new Sprite(0, 0, this.mBackgroundTextureRegion, getVertexBufferObjectManager());
        scene.attachChild(backgroundSprite);
    }

由于我尝试自己解决此错误,因此我已经尝试过:

>设置相机FillResolutionPolicy(),对结果没有影响.
>将背景创建为BitmapTextureAtlas,BitmapTextureAtlasTextureRegionFactory.createFromAsset
>调用mEngine.getScene().setBackground而不是attachChild
>使用其他API级别重新创建Android虚拟设备(尝试16、15)

另外,AndEngine论坛上有一个主题为this one的主题,我没有找到答案.

解决方法:

您的代码中有一些错误

1)在myCamera =新的Camera(800,480,CAMERA_WIDTH,CAMERA_HEIGHT);
第一个和第二个参数用于位置x和y.所以你应该喜欢

myCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

2)在CameraOptions中使用

new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED,
new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), myCamera);

3)要加载BitmapTexture,应使用BitmapTextureAtlasTextureRegionFactory

例如.

BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
BitmapTextureAtlas bitmapTextureAtlas = new BitmapTextureAtlas(
        getTextureManager(), width_of_image, height_of_image,
        TextureOptions.BILINEAR //Or any TextureOpion you want.
);
ITextureRegion texture = BitmapTextureAtlasTextureRegionFactory
    .createFromAsset( bitmapTextureAtlas, this, "file_name.png", x_position, y_position);

bitmapTextureAtlas.load();

标签:andengine,android
来源: https://codeday.me/bug/20191101/1979964.html