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

问题描述 投票:0回答:2

嗨,我是 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;
}

}

我做错了什么?预先感谢。

android andengine
2个回答
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 的覆盖,它应该可以工作。


0
投票

检查 onCreate 函数中的 setContentView

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

您的代码可能不会在“activity_main”中写入代码,请检查您在那里编写代码的布局中的其他 xml 文件

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.