2019年11月30日

egretEui基础-图片加载与更改纹理

作者 admin


class ball extends eui.Component implements eui.UIComponent {

//按钮
public btn_start:eui.Button;
//足球图片
public img_ball:eui.Image;

public constructor() {
super();
this.skinName = “resource/gameSkins/ball.exml”;
}

protected partAdded(partName:string,instance:any):void
{
super.partAdded(partName,instance);
}

protected childrenCreated():void
{
super.childrenCreated();

this.startLoad();

}
private startLoad(){

//ImageLoader用于加载图片(jpg,png,gif),通过load()方法来启动加载,被加载的图片对象存储在ImageLoader.data属性上
var loader:egret.ImageLoader = new egret.ImageLoader();

//图片加载完成调用this.loadComplate函数
loader.addEventListener(egret.Event.COMPLETE,this.loadComplate,this)

//想要加载的图片地址
var url:string = ‘resource/assets/ballImgs.png’

//放入加载函数
loader.load(url)
}
private loadComplate() {
console.log(‘预加载图片完成’)

this.btn_start.addEventListener(egret.TouchEvent.TOUCH_TAP,
this.onButtonClick,this);
}
//点击按钮,图片更改纹理
private onButtonClick(e:egret.TouchEvent){

//更改eui的source属性

this.img_ball.source = ‘resource/assets/ballImgs.png’
}
}