添加/删除对象
const spr: egret.Sprite = new egret.Sprite();
spr.graphics.beginFill(0x00ff00);
spr.graphics.drawRect(0, 0, 100, 100);
spr.graphics.endFill();
this.addChild(spr);spr.touchEnabled = true;
spr.addEventListener(egret.TouchEvent.TOUCH_BEGIN, onRemoveSpr, this);function onRemoveSpr(e) {if (spr.parent) {spr.parent.removeChild(spr);}}
不同容器添加同一对象
const A: egret.Sprite = new egret.Sprite();
A.graphics.beginFill(0x0000ff);
A.graphics.drawRect(0, 0, 100, 100);
A.graphics.endFill();
A.x = 50;
A.y = 50;
const B: egret.Sprite = new egret.Sprite();
B.graphics.beginFill(0xff0000);
B.graphics.drawRect(0, 0, 300, 300);
B.graphics.endFill();
this.addChild(B);
const C: egret.Sprite = new egret.Sprite();
C.graphics.beginFill(0x00ff00);
C.graphics.drawRect(0, 0, 200, 200);
C.graphics.endFill();
C.x = 400;
this.addChild(C);
B.addChild(A);
C.addChild(A);