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
coocsCreator杂记的更多相关文章
- [Erlang 0118] Erlang 杂记 V
我在知乎回答问题不多,这个问题: "对你职业生涯帮助最大的习惯是什么?它是如何帮助你的?",我还是主动回答了一下. 做笔记 一开始笔记软件做的不好的时候就发邮件给自己, ...
- Ubuntu杂记——Ubuntu下用虚拟机共享上网
由于最近把自己电脑环境换成了Ubuntu,但学校的网络是电信的闪讯,大学里用过的人都知道这货有多坑,而且没有Linux客户端,上网都是问题,怪不得国内用Linux的人那么少,特别是高校的学生(让我瞎逼 ...
- 一个ubuntu phper的自我修养(杂记)
ubuntu使用杂记 1.flatabulous安装使用. flatabulous是一个ubuntu图标主题. 使用它,必须得安装tweak插件. sudo add-apt-repository pp ...
- 有关Java的日期处理的一些杂记
在企业应用开发中,经常会遇到日期的相关处理,说实话JDK自带的日期方法很难用.就我个人而言我一般都会采用joda-time来替代JDK自身的日期. 这篇文章是杂记,所以写的比较零散,希望大家不要见怪. ...
- 分布式系统之CAP理论杂记[转]
分布式系统之CAP理论杂记 http://www.cnblogs.com/highriver/archive/2011/09/15/2176833.html 分布式系统的CAP理论: 理论首先把分布式 ...
- Redis杂记
参考资料: Redis 教程 | 菜鸟教程 : http://www.runoob.com/redis/redis-tutorial.html Redis快速入门 :http://www.yiibai ...
- MySQL杂记
参考资料: w3school SQL 教程 : http://www.w3school.com.cn/sql/index.asp 21分钟 MySQL 入门教程 : http://www.cnblo ...
- Android之开发杂记(一)
1.cygwin环境变量设置 可在Cygwin.bat 中设置 set NDK_ROOT=P:/android/android-ndk-r8e 或者在home\Administrator\.bash_ ...
- ios程序开发杂记
ios程序开发杂记 一.程序构建 与一般的程序构建无太大区别,都是源文件编译链接这一套,通常是在mac上做交叉编译,也就是利用xcode里带的ios编译工具集去生成arm架构的ios程序(或是x86的 ...
随机推荐
- 在project窗口中快速定位文件
[在project窗口中快速定位文件] 点击带圆圈的小叉叉按钮,这个时候Project中就会定位到当前文件目录下了. 参考:http://blog.csdn.net/hyr83960944/artic ...
- pinyin4j 中文转拼音
- angularjs 粘贴事件
参考 http://www.jb51.net/article/89708.htm ng-paste 需要setTimeout,否则无法获取到数据
- pta7-20 畅通工程之局部最小花费问题(Kruskal算法)
题目链接:https://pintia.cn/problem-sets/15/problems/897 题意:给出n个城镇,然后给出n×(n-1)/2条边,即每两个城镇之间的边,包含起始点,终点,修建 ...
- C++中 top()与pop()
top()是取出栈顶元素,不会删掉栈里边的元素 pop()是删除栈顶元素.
- Angular之RouterModule的forRoot与forChild
Angular 提供了一种方式来把服务提供商从模块中分离出来,以便模块既可以带着 providers 被根模块导入,也可以不带 providers 被子模块导入. 区别: `forRoot` crea ...
- stm32阅读代码工具source insight
不知道学stm32有没有这样的烦恼,想看一个项目的代码,但是用keil又发现建立工程太麻烦,单个打开文件又找不到函数和变量之间的依赖关系,变量和函数又不能高亮显示,linux下vim和emacs虽然很 ...
- 微信小程序开发——模板中加载html代码
最新方法可以使用微信小程序提供的 rich-text (富文本)组件直接写解析html,详见 rich-text: <rich-text class='f13 c_9' nodes=" ...
- Ubuntu虚拟机全屏问题
方法一:修改分辨率 xrandr查看能用的 xrandr -s 1920x1200改变. 方法二:安装新vmtools apt-get install open-vm-tools apt-get in ...
- 项目总结10:通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题
通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题 关键字 springboot热部署 ClassCastException异常 反射 ...