私のゲーム アプリケーションでは、多くの子が実行時にメイン シーンに追加され、実行時に zIndex を新しい子に与えたいのですが、考慮されていません。子供に zIndex を与えるには、次のようなコードを使用します。
mySprite.setZIndex(1);
しかし、実行時にメイン シーンを更新すると、子はその zIndex を適切に取得します。メイン シーンを更新するために、次のコードを使用します。
mainScene.sortChildren();
しかし、実行時に上記のコードを使用すると、すべての子にジャークが与えられ、見た目が非常に悪いことがわかります。では、メインシーンの子供たちをジャークなしでソートするにはどうすればよいでしょうか?
編集
コードとコメントで問題を説明しようとしています。以下のコードでは、3 つのメソッド: 1 つのメソッドはアニメーション化されたスプライトをメイン シーンに追加し、1 つの draw line メソッドはメイン シーンに線を引きます。しかし、5 秒ごとに、この例では 1 のようにアニメーション スプライトの z インデックスを更新したいので、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);
}
}