2019年12月8日

egret基础-加载位图与使用图集

作者 admin

定义一个loadBitmap类,继承自sprite组件,单例模式

class loadBitmap extends egret.Sprite {
private static shared: loadBitmap;
public static getInstance() {
if (!loadBitmap.shared) {
loadBitmap.shared = new loadBitmap();
}
return loadBitmap.shared;
}

public constructor() {
super();
this.addEventListener(egret.Event.ADDED_TO_STAGE, this.loadPicSprite, this);
this.loadBitMap();
}
private loadBitMap() {
var imgLoader: egret.ImageLoader = new egret.ImageLoader;
imgLoader.once(egret.Event.COMPLETE, this.imgLoadHandler, this);
imgLoader.load("resource/assets/egret_icon.png");
}
private imgLoadHandler(evt: egret.Event): void {
let loader: egret.ImageLoader = evt.currentTarget;
let bmd: egret.BitmapData = loader.data;
//创建纹理对象
let texture = new egret.Texture();
texture.bitmapData = bmd;
let bmp: egret.Bitmap = new egret.Bitmap(texture);
// 创建一个自定义矩形对象
var rect: egret.Rectangle = new egret.Rectangle(30, 31, 40, 41);
// 开启缩放区域检测
bmp.scale9Grid = rect;
bmp.width *= 2;
bmp.y = 150;

this.addChild(bmp);
}
private loadPicSprite(event: egret.Event) {
RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onGroupComplete, this);
RES.loadConfig("resource/default.res.json", "resource/");
RES.loadGroup("preload");
}
private onGroupComplete() {
//json图集文件名字,图集指定图片name
var txtr: egret.Texture = RES.getRes("pics_json#pic1");
var img: egret.Bitmap = new egret.Bitmap(txtr);
console.log(txtr, img)
img.y = 450;
img.x = 300;
this.addChild(img);
}
}

引用方法:在main.ts中

this.addChild( loadBitmap.getInstance() );