【问题标题】:AndEngine background displayed as a triangleAndEngine 背景显示为三角形
【发布时间】:2012-08-17 19:07:14
【问题描述】:

我是 AndEngine 的新手,我正在尝试按照 this tutorial 实现 hanoi 游戏。 将背景图片插入 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);
    }

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

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

另外,AndEngine 论坛中有一个帖子,this one 我没有找到答案。

【问题讨论】:

    标签: android andengine


    【解决方案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, 新的 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();
    

    【讨论】:

      【解决方案2】:

      试试这个

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

      改为

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

      【讨论】:

        【解决方案3】:

        对我来说,这看起来像是一些 OpenGL 问题。看看 RenderOptions,你可以通过调用 engineOptions.getRenderOptions(); 来获取它们,在那里你可以调整各种渲染选项。

        不管怎样,你的 Camera 构造函数很奇怪,你想通过它来完成什么?常用参数如driver613所说。此外,您似乎交换了 CAMERA_WIDTH 和 CAMERA_HEIGHT 的值。如果我没记错的话,Android 会处理设备方向,因此宽度确实对应于设备当前方向的宽度。

        【讨论】:

        • 我使用了教程中找到的相机构造函数...我尝试交换宽度和高度参数,没有改变。
        • 然后尝试调整 RenderOptions,它们并不多。也许您应该使用以下信息改进问题:您使用的是 GLES2 还是 GLES1 版本的 AndEngine?这会发生在真实设备上吗?不同的屏幕分辨率呢?
        • 我只在几个屏幕大小的模拟器上试过,没有变化。我没有在真实设备上尝试过......即使它可以工作它也不会帮助我调试。我正在使用 GLES2
        • 嗯,在真实设备上调试和在模拟器上调试一样好,如果不是更好的话。您的问题可能是由对模拟器的错误 GLES2 支持引起的。我的渲染问题只发生在模拟器上,而不同的问题只发生在物理设备上,那就是 GLES1。如果您有使用 Android 手机的朋友,请尝试将 .apk 提供给他们,看看会发生什么。或者上传到互联网上让我们测试一下。
        【解决方案4】:

        部分问题可能在这里

        public void onCreateResources(
                OnCreateResourcesCallback pOnCreateResourcesCallback)
        

        你永远不会调用 pOnCreateResourcesCallback - 你应该在你的 OnCreateResources 方法结束时这样做。

        【讨论】:

          【解决方案5】:

          当您的 textureAtlas 太小而无法容纳所有纹理时,也会发生这种情况。检查是否所有纹理都在其中找到了位置。例如,如果您有一排 10 个 10x10 像素的纹理,而您的图集只有 90(或小于 100 像素)宽或小于 10 像素高。如果您有疑问,只需尝试足够大的尺寸,看看会发生什么:)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-12-03
            • 2020-02-27
            • 2012-06-13
            • 1970-01-01
            • 1970-01-01
            • 2013-03-27
            • 2017-04-29
            相关资源
            最近更新 更多