1

嗨,我是 Android 开发的新手。

我想使用 AndEngine 创建一个简单的游戏。所以我想尝试一个小样本。但是当我将 apk 文件部署到我的 nexus7 时。但是当我运行应用程序时,它总是在屏幕中央显示“Hello World”,背景为白色。但它应该显示具有特定背景颜色的图像。

这是我的代码:

public class MainActivity extends SimpleBaseGameActivity {
private static final float CAMERA_WIDTH = 720;
private static final float CAMERA_HEIGHT = 480;
private Camera mCamera;
private BitmapTextureAtlas mBitmapTextureAtlas;
private Engine mEngine;
private TextureRegion mFaceTextureRegion;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public EngineOptions onCreateEngineOptions() {

    this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

    return new EngineOptions(true,
            ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(
                    CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);
}

@Override
protected void onCreateResources() {

    this.mBitmapTextureAtlas = new BitmapTextureAtlas(
            mEngine.getTextureManager(), 32, 32,
            TextureOptions.BILINEAR_PREMULTIPLYALPHA);

    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

    this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory
            .createFromAsset(this.mBitmapTextureAtlas, this,
                    "face_box.png", 0, 0);

    getTextureManager().loadTexture(this.mBitmapTextureAtlas);
}

@Override
protected Scene onCreateScene() {

    this.mEngine.registerUpdateHandler(new FPSLogger());
    final Scene scene = new Scene();
    scene.setBackground(new Background(new Color(0, 255, 128)));
    final int centerX = (int) ((CAMERA_WIDTH - this.mFaceTextureRegion
            .getWidth()) / 2);
    final int centerY = (int) ((CAMERA_HEIGHT - this.mFaceTextureRegion
            .getHeight()) / 2);
    final Sprite face = new Sprite(centerX, centerY,
            this.mFaceTextureRegion, new VertexBufferObjectManager());
    scene.attachChild(face);

    return scene;
}

}

我究竟做错了什么?提前致谢。

4

1 回答 1

3

由于您使用的是 AndEngine SimpleBaseGameActivity,因此您不需要这个

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

“Hello world”出现是因为您正在设置内容视图,删除对 onCreate 的覆盖,它应该可以工作。

于 2012-12-01T17:46:10.140 回答