【问题标题】:AndEngine, Sprites not ordering correctlyAndEngine,Sprites 没有正确排序
【发布时间】:2013-10-12 02:11:21
【问题描述】:

我正在尝试将两个精灵附加到一个实体,但它们没有按照我希望它们附加的顺序进行附加。

我希望 avatarSprite 位于 BarSprite 之上,但无论我做什么,它总是出现在 barSprite 之后。

我尝试设置 ZIndex,更改我将它附加到实体的顺序,我什至调用 sortChildren,但它仍然以相同的顺序出现,avatarSprite,上面有 barSprite。这是我正在使用的一些代码。


public class MyList extends Entity{
    public ArrayList<MyListItem> listItems;
    public ArrayList<Player> players;

    public MyList(ArrayList<Player> pList){
        super();
        listItems = new ArrayList<MyListItem>();
        players = pList;
        buildList();
        ...
        //set height and width
    }

    private void buildList(){
        float buffer = LIST_BUFFER;
        int i = 0;
        for(Player mPlayer : players){
            MyListItem mItem = new MyListItem(mPlayer);

            mPlayer.sprite.setTag(i);

            if (i == 0){
                mItem.setPosition(INITIAL_ITEM_X, INITIAL_ITEM_Y);
            } else{

            float x = listItems.get(i - 0).getX();
            float y = listItems.get(i - 0).getY() + mItem.getHeight() + buffer;

            mItem.setPosition(x,y);
           }

           attachChild(mItem);
           listItems.add(mItem);
           i++;
        }
    }


public class MyListItem extends Entity{
    private Player mPlayer;  

    public MyListItem(pPlayer){
        super();
        this.mPlayer = pPlayer;
        VertexBufferObjectManager vbom = MGA.getInstance().getVertexBufferObjectManager();
        Sprite barSprite = new Sprite( 0, 0, MGA.getInstance().mPlayerBar, vbom );

        Sprite avatarSprite = new Sprite( 0, 0, MGA.getInstance().mAvatarTextureRegion, vbom );
        avatarSprite.setScale( 3.0f );
        avatarSprite.setZIndex( 3 );


        float bWidth = barSprite.getWidth();
        float bHeight = barSprite.getHeight(); 

        barSprite.setX( bWidth / 2 );
        barSprite.setY( bHeight / 2 );
        barSprite.setZIndex( 2 );



        avatarSprite.setPosition( bWidth * 0.16058f, bHeight
            / 0.5f - avatarSprite.getHeight() / 2 );


        this.attachChild( barSprite );
        this.attachChild( avatarSprite );

        this.sortChildren( true );

        this.setHeight( bHeight );
        this.setWidth( bWidth );
    }
}

}

编辑: 我已经更新了代码以匹配我当前使用的代码。创建列表后,它通过以下方式附加到 HUD: myList = new MyList(players); attachChild(myList);

我确信我所做的一切都是正确的,就像 Rectangle 一样,它扩展了与 Sprite 相同的类,Shape->Entity

我错过了什么吗?

编辑2: 我刚刚尝试再次使用Rectangle,但我仍然遇到同样的问题......

【问题讨论】:

    标签: java android andengine


    【解决方案1】:

    你应该使用图层。

    1. 将图层创建为实体

      private Entity backgroundLayer = new Entity();
      private Entity foregroundLayer = new Entity();
      
    2. 将图层附加到您的场景

      scene.attachChild(backgroundLayer);
      scene.attachChild(foregroundLayer);
      // important note: backgroundLayer should be attached first so it will be at the bottom.
      
    3. 现在将您的精灵附加到图层上

      backgroundLayer.attachChild(barSprite);
      foregroundLayer.attachChild(avatarSprite);
      

    【讨论】:

    • Sprite 是一个实体,所以我认为这只是多余的。即便如此,我也曾尝试过,但没有运气:(
    • 分层方法对我来说就像一个魅力。很高兴您解决了它,但是除了使用图层之外,您的答案是无法解释的。 x,y 定位如何影响 z 定位?我相信您在代码中的另一个位置解决了它,而不是使用 setPosition()。
    【解决方案2】:

    哇...好吧..我修好了,这就是我所做的:


    旧:

    ...
    avatarSprite.setPosition( bWidth * 0.16058f, bHeight
            / 0.5f - avatarSprite.getHeight() / 2 );
    ...
    

    变化:

    ...
    
    avatarSprite.setPosition( bWidth * 0.16058f, bHeight
            * 0.5f - avatarSprite.getHeight() / 2 );
    ...
    

    我所做的只是将/ 0.5f 更改为* 0.5f

    通过除以 0.5,我将高度乘以 2,使 y 位置比我想要的高 200px,导致它出现在其上方的 listItem 下方。更正它会将精灵放置在正确列表项中的正确位置。

    由于我生成项目的方式,每个新项目都放置在前一个项目之上。这就是为什么它似乎在酒吧精灵的下方。因为它在上面的层之下。

    【讨论】:

      猜你喜欢
      • 2020-04-26
      • 2014-09-07
      • 2014-11-21
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多