深度值
// 設(shè)置顯示對象在顯示容器中的深度值
顯示容器.addChildAt(顯示對象, 深度值)
// 交換2個顯示對象的深度值
顯示容器.swapChileren(顯示對象, 顯示對象)
顯示容器.swapChildrenAt(深度值, 深度值)
// 改變顯示對象的深度值
顯示容器.setChildIndex(顯示對象, 深度值)
class Main extends egret.DisplayObjectContainer{
public constructor(){
super()
this.addEventListener(egret.Event.ADDED_TO_STAGE,this.init,this)
}
private init(event:egret.Event){
// 創(chuàng)建一個容器
let box:egret.Sprite = new egret.Sprite()
// 添加到顯示列表
this.addChild(box)
// 創(chuàng)建4個顯示對象
let rect:egret.Sprite
for(let i:number = 0;i < 4;i++){
rect = new egret.Sprite()
rect.graphics.beginFill(0xffffff*Math.random())
rect.graphics.drawRect(0,0,150,150)
rect.graphics.endFill()
rect.x = i*100
// 添加到box顯示容器中
box.addChild(rect)
}
console.log(box.width) // 250
console.log(box.height) // 100
// 創(chuàng)建新的顯示對象寥茫,并設(shè)置深度值
let newRect:egret.Sprite = new egret.Sprite()
newRect.graphics.beginFill(0xff0000)
newRect.graphics.drawRect(0,0,500,500)
newRect.graphics.endFill()
newRect.y = 50
box.addChildAt(newRect,2) // newRect的深度值是2馒索,相當(dāng)于層級是2
// 從新設(shè)置newRect深度值
box.setChildIndex(newRect,1) // newRect的深度值是1用踩,相當(dāng)于層級是1
// 顯示對象交換深度值,,,這里的rect相當(dāng)于第4個顯示對象,深度值是4混埠,newRect是1,
// 交換后newRect是4,最后的rect的深度值是1
box.swapChildren(rect,newRect)
// 交換深度值腮介,
box.swapChildrenAt(1,4)
}
}
訪問容器子對象
訪問的是容器的子對象:是根據(jù)容器訪問容器中的子對象
class Main extends egret.DisplayObjectContainer{
public constructor (){
super()
this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAddToStage,this)
}
private onAddToStage(event:egret.Event){
// 創(chuàng)建顯示容器box并添加到顯示列表
let box:egret.Sprite = new egret.Sprite()
this.addChild(box)
// 創(chuàng)建顯示對象shp
let shp:egret.Sprite = new egret.Sprite()
shp.graphics.beginFill(0x00ff00)
shp.graphics.drawRect(0,0,200,200)
shp.graphics.endFill()
shp.name = 'hell'
box.addChild(shp)
// 如何獲取顯示對象呢续搀,有2中方式
// 通過顯示對象的name值或深度值
// let _shp:egret.DisplayObject = box.getChildAt(0)
let _shp:egret.DisplayObject = box.getChildByName('hell')
_shp.scaleX = 0.5
}
}