android - 无法在 AndEngine 中加载 Sprite

标签 android sprite andengine

我正在学习 AndEngine 并且正在学习一些基础教程。我试图在屏幕上加载 Sprite ,但 Sprite 看起来已损坏。

这是我的代码:

package com.example.andengine_demo;

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.TextureOptions;
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.BaseGameActivity;

public class MainActivity extends BaseGameActivity {

    // ===========================================================
    // Constants
    // ===========================================================
    static final int CAMERA_WIDTH = 720; 
    static final int CAMERA_HEIGHT = 480;

    private static final String TAG = "AndEngineTest";
    private BitmapTextureAtlas mBitmapTextureAtlas;
    private TextureRegion mPlayerTextureRegion;
    // ===========================================================
    // Fields
    // ===========================================================

    //private ZoomCamera mCamera;



@Override
public EngineOptions onCreateEngineOptions() {
    // TODO Auto-generated method stub
    Camera mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    return new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);
}

@Override
public void onCreateResources(
        OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception {
    // TODO Auto-generated method stub
    mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box.png", 0, 0);
    mBitmapTextureAtlas.load();

}

@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
{
    this.mEngine.registerUpdateHandler(new FPSLogger());
    // TODO Auto-generated method stub
    Scene scene = new Scene();
    scene.setBackground(new Background(0f, 0f, 1f));

    final Sprite oPlayer = new Sprite(32,32, mPlayerTextureRegion, getVertexBufferObjectManager());
    scene.attachChild(oPlayer);
}

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

}
}

这是它的样子:

Corrupted Sprite

我检查了图像大小是 32x32,图像的路径,我也尝试过在加载时使用不同的纹理选项。图片格式为PNG。如果你看到,背景颜色不适合我设置的颜色。我认为我的模拟器已正确配置(使用 GPU 模拟)以使用 GLES2.0。 我认为问题可能出在相机高度和相机宽度的值上。我设置它是因为我在教程中看到了它们,但不知道它们是否正确...我的分辨率是 WVGA800。

我不知道我做错了什么......我需要解决这个问题才能继续创建游戏,所以任何帮助都会受到欢迎。

谢谢!

最佳答案

您应该改为扩展 SimpleBaseGameActivity。然后你不必担心那些回调或任何事情。

public class MainActivity extends SimpleBaseGameActivity {

// ===========================================================
// Constants
// ===========================================================
static final int CAMERA_WIDTH = 720; 
static final int CAMERA_HEIGHT = 480;

private static final String TAG = "AndEngineTest";
private BitmapTextureAtlas mBitmapTextureAtlas;
private TextureRegion mPlayerTextureRegion;
// ===========================================================
// Fields
// ===========================================================

//private ZoomCamera mCamera;



@Override
public EngineOptions onCreateEngineOptions() {
    // TODO Auto-generated method stub
    Camera mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    return new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);
}

@Override
public void onCreateResources() {
    mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box.png", 0, 0);
    mBitmapTextureAtlas.load();
}

@Override
public Scene onCreateScene()
{
    this.mEngine.registerUpdateHandler(new FPSLogger());
    // TODO Auto-generated method stub
    Scene scene = new Scene();
    scene.setBackground(new Background(0f, 0f, 1f));

    final Sprite oPlayer = new Sprite(32,32, mPlayerTextureRegion, getVertexBufferObjectManager());
    scene.attachChild(oPlayer);
    return scene;
}

}

关于android - 无法在 AndEngine 中加载 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11971674/

相关文章:

android - 在flutter中单击+按钮时如何滚动?

java - 为什么(新)Admob 尝试将我的自定义中介适配器转换为旧版本?

android - Cordova -插件-fcm : Cannot find name 'FCMPlugin'

java - 如何在 Java 中发送带有 XML 数据的 HTTPS POST 请求

python - 如何在 Pygame 中显示 Sprite ?

javascript - 在 EaselJS 中使用 SpriteStage 会使 Sprite 的 mask 不起作用

macos - 如何找到位置 X 和 Y(图像、区域)

android - 和 EngineRuntimeException : Inherently incompatible device detected.

android - 在模拟器中找不到 EGLConfig,gpu 模拟为真

android - 快速移动的物体有时会错过 Box2d 和 AndEngine 中的碰撞