【问题标题】:AndEngine background imageAndEngine 背景图片
【发布时间】:2012-03-13 07:30:05
【问题描述】:

最近我开始研究 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

    }
}

【问题讨论】:

    标签: android background-image andengine


    【解决方案1】:

    你需要删除:

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

    【讨论】:

      【解决方案2】:

      使用以下内容:

      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);
        

        【讨论】:

        • 您好,感谢您的回复,但我也尝试过,但没有帮助。我错过了什么吗?还有什么想法吗?
        • 好消息终于来了。从上面的附加代码中可以看出,onCreate(..) 方法正在覆盖布局。我删除了该方法,它现在可以工作了。
        • 嗯,是的,你不想拥有那个 :-)
        • 他忘记删除覆盖表面视图的 setContent 函数调用
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-09
        • 2014-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多