// Learn cc.Class: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html // Learn Attribute: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html var JCMsgHandler = cc.Class({ //name: "GameMsgList", extends: cc.Component, properties: { // foo: { // // ATTRIBUTES: // default: null, // The default value will be used only when the component attaching // // to a node for the first time // type: cc.SpriteFrame, // optional, default is typeof default // serializable: true, // optional, default is true // }, // bar: { // get () { // return this._bar; // }, // set (value) { // this._bar = value; // } // }, }, // LIFE-CYCLE CALLBACKS: // onLoad () {}, init(handler){ this._handobj = handler; this.msglst = []; this.deltatime = 0; this.delaytime = 500; this.rate = 60; }, start () { }, // update (dt) {}, run (dt){ var self = this; this.msglst.forEach(element => { element.delaycount--; if(element.delaycount <= 0){ self._handobj && self._handobj.handleData && self._handobj.handleData(element); element.handled = true; } }); var i = this.msglst.length; while(i--){ if(this.msglst[i].handled){ this.msglst.splice(i,1); } } }, pushMsg (obj){ var msg = this.msgclone(obj); var subobj = JSON.parse(msg.content); var tm = subobj.timestamp + this.deltatime; var d = new Date(); msg.delaycount = Math.ceil((d.getTime() - tm) / this.rate); msg.handled = false; this.msglst.push(msg); }, msgclone(obj) { // Handle the 3 simple types, and null or undefined if (null == obj || "object" != typeof obj) return obj; // Handle Date if (obj instanceof Date) { var copy = new Date(); copy.setTime(obj.getTime()); return copy; } // Handle Array if (obj instanceof Array) { var copy = []; var len = obj.length; for (var i = 0; i < len; ++i) { copy[i] = this.msgclone(obj[i]); } return copy; } // Handle Object if (obj instanceof Object) { var copy = {}; for (var attr in obj) { if (obj.hasOwnProperty(attr)) copy[attr] = this.msgclone(obj[attr]); } return copy; } throw new Error("Unable to copy obj! Its type isn't supported."); }, setDeltaTime(dt){ this.deltatime = dt; }, setFramerate(rt){ this.rate = rt; } }); module.exports = JCMsgHandler;