0

我正在开发一款安卓游戏。我想显示包含一些图像的界面。我在资产中有一个名为 gfx 的文件夹。这个文件夹包含图像。我已经写了这段代码。

公共类 MainActivity 扩展 SimpleBaseGameActivity {

private static int CAMERA_WIDTH = 800;
private static int CAMERA_HEIGHT = 480;
private ITextureRegion mBackgroundTextureRegion, mTowerTextureRegion, mRing1, mRing2, mRing3;
private Sprite mTower1, mTower2, mTower3;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);





}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

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

@Override
protected void onCreateResources() {
    // TODO Auto-generated method stub
    try {
        // 1 - Set up bitmap textures
        ITexture backgroundTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
            @Override
            public InputStream open() throws IOException {
                return getAssets().open("gfx/background.png");
            }
        });
        ITexture towerTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
            @Override
            public InputStream open() throws IOException {
                return getAssets().open("gfx/tower.png");
            }
        });
        ITexture ring1 = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
            @Override
            public InputStream open() throws IOException {
                return getAssets().open("gfx/ring1.png");
            }
        });
        ITexture ring2 = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
            @Override
            public InputStream open() throws IOException {
                return getAssets().open("gfx/ring2.png");
            }
        });
        ITexture ring3 = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
            @Override
            public InputStream open() throws IOException {
                return getAssets().open("gfx/ring3.png");
            }
        });
        // 2 - Load bitmap textures into VRAM
        backgroundTexture.load();
        towerTexture.load();
        ring1.load();
        ring2.load();
        ring3.load();

     // 3 - Set up texture regions

        this.mBackgroundTextureRegion = TextureRegionFactory.extractFromTexture(backgroundTexture);
        this.mTowerTextureRegion = TextureRegionFactory.extractFromTexture(towerTexture);
        this.mRing1 = TextureRegionFactory.extractFromTexture(ring1);
        this.mRing2 = TextureRegionFactory.extractFromTexture(ring2);
        this.mRing3 = TextureRegionFactory.extractFromTexture(ring3);
    } catch (IOException e) {

        Debug.e(e);
    }

}

@Override
protected Scene onCreateScene() {
    // TODO Auto-generated method stub
    final Scene scene = new Scene();
    Sprite backgroundSprite = new Sprite(0, 0, this.mBackgroundTextureRegion, getVertexBufferObjectManager());
    scene.attachChild(backgroundSprite);

    // 2 - Add the towers
    mTower1 = new Sprite(192, 63, this.mTowerTextureRegion, getVertexBufferObjectManager());
    mTower2 = new Sprite(400, 63, this.mTowerTextureRegion, getVertexBufferObjectManager());
    mTower3 = new Sprite(604, 63, this.mTowerTextureRegion, getVertexBufferObjectManager());
    scene.attachChild(mTower1);
    scene.attachChild(mTower2);
    scene.attachChild(mTower3);
    return scene;

}

}

但只能在用 xml 编写的屏幕上显示 hello world。我注意到没有调用 onCreateResources() 和 onCreateScene() 。需要显式调用它们吗?

谁能帮我??

提前致谢。

4

2 回答 2

0

看起来您没有正确地将 AndEngine 的所有部分连接在一起。我建议您从这里开始获取有关如何执行此操作的信息。

于 2013-06-22T20:05:12.380 回答
0

您正在将 android 活动与 andengine 活动混合。

这都是关于活动生命周期的,对于andengine和android来说都是不同的

于 2013-06-28T09:20:48.177 回答