1.場景加載
cc.director.loadScene('場景名稱');//場景跳轉(zhuǎn)
cc.director.preloadScene('場景名稱');//預(yù)加載場景
cc.director.getScene();//獲取當(dāng)前場景
2.獲取節(jié)點
var node = cc.find("Canvas/bg");//通過訪問路徑來獲取節(jié)點
var a = this.node.getChildByName('name');//通過名字獲取子節(jié)點
node.getComponent(cc.Label).string = 'abc';//獲取節(jié)點上的組件值
var a = cc.find("Canvas/bg").getComponent(cc.Sprite);//通過訪問路徑來獲取節(jié)點绿映,及獲取該節(jié)點的指定組件
this.node .getChildByName('節(jié)點名稱').getComponent(cc.Label)//通過節(jié)點名獲取子節(jié)點夜牡,獲取該節(jié)點指定組件
var a = this.node;//獲取當(dāng)前腳本所在的節(jié)點
var a = this.node.parent;//獲取父節(jié)點
var a = this.node.getChildByTag(1001);//通過標(biāo)簽獲取子節(jié)點
var a = cc.find("bg/score",this.node);//通過指定節(jié)點下的路徑獲取節(jié)點
var a = this.node.children;//獲取所有子節(jié)點
var a = this.node.childrenCount;//獲取子節(jié)點數(shù)量
var a = cc.director.getScene();//獲取場景主節(jié)點
var a = cc.instantiate(node);//克隆節(jié)點
this.node.parent = cc.find('Canvas');//綁定父節(jié)點
this.node.addChild(nodeName,zIndex,tag);//添加子節(jié)點,可設(shè)置層級和標(biāo)簽
this.node.removeChild(nodeName);//通過名字移除子節(jié)點
this.node.removeChildByTag (nodeTag);//通過標(biāo)簽移除子節(jié)點
this.node.destroy();//銷毀節(jié)點
this.node.isValid;//判定節(jié)點是否可用
this.node.removeChild(newNode);//移除節(jié)點中指定的子節(jié)點
this.node.removeChildByTag(1001);//通過標(biāo)簽移除節(jié)點中指定的子節(jié)點
this.node.removeAllChildren();//移除所有子節(jié)點
this.node.destroyAllChildren();//銷毀所有子節(jié)點
this.node.cleanup();//停止所有正在播放的動作和計時器
var sprites = this.node.getComponentsInChildren(cc.Label);//遞歸查找自身及所有子節(jié)點中指定類型的組件
3.獲取節(jié)點位置穆役,設(shè)置節(jié)點
var a = node.getPositionX();或 getPositionY() //獲取節(jié)點的X軸或Y軸坐標(biāo)
var a = node.getScaleX(); 或getScaleY() //獲取節(jié)點的X軸或Y軸縮放比例
node.x = 100;//設(shè)置節(jié)點x軸坐標(biāo)
node.y = 100;//設(shè)置節(jié)點y軸坐標(biāo)
node.setPosition(x,y); //設(shè)置節(jié)點坐標(biāo)
node.rotation = 90; //設(shè)置節(jié)點旋轉(zhuǎn)角度
node.scaleX = 2; //設(shè)置節(jié)點x軸縮放倍數(shù)
node.scaleY = 2; //設(shè)置節(jié)點y軸縮放倍數(shù)
node.setScale(2); //設(shè)置節(jié)點整體縮放倍數(shù)
node.width = 100; //設(shè)置節(jié)點寬度大小
node.height = 100;? //設(shè)置節(jié)點高度大小
node.setContentSize(100, 100); //設(shè)置節(jié)點寬高尺寸大小
node.anchorX = 1; //設(shè)置節(jié)點x軸錨點坐標(biāo)
node.anchorY = 0; //設(shè)置節(jié)點y軸錨點坐標(biāo)
node.setAnchorPoint(1, 0); //設(shè)置節(jié)點錨點坐標(biāo)
node.opacity = 128; //設(shè)置節(jié)點透明度大醒诺埂(0-255)
node.setOpacity(20); //設(shè)置節(jié)點透明度(0~255)
node.color = new cc.color(100,100,100,255); //設(shè)置節(jié)點顏色(R,G,B,透明度)
if (cc.isValid(this.label.node) ) //判定節(jié)點是否存在
node.destroy(); //銷毀節(jié)點
this.cannons = [];
this.cannons = node.getChildren(); //獲取所有子節(jié)點
this.cannons = node.getChildrenCount(); //獲取子節(jié)點數(shù)量
node.active = false; //關(guān)閉節(jié)點(隱藏節(jié)點)
cc.game.addPersistRootNode(myNode); //常駐節(jié)點(全局變量)
cc.game.removePersistRootNode(myNode); //取消常駐節(jié)點
4.動作操作
cc.show()//立即顯示
cc.hide ()//立即隱藏
cc.toggleVisibility()//顯隱切換
cc.fadeIn(1)//漸顯效果
cc.fadeOut(1)//漸隱效果
cc.delayTime(1)//等待1秒
node.runAction(cc.moveTo(1,0,0)); //移動到當(dāng)前節(jié)點(時間(s)尿瞭,X軸坐標(biāo)漂洋,Y 軸坐標(biāo))
node.runAction(cc.scaleTo(1,0.7,0.8));//縮放到當(dāng)前倍數(shù)節(jié)點(時間(s)斗搞,X軸倍數(shù),Y 軸倍數(shù))
node.runAction(cc.rotateTo(1,160,160));//旋轉(zhuǎn)到指定角度(時間(s)叶眉,X軸角度址儒,Y 軸角度)
node.runAction(cc.skewTo(1,5,-5));//變化節(jié)點傾斜度(時間(s),X軸傾斜度衅疙,Y 軸傾斜度)
node.runAction(cc.fadeTo(2,0));//變化當(dāng)前節(jié)點的透明度(時間(s)莲趣,透明度)
node.runAction(cc.tintTo(2,255,255,0));//變化當(dāng)前節(jié)點顏色(時間,R,G,B)
node.stopAllActions();//停止所有動作
//自定義動作
var action = cc.moveTo(2, 100, 100);// 創(chuàng)建一個移動動作
node.runAction(action);// 執(zhí)行動作
node.stopAction(action);// 停止一個動作
cc.sequence(action1,action2); //按順序連續(xù)執(zhí)行饱溢,先action1喧伞,后action2
cc.spawn(action1,action2); //同時執(zhí)行理朋,action1和action2一起執(zhí)行
cc.repeatForever(cc.sequence(action1,action2)); //一直重復(fù)括號里的動作
5.計時器
//只用1次的計時器,2秒后執(zhí)行
? ? ? ? this.scheduleOnce(function(){
? ? ? ? ? ? //一條或多條執(zhí)行語句??
? ? ? ? },2); //(function(){},時間(s))
//每隔5秒執(zhí)行1次
this.schedule(function(){
//一條或多條執(zhí)行語句
? ? ? ? },5);
//計算多次的計時器(1秒后,以0.1秒的執(zhí)行間隔绿聘,執(zhí)行10次)
this.schedule(function(){
//一條或多條執(zhí)行語句
? ? ? ? },0.1,10,1); //(function(){},間隔時間嗽上,次數(shù),多久后開始)
this.unscheduleAllCallbacks(this);//停止某組件的所有計時器
//自定義定時器執(zhí)行內(nèi)容(相比常規(guī)使用的定時器優(yōu)勢是:方便隨時開啟或關(guān)閉)
var cb= function(){
//do something
};
this.schedule(cb,1);//啟動定時器
this.unschedule(cb);//取消定時器
6.事件監(jiān)聽
(開始:'touchstart'熄攘,移動:'touchmove'兽愤,結(jié)束:'touchend',取消:'touchcancel')
node.on('touchstart',function(event){
this.doSomething();
},this);
var a = event.getID();//獲取觸點的ID
var a = event.getLocationX();//獲取觸摸點的坐標(biāo)X
var b = event.getLocationY();//獲取觸摸點的坐標(biāo)Y
cc.eventManager.addListener({
event: cc.EventListener.KEYBOARD/TOUCH_ONE_BY_ONE,myfunction},self.node);
7.定義全局變量
window.DEFAULT_IP = "192.168.1.1";//任意腳本里可定義全局變量
//任意腳本里可定義全局變量
window.G = {
a: null,
b: null,
};
//任意腳本里可訪問全局變量(切記定義全局變量的那個腳本已執(zhí)行過)
G.a = 0;
G.b = 0;
var something = require(‘something’)挪圾;
cc.game.addPersistRootNode(myNode);//常駐節(jié)點,必須位于層級的根節(jié)點(也可算全局節(jié)點吧)
module.exports = {
? ? ? ? ?config: 123
}
8.分辨率
//獲得設(shè)備分辨率
var b = cc.director.getWinSizeInPixels()
var bx = b.width
var by = b.height
cc.view.getCanvasSize().width;//獲得設(shè)備分辨率的寬度
cc.view.getCanvasSize().height;//獲得設(shè)備分辨率的高度
cc.director.setDisplayStats(true);//顯示幀數(shù)信息
9.音頻控制
cc.audioEngine.playMusic(this.BGAudio,true);//播放音樂(true代表循環(huán))
cc.audioEngine.stopMusic()//停止播放背景音樂
cc.audioEngine.playEffect(this.ClickAudio,false);//播放音效(false代表只播放一次)
cc.audioEngine.stopEffect(音效變量名);//停止指定音效(需要先把音效賦值給變量)
cc.audioEngine.AllEffects();//停止所有音效
cc.audioEngine.setMusicVolume(參數(shù));? //設(shè)置背景音樂的音量(該參數(shù)范圍是0到1)
cc.audioEngine.setEffectsVolume(參數(shù));? //設(shè)置音效的音量(該參數(shù)范圍是0到1)
10.存檔操作
cc.sys.localStorage.setItem('存儲標(biāo)識名',變量名);//存儲存檔數(shù)據(jù)
var a = cc.sys.localStorage.getItem('存儲標(biāo)識名');//讀取存檔數(shù)據(jù)
cc.sys.localStorage.removeItem('存儲標(biāo)識名');//擦除存檔數(shù)據(jù)
userData = {
? ? name: 'Tracer',
? ? level: 1,
? ? gold: 100
};
cc.sys.localStorage.setItem('userData', JSON.stringify(userData));//存取復(fù)雜對象數(shù)據(jù)
var userData = JSON.parse(cc.sys.localStorage.getItem('userData'));//讀取復(fù)雜對象數(shù)據(jù)
11.判斷平臺
cc.sys.isNative ?//是否是本地
cc.sys.isBrowser ?//是否是網(wǎng)頁
cc.sys.isMobile ?//是否是移動系統(tǒng)
cc.sys.platform ?//正在運行的平臺
cc.sys.language ?//當(dāng)前運行系統(tǒng)的語言
cc.sys.os ?//當(dāng)前正在運行的系統(tǒng)
cc.sys.OS_IOS ?//是否是IOS系統(tǒng)
cc.sys.OS_ANDROID ?//是否是android系統(tǒng)
cc.sys.OS_WINDOWS ?//是否是windows系統(tǒng)
cc.sys.openURL('Http://www.baidu.com'); ?//打開網(wǎng)頁
12.監(jiān)聽和發(fā)射事件
this.node.pauseSystemEvents(true);//暫停節(jié)點系統(tǒng)事件
this.node.resumeSystemEvents(true);//恢復(fù)節(jié)點系統(tǒng)事件
this.node.targetOff(this);//移除所有注冊事件
觸摸監(jiān)聽:開始'touchstart',移動'touchmove',結(jié)束'touchend',取消'touchcancel'
var pos = event.getLocation();//獲取觸摸點的坐標(biāo)(包含X和Y)
var x = event.getLocationX();//獲取觸摸點的X坐標(biāo)
var y = event.getLocationY();//獲取觸摸點的Y坐標(biāo)
var a = event.getID();//獲取觸點的ID
鼠標(biāo)監(jiān)聽:鼠標(biāo)按下'mousedown',移入節(jié)點'mouseenter',節(jié)點中移動'mousemove',移出節(jié)點'mouseleave,'松開鼠標(biāo)'mouseup'
var a = event.getScrollY();//獲取滾輪滾動的 Y 軸距離浅萧,只有滾動時才有效
var a = event.getLocation();//獲取鼠標(biāo)位置對象,對象包含 x 和 y 屬性
輸入框監(jiān)聽:獲得焦點'editing-did-began',文字變化'text-changed',失去焦點'editing-did-ended',按下回車'editing-return'
屬性變化監(jiān)聽:位置'position-changed',寬高 'size-changed',旋轉(zhuǎn)'rotation-changed',縮放'scale-changed'
ScrollView控件監(jiān)聽:滾動中'scrolling',停止?jié)L動'scroll-ended'
用戶自定義事件:
this.node.on('事件名',function,this);//注冊監(jiān)聽
this.node.emit('事件名');//發(fā)送監(jiān)聽廣播
this.node.off('事件名',function,this);//關(guān)閉監(jiān)聽
//注冊帶參數(shù)監(jiān)聽
this.node.on('事件名',function(event){
“具體方法函數(shù)內(nèi)容”
},this);
//發(fā)送帶參數(shù)的監(jiān)聽
this.node.emit('事件名',{id:1001});
cc.eventManager.addListener(listener, node);//添加事件
cc.eventManager.removeListener((listener);//移除事件
13.其他操作
cc.director.pause();//暫停
cc.director.resume();//繼續(xù)
cc.director.end();//退出整個應(yīng)用
cc.log(變量)? 或 console.log(something);//輸出想要的信息
let self = this;//鎖定當(dāng)前使用的this指向
node.getLocalZOrder();//層級獲取
node.setLocalZOrder(1);//層級改變
cc.find('canvas/map' + num)//讀取帶變量的路徑
cc.sys.openURL('Http://www.baidu.com');//打開網(wǎng)頁點擊鏈接加入群聊【cocos/unity交流群】