coocsCreator杂记

判断是否继承

cc.isChildClassOf = function (subclass, superclass) {

获取所有super classes

CCClass.getInheritanceChain = function (klass) 

cc.js下有很多判断方法

判断是否是数字

cc.js.isNumber

字符串

cc.js.isString
copy all properties from arguments[1...n] to obj
cc.js.mixin

阻止事件向下层传递

event.stopPropagation();停止事件冒泡。

对node执行scheduleUpdate方法

var node = new cc.Node();
node.parent = this.node;
node.update = function (dt){
}.bind(this);
cc.director.getScheduler().scheduleUpdate(node, , false);

注意webGL 的 cc.director.getScheduler().scheduleUpdate的第4个参数可以传递function ,但在canvas中第四个参数无效

继承自cc.Asset的类

cc.TextAsset
cc.AnimationClip
cc.Font
cc.Prefab
cc.SceneAsset
cc.Script
cc.SpriteAtlas
cc.TiledMapAsset
dragonBones.DragonBonesAsset
dragonBones.DragonBonesAtlasAsset
sp.SkeletonData
cc.SpriteFrame

cc.loader.getRes  加载Sprite:如果加载过(cc.loader.loadResArr      cc.loader.loadResDir   cc.loader.loadRes等方法)此素材,可以正常读取     cc.SpriteFrame,否则会返回空

 cc.loader.loadResArray(["imgage/common/btn_common_close1.png"], cc.SpriteFrame, function () {//第一次加载(异步)
var asset = cc.loader.getRes("imgage/common/btn_common_close1.png", cc.SpriteFrame);//第二次可以用getRes加载(同步)
if (asset) {
      var node = new cc.Node();
var spr = node.addComponent(cc.Sprite);
spr.spriteFrame = asset;
       node.parent = this.node;
       node.setPosition(, );
}
}.bind(this));

如果想第一次用同步方法加载:

var node = new cc.Node();
var component = node.addComponent(cc.Sprite);
component.spriteFrame = new cc.SpriteFrame(cc.url.raw("resources/imgage/common/btn_common_close1.png"));
node.parent = this.node;
node.setPosition(0, 0);

bitmapFont  Label可以直接使用cc.loader.getRes加载,而不必担心是否加载过

 var asset = cc.loader.getRes(url, cc.Font);
if (asset) {
var node = new cc.Node();
var component = node.addComponent(cc.Label);
component.font = asset;
}

但是ttf 字体的cc.Font 只有使用cc.loader.loadRes(cc.loader.load    loadResArray   loadResDir等方法加载无效)异步加载过一次后,才能使用getRes方法直接加载

cc.AudioClip可以直接使用cc.loader.getRes同步加载

var asset = this.getRes("sound/audio_test", cc.AudioClip);
if (asset) {
var node = new cc.Node();
node.parent = this.node;
var component = node.addComponent(cc.AudioSource);
component.clip = asset;
component.play();
}

或者

  var node = new cc.Node();
node.parent = rootNode;
var component = node.addComponent(cc.AudioSource);
component.clip = cc.url.raw("resources/sound/audio_test.mp3");
component.play();

游戏增量更新后需要重新加载 cc.game.restart();

cocosCreator 支持es6新方法

1 方法的不定数量传参:

  

var func = function(arg1, ...argsArr) {
console.log("arg1:" + arg1);
for (var i = , len = argsArr.length; i < len; i ++) {
console.log("args " + i + ": " + argsArr[i]);
}
};
func(, "haha", , );

结果:

arg1:
args : haha
args :
args :

2 解构赋值

var func = function() {
return [, ];
}
var a = null;
var b = null;
[a, b] = func();
console.log("a:" + a);
console.log("b:" + b);

a 、b被分别赋值 1、 2,可以用更少的代码,实现函数返回值赋值多个变量

3 map和set的数据结构

var m = new Map([['Michael', ], ['Bob', ], ['Tracy', ]]);
m.get('Michael'); //

  map初始化

var m = new Map(); // 空Map
m.set('Adam', ); // 添加新的key-value
m.set('Bob', );
m.has('Adam'); // 是否存在key 'Adam': true
m.get('Adam'); //
m.delete('Adam'); // 删除key 'Adam'
m.get('Adam'); // undefined

  set

var s1 = new Set(); // 空Set
var s2 = new Set([, , ]); // 含1, 2, 3

  添加元素

>>> s.add()
>>> s
{, , , }
>>> s.add()
>>> s
{, , , }
for (let i of s.values()) {
  console.log(i);
}
let arr = [...s];
console.log(JSON.stringify(arr));
>>>[1, 2, 3, 4]
还有 delete() clear() forEach() keys() values() size 等方法

set 交集并集计算

let set1 = new Set([,,,,,]);
let set2 = new Set([,,,,,]); //并集
let union = new Set([...set1,...set2]);
console.log("union:" + JSON.stringify([...union]));
//[1,2,3,4,5,6,7,8,9]
//交集
let intersect = new Set([...set1].filter(x => set2.has(x)));
console.log("intersect:" + JSON.stringify([...intersect]));
//[4,5,6]
//差集
let diffsect = new Set([...set1].filter(x => !set2.has(x)));
console.log("diffsect:" + JSON.stringify([...diffsect]));
//[1,2,3]

4promise

var promise1 = new Promise(function (resolve, reject) {
GameCtr.resCtr.getSpriteAsyn(Res.sheep_plist, function (asset) {
if (asset) {
resolve(asset);
}
}, "sheep_run_0");
});
var promise2 = new Promise(function (resolve, reject) {
GameCtr.resCtr.getSpriteAsyn(Res.sheep_plist, function (asset) {
if (asset) {
resolve(asset);
}
}, "sheep_run_0");
});
Promise.all([promise1, promise2]).then(function (assets) {
for (var i = , len = assets.length; i < len; i ++) {
let asset = assets[i];
asset.position = cc.p(, i * );
asset.parent = this.node;
var atlas = asset['Atlas'];
var spr = asset.getComponent(cc.Sprite);
spr.spriteFrame = atlas.getSpriteFrames()[];
}
}.bind(this));

5 proxey

如果节点不存在自动生成节点的Tree

 function Tree() {
return new Proxy({}, handler);
}
var handler = {
get: function (target, key, receiver) {
if (!(key in target)) {
target[key] = Tree(); // 自动创建一个子树
}
return Reflect.get(target, key, receiver);
}
};
var tree = Tree();
//tree.a.b.c ="1";
console.log(tree.a.b.c);

扩展已有方法

this.testFunc = function(key) {
console.log(`testFunc1 ${key}`);
};
var obj = new Proxy(this, {
get(target, key, proxy) {
return function(...args) {
console.log(`testFunc2 ${args[]}`);
return Reflect.apply(target[key], target, args);
}
}
});
obj.testFunc(""); >>>testFunc2
>>>testFunc1

revocable proxy

this. testProxyFunc = function (key) {
console.log(`testProxyFunc ${key}`);
};
var handler = {
get(target, key, proxy) {
return function(...args) {
console.log(`testProxyFunc ${args[]}`);
return Reflect.apply(target[key], target, args);
}
}
};
var revocable = Proxy.revocable(this, handler);
var proxy = revocable.proxy;
proxy.testProxyFunc("");
revocable.revoke();
console.log("-------");
proxy.testProxyFunc(""); >>>testProxyFunc
>>>testProxyFunc
>>>-------
Uncaught TypeError: Cannot perform 'get' on a proxy that has been revoked

6 class module

    

上一篇:python SSH客户端的交互式和非交互方式


下一篇:微信小程序获取二维码参数