安卓游戏引擎AndEngine的背景图像

6

最近我开始使用AndEngine。在这方面很难找到最新的文档/帮助材料。我试图通过查看示例和源代码设置背景图像。但是由于某种原因,屏幕仍然是空白的。我无法找到任何有用的相关信息。以下是代码:

public class AndEngineActivity extends BaseGameActivity {

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

    private Camera mCamera;
    private TextureRegion mBgTexture;
    private BitmapTextureAtlas mBackgroundTexture;


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

    @Override
    public Engine onLoadEngine() {
        // TODO Auto-generated method stub
        this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
        return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
    }

    @Override
    public void onLoadResources() {
        // TODO Auto-generated method stub
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

        this.mBackgroundTexture = new BitmapTextureAtlas(1024, 1024, TextureOptions.DEFAULT);
        mBgTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBackgroundTexture, this, "background.png", 0, 0);

        this.mEngine.getTextureManager().loadTextures(this.mBackgroundTexture);

    }

    @Override
    public Scene onLoadScene() {

        this.mEngine.registerUpdateHandler(new FPSLogger());

        final Scene scene = new Scene();
        final int centerX = (CAMERA_WIDTH -
                mBgTexture.getWidth()) / 2; final int centerY = (CAMERA_HEIGHT -
                mBgTexture.getHeight()) / 2;
        SpriteBackground bg = new SpriteBackground(new Sprite(centerX, centerY, mBgTexture));
        scene.setBackground(bg);

        return scene;
    }

    @Override
    public void onLoadComplete() {
        // TODO Auto-generated method stub

    }
}
3个回答

5
你需要删除:


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

-1
请使用以下代码:

CCSprite background=CCSprite.sprite("car7m.jpg" );


(注:此文本为程序开发相关内容)

-3

不是

    SpriteBackground bg = new SpriteBackground( ... );
    scene.setBackground(bg);

尝试

    Sprite bg = new Sprite(new Sprite(centerX, centerY, mBgTexture));
    scene.attachChild(bg);

嗨,感谢您的回复,但我也尝试了那个方法,没有帮助。我是不是漏掉了什么?还有其他的想法吗? - Mob
好消息,终于搞定了。如上所示,在onCreate(..)方法之前的附加代码中覆盖了布局。我删除了该方法,现在它可以正常工作了。 - Mob
好的,是的,你不想要那个 :-) - bos
他忘记删除setContent函数调用,该函数覆盖了SurfaceView。 - Ashraf Sayied-Ahmad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接