一、場景加載
- cc.director.loadScene(‘場景名稱');//場景跳轉(zhuǎn)
- cc.director.preloadScene(‘場景名稱');//預加載場景
- cc.director.getScene();//獲取當前場景
二、查找節(jié)點
1,節(jié)點查找
- node = cc.find(“Canvas/bg”);//路徑訪問節(jié)點 性能消耗相對較大
- this.node.getChildByName(‘name');//名稱獲取子節(jié)點 性能消耗較小
- node.getComponent(cc.Label)//獲取節(jié)點上label屬性值
- this.node; //當前腳本節(jié)點
- this.node.parent; //父節(jié)點
- this.node.getChildByTag(100); //通過標簽獲取子節(jié)點
- cc.find(“game/test”,this.node); //通過指定節(jié)點下的路徑獲取節(jié)點
- this.node.children; //獲取所有子節(jié)點
- node.getChildren(); //獲取所有子節(jié)點
- this.node.childrenCount; //獲取子節(jié)點數(shù)量
- node.getChildrenCount(); //獲取子節(jié)點數(shù)量
- cc.director.getScene(); //獲取場景主節(jié)點
- var sprites = this.node.getComponentsInChildren(cc.Label);//遞歸查找自身及所有子節(jié)點中指定類型的組件
2,節(jié)點其他操作
- cc.instantiate(node);//克隆節(jié)點
- this.node.parent = cc.find(‘Canvas');//綁定父節(jié)點
- this.node.addChild(nodeName,zIndex,tag);//添加子節(jié)點,可設置層級和標簽
- this.node.removeChild(nodeName);//移除子節(jié)點
- this.node.removeChildByTag (nodeTag);//通過標簽移除子節(jié)點
- this.node.destroy();//銷毀節(jié)點
- this.node.isValid;//判定節(jié)點是否可用
- this.node.removeChild(newNode);//移除節(jié)點中指定的子節(jié)點
- this.node.removeChildByTag(100);//通過標簽移除節(jié)點中指定的子節(jié)點
- this.node.removeAllChildren();//移除所有子節(jié)點
- this.node.destroyAllChildren();//銷毀所有子節(jié)點
3,停止播放動作以及計時器
this.node.cleanup();//停止所有正在播放的動作和計時器
三、節(jié)點屬性設置
- node.getPositionX();或 getPositionY() //X軸或Y軸坐標
- node.getScaleX(); 或getScaleY() //X軸或Y軸縮放比例
- node.x = 100;//設置節(jié)點x軸坐標
- node.y = 100;//設置節(jié)點y軸坐標
- node.setPosition(x,y); //設置節(jié)點坐標
- node.rotation = 90; //設置節(jié)點旋轉(zhuǎn)角度
- node.scaleX = 2; //設置節(jié)點x軸縮放倍數(shù)
- node.scaleY = 2; //設置節(jié)點y軸縮放倍數(shù)
- node.setScale(2); //設置節(jié)點整體縮放倍數(shù)
- node.width = 100; //設置節(jié)點寬度大小
- node.height = 100; //設置節(jié)點高度大小
- node.setContentSize(100, 100); //設置節(jié)點寬高尺寸大小
- node.anchorX = 1; //設置節(jié)點x軸錨點坐標
- node.anchorY = 0; //設置節(jié)點y軸錨點坐標
- node.setAnchorPoint(1, 0); //設置節(jié)點錨點坐標
- node.opacity = 255; //設置節(jié)點透明度大?。?-255)
- node.setOpacity(20); //設置節(jié)點透明度(0~255)
- node.color = new cc.color(100,100,100,255); //設置節(jié)點顏色(R,G,B,透明度)
- cc.isValid(this.label.node) //判定節(jié)點是否存在
- node.active = false; //關(guān)閉節(jié)點(隱藏節(jié)點)
常駐節(jié)點
- cc.game.addPersistRootNode(myNode); //常駐節(jié)點(全局變量)
- cc.game.removePersistRootNode(myNode); //取消常駐節(jié)點
四、節(jié)點動作
- cc.show()//立即顯示
- cc.hide ()//立即隱藏
- cc.toggleVisibility()//顯隱切換
- cc.fadeIn(1)//漸顯效果
- cc.fadeOut(1)//漸隱效果
- cc.delayTime(1)//等待1秒
- node.runAction(cc.moveTo(1,0,0)); //移動到當前節(jié)點(時間(s),X軸坐標,Y 軸坐標)
- node.runAction(cc.scaleTo(1,0.7,0.8));//縮放到當前倍數(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));//變化當前節(jié)點的透明度(時間(s),透明度)
- node.runAction(cc.tintTo(2,255,255,0));//變化當前節(jié)點顏色(時間,R,G,B)
- node.stopAllActions();//停止所有動作
- var action = cc.moveTo(2, 100, 100);// 創(chuàng)建一個動作(moveTo是移動)
- node.runAction(action);// 執(zhí)行指定動作
- node.stopAction(action);// 停止指定動作
- cc.sequence(action1,action2); //按順序連續(xù)執(zhí)行
- cc.spawn(action1,action2); //同時執(zhí)行
- cc.repeatForever(cc.sequence(action1,action2)); //一直重復的動作
五、計時器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
start() { // 定時啟動 // 在2S以后啟動 this .scheduleOnce(() => { cc.log( "scheduleOnce" ) }, 2) // 頻率 次數(shù)+1 延遲 this .schedule(() => { cc.log( "schedule" ) }, 1, 3, 5) // 永遠執(zhí)行 let one = this .schedule(() => { cc.log( "schedule" ) }, 1, cc.macro.REPEAT_FOREVER, 2) // 清除所有定時 this .scheduleOnce(() => { cc.log( "scheduleOnce" ) this .unscheduleAllCallbacks() }, 5) let callb = function () { cc.log( "callb" ) } this .schedule(callb, 0.5) //默認永遠執(zhí)行 this .scheduleOnce(() => { cc.log( "scheduleOnce" ) this .unschedule(callb) }, 2) }, |
六、事件監(jiān)聽
(開始:‘touchstart',移動:‘touchmove',結(jié)束:‘touchend',取消:‘touchcancel')
1
2
3
|
node.on( 'touchstart' , function (event){ this .doSomething(); }, this ); |
- event.getID();//獲取觸點的ID
- event.getLocationX();//獲取觸摸點的坐標X
- event.getLocationY();//獲取觸摸點的坐標Y
1
2
|
cc.eventManager.addListener({ event: cc.EventListener.KEYBOARD/TOUCH_ONE_BY_ONE,myfunction},self.node); |
七、定義全局變量
window.global= “blobal string”;//任意腳本里可定義全局變量
1
2
3
4
|
window.G = { a: null , b: null , }; |
任意腳本里可訪問全局變量(前提是腳本已執(zhí)行過)
G.a = 0;
G.b = 0;
1
2
3
4
5
|
var something = require(‘something'); cc.game.addPersistRootNode(myNode); //常駐節(jié)點,必須位于層級的根節(jié)點 module.exports = { config: 123 } |
八、分辨率
獲得設備分辨率
- var equipment= cc.director.getWinSizeInPixels()
- var equipmentW= equipment.width
- var equipmentH= equipment.height
- cc.view.getCanvasSize().width;//獲得設備分辨率的寬度
- cc.view.getCanvasSize().height;//獲得設備分辨率的高度
- cc.director.setDisplayStats(true);//顯示幀數(shù)信息
九、音頻控制
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ù)); //設置背景音樂的音量(范圍是0到1)
cc.audioEngine.setEffectsVolume(參數(shù)); //設置音效的音量(范圍是0到1)
十、設備判斷
- cc.sys.isNative //是否是本地
- cc.sys.isBrowser //是否是網(wǎng)頁
- cc.sys.isMobile //是否是移動系統(tǒng)
- cc.sys.platform //正在運行的平臺
- cc.sys.language //當前運行系統(tǒng)的語言
- cc.sys.os //當前正在運行的系統(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)頁
十一、監(jiān)聽和發(fā)射事件
- this.node.pauseSystemEvents(true);//暫停節(jié)點系統(tǒng)事件
- this.node.resumeSystemEvents(true);//恢復節(jié)點系統(tǒng)事件
- this.node.targetOff(this);//移除所有注冊事件
1、觸摸監(jiān)聽
開始'touchstart',
移動'touchmove',
結(jié)束'touchend',
取消'touchcancel'
- var pos = event.getLocation();//獲取觸摸點的坐標(包含X和Y)
- var x = event.getLocationX();//獲取觸摸點的X坐標
- var y = event.getLocationY();//獲取觸摸點的Y坐標
- var a = event.getID();//獲取觸點的ID
2、鼠標監(jiān)聽
鼠標按下'mousedown',
移入節(jié)點'mouseenter',
節(jié)點中移動'mousemove',
移出節(jié)點'mouseleave,
‘松開鼠標'mouseup'
- event.getScrollY();//獲取滾輪滾動的 Y 軸距離,只有滾動時才有效
- event.getLocation();//獲取鼠標位置對象,對象包含 x 和 y 屬性
3、輸入框監(jiān)聽
獲得焦點'editing-did-began',
文字變化'text-changed',
失去焦點'editing-did-ended',
按下回車'editing-return'
4,屬性變化監(jiān)聽
位置'position-changed',
寬高 ‘size-changed',
旋轉(zhuǎn)'rotation-changed',
縮放'scale-changed'
5、ScrollView控件監(jiān)聽
滾動中'scrolling',
停止?jié)L動'scroll-ended'
6、用戶自定義事件
監(jiān)聽: this.node.on(“自定義事件名稱”, function(target) , this);
- this.node.on(‘事件名',function,this);//注冊監(jiān)聽
- this.node.emit(‘事件名');//發(fā)送監(jiān)聽廣播
- this.node.off(‘事件名',function,this);//關(guān)閉監(jiān)聽
自派送: emit(“事件名稱”, [detail]); 只有自己能夠收到
1
2
3
4
5
6
7
8
9
10
|
onLoad: function () { // 接收者 // 事件類型,是你自定義的字符串; // 回掉函數(shù): function(e) {} e--> cc.Event.EventCustom的實例 this .node.on( "pkg_event" , function (e) { console.log( "pkg_event" , e); }, this ); // 派發(fā)者,只能傳遞給自己,不會向上傳遞 this .node.emit( "pkg_event" , { name: "hanbao" }); }, |
冒泡派送: dispatchEvent(new cc.Event.EventCustom(“name”, 是否冒泡傳遞));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
onLoad: function () { // 接收者 // 事件類型,是你自定義的字符串; // 回掉函數(shù): function(e) {} e--> cc.Event.EventCustom的實例 this .node.on( "pkg_event" , function (e) { console.log( "pkg_event" , e.detail); }, this ); }, start: function () { this .node.emit( "pkg_event" , { name: "hanbao" }); //這里會派發(fā)一次給自己 // //這里派發(fā)給全局 發(fā)給這個體系; // true/false, true向上傳遞, false不向向上傳遞 var e = new cc.Event.EventCustom( "pkg_event" , true ); e.detail = { name: "haobao" }; this .node.dispatchEvent(e); }, |
補充:
- cc.director.pause();//暫停
- cc.director.resume();//繼續(xù)
- cc.director.end();//退出整個應用
- node.getLocalZOrder();//層級獲取
- node.setLocalZOrder(1);//層級改變
- cc.find(‘canvas/map' + num)//讀取帶變量的路徑
以上就是整理CocosCreator常用知識點的詳細內(nèi)容,更多關(guān)于CocosCreator知識點的資料請關(guān)注服務器之家其它相關(guān)文章!
原文鏈接:https://blog.csdn.net/qq_33302253/article/details/113789934