上一个核心切割类:
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 );
}
}
结果:
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。