AndEngine背景显示为三角形

3
我是AndEngine的新手,正在尝试按照这篇教程来实现汉诺塔游戏。 在将背景图片插入gfx文件夹并设置所有onCreateResources代码和onCreateScene代码之后,我尝试运行应用程序,但我所能看到的只是一个代表我的背景图像的三角形,就像这张图片中所示。 以下是我的代码:
    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);
    }

由于我已经尝试了解决这个错误,我已经尝试过:

  1. 使用camera FillResolutionPolicy()设置,但对结果没有影响。
  2. 将背景创建为BitmapTextureAtlas,使用BitmapTextureAtlasTextureRegionFactory.createFromAsset方法。
  3. 调用mEngine.getScene().setBackground而不是attachChild方法。
  4. 重新创建Android虚拟设备,使用其他API级别(尝试了16、15)。

此外,在AndEngine论坛上有一个帖子,链接在此,但我没有找到答案。

5个回答

1

你的代码有一些错误

1) 在 myCamera = new 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();

1
对我来说,这似乎是一些OpenGL问题。看一下RenderOptions,你可以通过调用engineOptions.getRenderOptions();来获取它们。在那里,你可以调整各种渲染选项。
无论如何,你的相机构造函数非常奇怪,你想通过这样做实现什么?通常的参数就像driver613所说的那样。此外,你似乎交换了CAMERA_WIDTH和CAMERA_HEIGHT的值。如果我没有错,Android会处理设备方向,以便宽度实际上对应于设备当前方向的宽度。

我使用了教程中找到的相机构造函数... 我尝试交换宽度和高度参数,但没有变化。 - Oz Radiano
尝试调整RenderOptions,因为它们并不多。也许您应该使用以下信息来改进问题:您是否正在使用AndEngine的GLES2或GLES1版本?在真实设备上会发生什么?不同屏幕分辨率呢? - JohnEye
我只在几个模拟器上尝试了几个屏幕大小,没有变化。我没有在真实设备上尝试过...即使它能工作,也不能帮助我调试。我正在使用GLES2。 - Oz Radiano
在真实设备上进行调试与模拟器上的效果一样好,甚至更好。你遇到的问题可能是由于模拟器对GLES2支持存在缺陷所致。我曾经遇到过只有在模拟器上出现渲染问题,而在真实设备上则出现不同的问题,这还是在使用GLES1的情况下。如果你有一个安卓手机的朋友,可以试着把.apk文件给他们看看会发生什么。或者上传到互联网上让我们来测试。 - JohnEye

1

试试这个

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

取而代之

myCamera = new Camera(800, 480, CAMERA_WIDTH, CAMERA_HEIGHT);

1

问题的一部分可能在这里

public void onCreateResources(
        OnCreateResourcesCallback pOnCreateResourcesCallback)

您从未调用过 pOnCreateResourcesCallback - 您应该在 OnCreateResources 方法的末尾执行此操作。


0
这也可能发生在纹理集太小无法容纳所有纹理的情况下。检查所有纹理是否都能放得下。例如,如果您有一行包含10个大小为10x10像素的纹理,并且您的图集宽度仅为90(或小于100像素),高度不到10像素。如果您有疑问,请尝试足够大的尺寸,看看会发生什么 :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接