白鹭笔记之egret.runEgret

  • egret.runEgret( options?: runEgretOptions )
    白鹭程序启动器,程序入口,此时启动心跳计时器
  /**
     * @private
     * 启动心跳计时器。
     */
    function startTicker(ticker: egret.sys.SystemTicker): void {
        let requestAnimationFrame =
            window["requestAnimationFrame"] ||
            window["webkitRequestAnimationFrame"] ||
            window["mozRequestAnimationFrame"] ||
            window["oRequestAnimationFrame"] ||
            window["msRequestAnimationFrame"];

        if (!requestAnimationFrame) {
            requestAnimationFrame = function (callback) {
                return window.setTimeout(callback, 1000 / 60);
            };
        }

        requestAnimationFrame(onTick);
        function onTick(): void {
            requestAnimationFrame(onTick);
            ticker.update();
        }
    }

Tips: 白鹭程序的心跳设定的频率大约是60次/每秒,具体要看宿主环境支持,并且不能被更改

window.requestAnimationFrame告诉浏览器——你希望执行一个动画,并且要求浏览器在下次重绘之前调用指定的回调函数更新动画,回调函数执行次数通常是每秒60次。该方法需要传入一个回调函数作为参数,该回调函数会在浏览器下一次重绘之前执行

  • egret.startTick( callBack: (timeStamp:number) => boolean, thisObject: any )
    注册并启动一个计时器,注册后将会持续触发回调方法,若要停止回调,需要手动调用stopTick()方法。
    @param callBack 要执行的回调方法。参数 timeStamp 表示从启动Egret框架开始到执行该回调方法前经过的时间(毫秒)。 若回调方法返回值为true,其作用与TimerEvent.updateAfterEvent()类似,将会忽略帧频限制,在此方法处理完成后立即重绘屏幕。
    @param thisObject 回调方法的this对象引用。

Tips: 这里并不建议设定返回值为true,因为会破坏设定的帧频,导致绘制频率加大,加重绘制负担

callback方法将会在egret每一次心跳中被执行,执行时间在画布绘制之前,如下:

      /**
         * @private
         * 执行一次刷新
         */
        public update(forceUpdate?: boolean): void {
            ...
            let callBackList = this.callBackList;
            let thisObjectList = this.thisObjectList;
            let length = callBackList.length;
            let requestRenderingFlag = $requestRenderingFlag;
            ....
            // 执行异步操作的回调函数,如:getResAsync
            this.callLaterAsyncs();               
            for (let i = 0; i < length; i++) {
                if (callBackList[i].call(thisObjectList[i], timeStamp)) {
                    requestRenderingFlag = true;
                }
            }
            let t2 = egret.getTimer();
            let deltaTime = timeStamp - this.lastTimeStamp;
            this.lastTimeStamp = timeStamp;
             // 两次心跳实际的间隔时间大于设定的帧频,则立即重新绘制
            if (deltaTime >= this.frameDeltaTime || forceUpdate) {
                this.lastCount = this.frameInterval;
            } else {
            /**
              *  在每一次心跳中会检查是否达到渲染时间,而这里的渲染时间实际上并不是真实的时间,它定义了
              * 一个叫lastCount的值,取值方式为lastCount = Math.round(60000 / 帧率)。举个例子如果设定帧率      
              * 为30,则值为2000,那么按照每次心跳减1000的处理,我们可以保证每两次心跳会执行一次渲 
              * 染。但是如果你设帧率为24,那么lastcount=2500,这样的话我们只能两次/三次心跳间隔来执行渲 
              * 染。所以最后我们就可以理解为什么白鹭官方建议帧率设定为能被60整除的数值就是这个原因
              */
                this.lastCount -= 1000;         
                if (this.lastCount > 0) {
                    if (requestRenderingFlag) {
                        this.render(false, this.costEnterFrame + t2 - t1);
                    }
                    return;
                }
                this.lastCount += this.frameInterval;
            }
            this.render(true, this.costEnterFrame + t2 - t1);
            let t3 = egret.getTimer();
            this.broadcastEnterFrame();
            let t4 = egret.getTimer();
            this.costEnterFrame = t4 - t3;
        }

从以上的代码中可以看出:

  • 白鹭程序的心跳频率和画布绘制的频率不是同一个概念,心跳频率是60次每秒。而画布的绘制频率是可以主动去设置的,但是画布的绘制是在每次心跳中执行的,所以它的有效值得范围在0~60之间。
  • 每帧实际的执行时间 = callbacklist执行时间 + 画布绘制时间(可能没有) + enterFrame事件执行时间(可能没有)

Tips: 可以将所有的用户逻辑归总放入callbacklist中,然后进行分帧管理,从而有效控制每帧的执行时间

你可能感兴趣的:(白鹭笔记之egret.runEgret)