1. 程式人生 > >白鷺引擎顯示物件程式碼例項

白鷺引擎顯示物件程式碼例項

白鷺引擎
描述:顯示物件程式碼例項

/**
 * @copyright www.egret.com
 * @author city
 * @desc 顯示物件最基本的操作。
 *      顯示物件可以是外部載入的JPG、PNG圖片資源,也可以是程式繪製的形狀。
 *      所有的顯示物件顯示均需要新增到顯示列表。
 */

class Main extends egret.DisplayObjectContainer {

    public constructor() {
        super();
        this.once( egret.Event.ADDED_TO_STAGE, this
.onAddToStage, this ); } private onAddToStage(event:egret.Event) { var imgLoader:egret.ImageLoader = new egret.ImageLoader; imgLoader.once( egret.Event.COMPLETE, this.imgLoadHandler, this ); imgLoader.load( "resource/cartoon-egret_00.png" ); } private _txInfo:egret.TextField; private
_bgInfo:egret.Shape; private imgLoadHandler( evt:egret.Event ):void{ var bmd:egret.BitmapData = evt.currentTarget.data; /*** 本示例關鍵程式碼段開始 ***/ /// 將已載入完成的影象顯示出來 var bird:egret.Bitmap = new egret.Bitmap( bmd ); bird.x = 100; bird.y = 100; this.addChild( bird ); /*** 本示例關鍵程式碼段結束 ***/
/// 為定位設定基準點(即錨點) bird.anchorOffsetX = bmd.width / 2; bird.anchorOffsetY = bmd.height / 2; bird.x = this.stage.stageWidth * .5; bird.y = this.stage.stageHeight * .5; /// 提示資訊 this._txInfo = new egret.TextField; this.addChild( this._txInfo ); this._txInfo.size = 28; this._txInfo.x = 50; this._txInfo.y = 50; this._txInfo.textAlign = egret.HorizontalAlign.LEFT; this._txInfo.textColor = 0x000000; this._txInfo.type = egret.TextFieldType.DYNAMIC; this._txInfo.lineSpacing = 6; this._txInfo.multiline = true; this._txInfo.text = "輕觸螢幕調整顯示物件位置"; this._bgInfo = new egret.Shape; this.addChildAt( this._bgInfo, this.numChildren - 1 ); this._bgInfo.x = this._txInfo.x; this._bgInfo.y = this._txInfo.y; this._bgInfo.graphics.clear(); this._bgInfo.graphics.beginFill( 0xffffff, .5 ); this._bgInfo.graphics.drawRect( 0, 0, this._txInfo.width, this._txInfo.height ); this._bgInfo.graphics.endFill(); this.stage.addEventListener( egret.TouchEvent.TOUCH_BEGIN, ( evt:egret.TouchEvent )=>{ bird.x = evt.localX ; bird.y = evt.localY ; }, this ); } }