这个ANDEngine HUD有什么问题?

时间:2014-11-01 09:47:43

标签: andengine hud

我正在尝试在AndEngine上制作一个HUD,但是当我在下面编写这个代码并运行模拟器时,模拟器上只出现一个带有崩溃的HUD图像的黑屏。

这就是我要显示的内容,enter image description here 这就是我的代码显示的方式。

enter image description here这是我的代码 -

package com.example.andenginetester;


import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.hud.HUD;
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.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.SimpleBaseGameActivity;


public class SpriteTester extends SimpleBaseGameActivity {

	public static final int CAMERA_WIDTH = 720;
	public static final int CAMERA_HEIGHT = 480;
	
	private HUD myHUD;
	
	private Sprite myHUDSprite;
	
	private BitmapTextureAtlas myAtlas;
	private TextureRegion myHUDImage;
	final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);	
	
	@Override
	public EngineOptions onCreateEngineOptions() {


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


	@Override
	protected void onCreateResources() {
		myAtlas = new BitmapTextureAtlas(this.getTextureManager(), 1024, 1024);
		myHUDImage = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.myAtlas, this, "gfx/HUD.png", 0, 0);
		mEngine.getTextureManager().loadTexture(myAtlas);
		
	}

	@Override
	protected Scene onCreateScene() {
		final Scene scene = new Scene();
		scene.setBackground(new Background(0, 0, 0));
		myHUDSprite = new Sprite(0, 0, myHUDImage, this.getVertexBufferObjectManager());
		
		myHUD.attachChild(myHUDSprite);
		camera.setHUD(myHUD);
		return scene;

	}
	

	
	

}

我该如何解决?

1 个答案:

答案 0 :(得分:0)

好吧,我设法得到了正确的结果,让我知道它是否适合你:

a)我更新了AndroidManifest.xml

<activity ... >

中添加

1)android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|mcc|mnc"

2)android:screenOrientation="landscape"

b)更新后的活动:

public class MainActivity extends SimpleBaseGameActivity {

    public static final int CAMERA_WIDTH = 720;
    public static final int CAMERA_HEIGHT = 480;

    private HUD myHUD = new HUD();

    private Sprite myHUDSprite;

    private BitmapTextureAtlas myAtlas;
    private TextureRegion myHUDImage;
    final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

    @Override
    public EngineOptions onCreateEngineOptions() {

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

    @Override
    protected void onCreateResources() {
        myAtlas = new BitmapTextureAtlas(this.getTextureManager(), 1024, 1024);
        myHUDImage = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
                this.myAtlas, this, "gfx/HUD.png", 0, 0);
        mEngine.getTextureManager().loadTexture(myAtlas);

    }

    @Override
    protected Scene onCreateScene() {
        final Scene scene = new Scene();
        scene.setBackground(new Background(0, 0, 0));
        myHUDSprite = new Sprite(0, 0, myHUDImage,
                this.getVertexBufferObjectManager());

        myHUD.attachChild(myHUDSprite);
        myHUD.setPosition(CAMERA_WIDTH/2,CAMERA_HEIGHT/2);
        camera.setHUD(myHUD);
        return scene;

    }

}