美文网首页
cocos creator ts util 防抖

cocos creator ts util 防抖

作者: 凡凡的小web | 来源:发表于2023-01-03 19:58 被阅读0次

// export function sleep(delay: number, ctx: cc.Component) {
// return new Promise(resolve => {
// ctx.scheduleOnce(resolve, delay)
// })
// }

// export function moveToParent(node1: cc.Node, node2: cc.Node) {
// if (!node1 || !node2 || (node1.parent == node2)) {
// return
// }
// let worldPos = node1.parent.convertToWorldSpaceAR(node1.getPosition())
// let local2Pos = node2.convertToNodeSpaceAR(worldPos)
// node1.parent = node2
// node1.position = cc.v3(local2Pos)
// }

export let throttleUtil = {
_throttleFunc: null,
_throttleFlag: true,
_throttleFn: null,

//防抖函数
use(func: Function, delay: number, ctx: cc.Component): Function {
this._throttleFn = func
this._throttleFunc = (...args) => {
this._throttleFn(...args)
this._throttleFlag = true
}

  return (...args: any) => {
      if (!this._throttleFlag) {
          return
      }
      this._throttleFlag = false

      ctx.scheduleOnce(() => {
          this._throttleFunc(...args)
      }, delay)
  }
}

};

export let poolUtil = {
poolCache: {},

_getCache(name) {
if (!this.poolCache[name]) {
this.poolCache[name] = new cc.NodePool();
}
return this.poolCache[name];
},

getItemShowNode(itemPerFab: cc.Prefab) {
var name = itemPerFab.name;

var pool = this._getCache(name);

let node = null;
if (pool.size() > 0) {
  node = pool.get();
} else {
  node = cc.instantiate(itemPerFab);
  node.name = name;
}
return node;

},

returnItemShowNode(node: cc.Node) {
var pool = this._getCache(node.name);
pool.put(node);
},
};

相关文章

网友评论

      本文标题:cocos creator ts util 防抖

      本文链接:https://www.haomeiwen.com/subject/yfilcdtx.html