在我的遊戲應用程序中,運行時在主場景上添加了許多子項,並且我希望在運行時將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);
}
}
我添加了一條帶有註釋的代碼,以更清晰地解釋我的問題。我認爲這是我最好的嘗試來解釋我的問題。謝謝。 – Hits