Egret之图集切割
上一个核心切割类:
module demo{ /** * 图集截个核心工具 * @author Aonaufly */ export class CutAtlasTools{ private static _instance : CutAtlasTools = null; public static get Instance() : CutAtlasTools{ if( !CutAtlasTools._instance ){ CutAtlasTools._instance = new CutAtlasTools(); } return CutAtlasTools._instance; } /** * 图集的切图 * @param {egret.Bitmap} $bitMap * @param {egret.Rectangle} $rec * @param {number} $scale * @returns {egret.Texture} */ public getSlice2Atlas( $bitMap : egret.Bitmap , $rec : egret.Rectangle , $scale : number = 1.0 ) : egret.Texture{ let $drawTexture: egret.RenderTexture = new egret.RenderTexture(); $drawTexture.drawToTexture( $bitMap , $rec , $scale ); return $drawTexture; } }}
测试 :
一 : 资源准备
预览资源
①:aaa.png:
②:aaa.json:
{"file":"aaa.png","frames":{"win.png":{"x":0,"y":0,"w":67,"h":60,"offX":0,"offY":0,"sourceW":67,"sourceH":60},"up.png":{"x":69,"y":0,"w":44,"h":58,"offX":0,"offY":1,"sourceW":44,"sourceH":60}}}
二 : 加载并使用(测试)切割工具 (修改Main.ts)
private _bitMapData : egret.BitmapData = null; private _json : JSON = null; /** * 创建场景界面 * Create scene interface */ protected startCreateScene(): void { let $json_res : string = `resource/assets/aaa.json`; let $png_res : string = `resource/assets/aaa.png`; demo.LoaderImages4UrlManager.Instance.startLoading( $png_res , this.png_loaded.bind(this), false ); demo.LoaderTexts4UrlManager.Instance.startLoading( $json_res, this.json_loaded.bind(this), false, demo.TyText._TEXT ); } private png_loaded( $res : demo.ILoaderImages4CallBack ) : void{ this._bitMapData = $res._bitmapdata; this.checkOver(); } private json_loaded( $res : demo.ILoaderTexts4CallBack ) : void{ this._json = JSON.parse( <string>$res._content ); this.checkOver(); } private checkOver() : void{ if( this._bitMapData && this._json ){ let $bitmap : egret.Bitmap = new egret.Bitmap( this._bitMapData ); let $win_config : JSON = this._json["frames"]["win.png"]; let $rec : egret.Rectangle = new egret.Rectangle( +<number>$win_config["x"], +<number>$win_config["y"], +<number>$win_config["w"], +<number>$win_config["h"] ); let $texture : egret.Texture = demo.CutAtlasTools.Instance.getSlice2Atlas( $bitmap , $rec,1.0 ); let $img : eui.Image = new eui.Image(); $img.source = $texture; this.addChild( $img ); } }
结果:
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。