我的应用程序总是显示“Hello World”

时间:2012-12-01 17:40:01

标签: android andengine

嗨,我是Android开发中的新手。

我想用AndEngine创建一个简单的游戏。所以我想试一下样品。但是当我将apk文件部署到我的nexus7时。但是当我运行App时,它总是在屏幕中央用白色背景说“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;
}

}

我做错了什么?提前谢谢。

1 个答案:

答案 0 :(得分: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并且它应该可以正常工作。