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;
        }
    }
}

测试 :
一 : 资源准备
Egret之图集切割

预览资源
①:aaa.png:
Egret之图集切割
②: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 );
        }
    }

结果:
Egret之图集切割

转载于:https://blog.51cto.com/aonaufly/2096912

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值