开发工具cocos creator。
比较随意,看demo源码总结
1.平台:
当前平台系统:cc.sys.os
cc.sys.isNative:false/true;(是不是真机)
cc.sys.isMobile:false/true;(是不是手机可以使浏览器选择了手机模式)
cc.sys.OS_ANDROID:Android;
cc.sys.OS_IOS:iOS;
cc.sys.OS_WINDOWS:Windows;
cc.sys.OS_OSX:OS X;
cc.sys.isBrowser:true/false(是不是浏览器);
cc.sys.browserType:浏览器类型,比如:safari;
用法:
(1).if(cc.sys.os == cc.sys.OS_ANDROID){}
(2).if(!cc.sys.isNative){ return; }
cc.sys.browserType !== cc.sys.BROWSER_TYPE_UC
2.存储:
cc.sys.localStorage.setItem("bgmVolume",v);
var t = cc.sys.localStorage.getItem("bgmVolume");
cc.sys.localStorage.removeItem("wx_account");
3.节点隐藏/显示:(可用于弹出框)
cc.find("Canvas/alert").active=true;(true为显示,false为隐藏);
4.获取某一节点:
(1)varcvs=this.node.getComponent(cc.Canvas);
(2)vart=this.node.getComponent("leixingxuanze");
(3)varbtnClose= cc.find("Canvas/game_result/btnClose");
seat.node.getChildByName("zimocishu").getComponent(cc.Label).string= info.numzimo;
this._lblTip= cc.find("Canvas/reconnect/tip").getComponent(cc.Label);
示例:
this._leixingxuanze= [];
vart=this.node.getChildByName("leixingxuanze");//该节点的名为“leixingxuanze”的子节点
for(vari=0;i
varn=t.children[i].getComponent("RadioButton");
if(n!=null){
this._leixingxuanze.push(n);
}
5.给label赋值:
this.tipLabel.string="nisdji";
6.引入别的js文件:
(1)如果UserMgr.js文件varHTTP= cc.Class({}),则
varUserMgr=require("UserMgr");
(2)如果UserMgr.js文件cc.Class({}),则
varUserMgr=require("UserMgr");
cc.vv.userMgr=newUserMgr();
示例:
cc.vv= {};
varUserMgr=require("UserMgr");
cc.vv.userMgr=newUserMgr();
7.输出:
cc.log('ddd');
console.log('sdd');
8.字典/数组 与字符串互转
字典或数组转字符串:
var dic={name:'sd',sex:'m'};
JSON.stringify(dic));
字符串格式的字典/数组恢复原类型:
var dicStr='{name:'sd',sex:'m'}';
var dic1=JSON.parse(dicStr);
9.打开地址链接进入别的网站:
cc.sys.openURL('http://www.jianshu.com');
10.跳转页面:
cc.director.loadScene("login");
11.全局变量:
(1)cc.baseUrl='192.12.23.13'
(2)window.baseUrl="192.12.23.13";
12.组件设置数组:
(1)
holdsEmpty:{
default:[],
type:[cc.SpriteFrame]
},
(2)holdsEmpty:[cc.SpriteFrame];
(3)
holdsEmpty:{
default:[],
type:cc.SpriteFrame,
},
13.随机:
Math.random();
14.事件监听:
(1)this.node.on('game_dingque',function(data){
});
(2)点击事件代码篇
varbtn= cc.find("Canvas/popups/"+ btnName);
this.addClickEvent(btn,this.node,"PopupMgr","onBtnClicked");
addClickEvent:function(node,target,component,handler){
vareventHandler=newcc.Component.EventHandler();
eventHandler.target= target;
eventHandler.component= component;
eventHandler.handler= handler;
varclickEvents= node.getComponent(cc.Button).clickEvents;
clickEvents.push(eventHandler);
},
onBtnClicked:function(event){
this.closeAll();
varbtnName= event.target.name;
if(btnName=="btn_agree"){
cc.vv.net.send("dissolve_agree");
}
else if(btnName=="btn_reject"){
cc.vv.net.send("dissolve_reject");
}
else if(btnName=="btn_sqjsfj"){
cc.vv.net.send("dissolve_request");
}
},
15.播放/关闭/暂停音频:
node.getComponent(cc.Animation).play("dingque_tuijian");
node.getComponent(cc.Animation).stop();
node.getComponent(cc.Animation).pause();
16.资源文件加载:
(1)进度条
cc.loader.onProgress=function( completedCount, totalCount, item ){
//console.log("completedCount:" + completedCount + ",totalCount:" + totalCount );
if(self._isLoading){
self._progress= completedCount/totalCount;
}
};
(2)全部图片加载
cc.loader.loadResAll("textures",function(err, assets) {
});
拓展:
获取url,并把其参数放到数组中:
urlParse:function(){
varparams= {};
if(window.location==null){
returnparams;
}
varname,value;
varstr=window.location.href;//取得整个地址栏
varnum=str.indexOf("?")
str=str.substr(num+1);//取得所有参数 stringvar.substr(start [, length ]
vararr=str.split("&");//各个参数放到数组里
for(vari=0;i
num=arr[i].indexOf("=");
if(num>0){
name=arr[i].substring(0,num);
value=arr[i].substr(num+1);
params[name]=value;
}
}
returnparams;
},