egret项目的一些小技巧


2019-9-30 Egret

egret数据请求的封装

egret.HttpRequest的封装;

class HttpRequest {
  static  Req(url:string,type:string,data:{}){
      return new Promise((resolve,reject)=>{
        let params:string='';
        let request:egret.HttpRequest = new egret.HttpRequest();
        request.responseType = egret.HttpResponseType.TEXT;
        request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        // request.setRequestHeader("Authorization", `Bearer ${JSON.parse(egret.localStorage.getItem("token")) || ''}`);//jwt需求才加
        
        Object.keys(data).forEach((item)=>{
            params+=`&${item}=${data[item]}`
        })
        params=params.substring(1);

        if(type==='GET'){
            request.open(`${url}?${params}`,egret.HttpMethod.GET);
            request.send();
        }
        if(type==='POST'){
            request.open(url,egret.HttpMethod.POST);
            request.send(params);
        }
        request.addEventListener(egret.Event.COMPLETE,(event:egret.Event)=>{
            let request = <egret.HttpRequest>event.currentTarget;
             resolve(request.response);
        },this);
        request.addEventListener(egret.IOErrorEvent.IO_ERROR,(event:egret.IOErrorEvent)=>{
            reject(event);
        },this);
        request.addEventListener(egret.ProgressEvent.PROGRESS,this.onProgress,this);
      })
  }
//请求进度
  static onProgress(event:egret.ProgressEvent):void{
    console.log("请求进度:" + Math.floor(100*event.bytesLoaded/event.bytesTotal) + "%");
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

请求数据案例;

    /**
     * 登录
     */
    static doLogin = (username:string,userpwd:string) => (
        new Promise((resolve)=>{
            HttpRequest.Req(`${Url.http}${Url.Login}`,'POST',
                {
                    "user_id":username,
                    "password":userpwd,
                },
            ).then(res => {
                resolve(JSON.parse(`${res}`));
            })
            .catch(err=>{
               egret.log(`${err.type}===>用户名或密码错误`);
            })
        })
    )
    /**
     * 獲取本期開獎時間及上次期號及開獎號碼
     */
    static getCurrentScheduleIssue=(lottery_id:number)=>(
        new Promise((resolve)=>{
            HttpRequest.Req(`${Url.http}${Url.CurrentScheduleIssue}`,'GET',
                {
                    lottery_id,
                },
            ).then(res=>{
                resolve(JSON.parse(`${res}`));
            })
            .catch(err=>{
               egret.log(`${err.type}===>数据请求错误`);
            })
        })
    )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

开启帧频信息面板

Egret会在舞台的左上角显示FPS和其他性能指标 在index文件内找到data-show-fps字段,设置为true即可 显示帧频信息,反之关闭。 其中:

  • FPS:-帧频
  • Draw:-每帧draw方法调用的平均次数,脏区域占舞台的百分比
  • Cost:-Ticker和EnterFrame阶段显示的耗时,每帧舞台所有事件处理和 矩阵运算耗时,绘制显示对象耗时(单位是ms)
<div style="margin: auto;width: 100%;height: 100%;" class="egret-player"
    data-entry-class="Main"
    data-orientation="auto"
    data-scale-mode="fixedWidth"
    data-frame-rate="60"
    data-content-width="640"
    data-content-height="1136"
    data-multi-fingered="2"
    data-show-fps="false" data-show-log="false"
    data-show-fps-style="x:0,y:0,size:12,textColor:0xffffff,bgAlpha:0.9">
</div>
data-entry-class:设置入口文件;
data-orientation:缩放方式
data-scale-mode:适配模式
data-content-widt和data-content-height:场景的宽高
data-show-fps和data-show-log:是否开启帧频信息;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

点击穿透

在点击生成一个物品后,要在这个物品上面能够点击穿透要在改实例对象上添加两个属性
    _BetChip.touchEnabled = false;
		_BetChip.touchChildren = false;
1
2
3
Thomas: 10/11/2019, 3:49:46 PM