如何在AndEngine中将精灵设置为背景?

时间:2014-07-22 12:48:53

标签: android background andengine sprite

我是AndEngine的新手。我想在我的游戏中将图像设置为背景。我能够完成这项工作,但问题是当我将精灵设置为背景时,当前看到的所有其他精灵都会被隐藏。这是代码:

public class SplashScene extends Scene {
BaseActivity activity;
Sprite sprite1,sprite2,backgrondSprite;

public SplashScene() {
    activity = BaseActivity.getSharedInstance();

    backgrondSprite = new Sprite(0, 0,
            BaseActivity.getSharedInstance().mBackgroundTextureRegion,
            BaseActivity.getSharedInstance().getVertexBufferObjectManager());

    setBackground(new SpriteBackground(backgrondSprite));


     sprite1 = new Sprite(32, 32,
            BaseActivity.getSharedInstance().mBirdTextureRegion, 
            BaseActivity.getSharedInstance().getVertexBufferObjectManager());

     sprite2 = new Sprite(32, 32,
                BaseActivity.getSharedInstance().mBirdTextureRegion, 
                BaseActivity.getSharedInstance().getVertexBufferObjectManager());

     sprite1.setPosition(-sprite1.getWidth(), activity.mCamera.getHeight() / 2);
     sprite2.setPosition(activity.mCamera.getWidth(),
            activity.mCamera.getHeight() / 2);

    attachChild(sprite1);
    attachChild(sprite2);
    attachChild(backgrondSprite);

    sprite1.registerEntityModifier(new MoveXModifier(1, sprite1.getX(),
            activity.mCamera.getWidth() / 2 - sprite1.getWidth()));
    sprite2.registerEntityModifier(new MoveXModifier(1, sprite2.getX(),
            activity.mCamera.getWidth() / 2));

    loadResources();
}

void loadResources() {
    DelayModifier dMod = new DelayModifier(2,
            new IEntityModifierListener() {

                @Override
                public void onModifierStarted(IModifier<IEntity> arg0,
                        IEntity arg1) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void onModifierFinished(IModifier<IEntity> arg0,
                        IEntity arg1) {
                    activity.setCurrentScene(new MainMenuScene());
                }
            });

    registerEntityModifier(dMod);
}

} 如何使sprite1和sprite2仍然显示与另一个精灵作为背景? 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

是的..那是对的; &#34; ..在附加背景后附上你的精灵&#34;

你应该知道zIndex ..

此代码也有效..

attachChild(sprite1);
attachChild(sprite2);
attachChild(background);

sprite1.setZIndex(1);
sprite2.setZIndex(1);
background.setZIndex(0);

scene.sortChildren();

背景的索引是0然后在排序时,它会稳定在底部..

(比赛时有时需要)