2013-08-19 51 views
1

在我的游戏应用程序中,运行时在主场景上添加了许多子项,并且我希望在运行时将zIndex赋予新的子项,但不会将其考虑在内。为了让zIndex的到我使用如下代码孩子:更新andengine中的子zIndex?

mySprite.setZIndex(1); 

但如果我刷新主场景在运行时,那么孩子将能正确应用其zIndex的。刷新主场景中,我使用下面的代码:

mainScene.sortChildren(); 

但是,如果我在运行时使用上面的代码,那么它给混蛋给所有的孩子,你知道它看起来很糟糕!那么,如何将主要场景的孩子排序不加混蛋?

编辑

我试着解释我的问题与代码和注释。在下面的代码中,有三种方法:一种方法将动画精灵添加到主场景中,一种绘制线方法将线绘制到主场景中,并且我的animsprite在线上移动,因为line zIndex是2,myAnimSprit是zIndex 3。但是在每隔5秒后,我想更新z示例中的animsprite的索引,所以线位于我的animSprite上,它由changeZIndex方法完成,并由定时器调用。

public class TouchPark extends BaseGameActivity { 

     private AnimatedSprite animSprite; 
     private Scene mainScene; 
     // load engine and load all resoureces here 

      @Override 
     public Scene onLoadScene(){ 
      mainScene = new Scene(); 
      createTimerHandler(); 
      addAnimatedSprite() 
     } 

     public void addAnimatedSprite(){ 
      animatedSprite = new AnimatedSprite(- mTextureRegion.getWidth(), -  mTextureRegion.getHeight(), mTextureRegion.deepCopy()); 
      animSprite.setZIndex(3); 
      animSprite.setPosition(initX, initY); 
      mainScene.attachChild(animSprite); 
     } 

     public void drawLine(ArrayList<Float> xList , ArrayList<Float> yList){ 
      float x1 = xList.get(xListLength - 2); 
      float x2 = xList.get(xListLength - 1); 

      float y1 = yList.get(yListLength - 2); 
      float y2 = yList.get(yListLength - 1); 

      Line line = new Line(x1, y1 , x2 ,y2 , lineWidth); 
      line.setZIndex(2); //my sprite move on the line so that line zIndex is 2 and animSprite zIndex is 3 
      line.setColor(1, 0, 0); 
      mainScene.attachChild(line); 
     } 
     public void changeZIndex(){ 

      animSprite.setZIndex(1); // now i want line is on the my animSprite so i changed zIndex of line 
      //but here it will not change zIndex of my animsprite at runTime 
      //but if i write below code then it will get effect 
      mainScene.sortChildren(); 
      //but above code update not only one aimSprite but also all sprite that are presents on the mainScene 
      //and it look like all chilrens get jerk 
     } 

     public void createTimerHandler(){ 

      mGeneralTimerHandler = new TimerHandler(5.0f, true, new ITimerCallback() { 
      public void onTimePassed(TimerHandler pTimerHandler) { 

       changeZIndex(); 
        } 
      getEngine().registerUpdateHandler(mGeneralTimerHandler); 
     } 
} 
+0

我添加了一条带有注释的代码,以更清晰地解释我的问题。我认为这是我最好的尝试来解释我的问题。谢谢。 – Hits

回答

1

首先加载场景中的所有资源。直到你拨打以上线

后实现以下线..

GameActivity.activity.getEngine().setScene(yourScene); 

用户不能查看现场。

所以它不会显示,抽搐。

+0

但@拉玛:我的精灵动态添加用户时扮演游戏和雪碧zIndex的也动态地改变按我的要求的话,我怎么能体现这个场景BCZ场景设置在method.Then我不能做onLoadScene()的结束它动态地意味着重复。 – Hits

0

Intialize最高ZORDER。然后添加的所有动态创建精灵对他的实体的实体。如果没有清除,那么发布代码

+0

感谢给予回复,虽然我不明白你想说什么,所以根据你的指示我把我的代码与评论,所以你可以清楚地了解我的问题。并能够更清晰地给出答案。 – Hits