整理CocosCreator常用知識(shí)點(diǎn)
一、場景加載
- cc.director.loadScene(‘場景名稱');//場景跳轉(zhuǎn)
- cc.director.preloadScene(‘場景名稱');//預(yù)加載場景
- cc.director.getScene();//獲取當(dāng)前場景
二、查找節(jié)點(diǎn)
1,節(jié)點(diǎn)查找
- node = cc.find(“Canvas/bg”);//路徑訪問節(jié)點(diǎn) 性能消耗相對(duì)較大
- this.node.getChildByName(‘name');//名稱獲取子節(jié)點(diǎn) 性能消耗較小
- node.getComponent(cc.Label)//獲取節(jié)點(diǎn)上label屬性值
- this.node; //當(dāng)前腳本節(jié)點(diǎn)
- this.node.parent; //父節(jié)點(diǎn)
- this.node.getChildByTag(100); //通過標(biāo)簽獲取子節(jié)點(diǎn)
- cc.find(“game/test”,this.node); //通過指定節(jié)點(diǎn)下的路徑獲取節(jié)點(diǎn)
- this.node.children; //獲取所有子節(jié)點(diǎn)
- node.getChildren(); //獲取所有子節(jié)點(diǎn)
- this.node.childrenCount; //獲取子節(jié)點(diǎn)數(shù)量
- node.getChildrenCount(); //獲取子節(jié)點(diǎn)數(shù)量
- cc.director.getScene(); //獲取場景主節(jié)點(diǎn)
- var sprites = this.node.getComponentsInChildren(cc.Label);//遞歸查找自身及所有子節(jié)點(diǎn)中指定類型的組件
2,節(jié)點(diǎn)其他操作
- cc.instantiate(node);//克隆節(jié)點(diǎn)
- this.node.parent = cc.find(‘Canvas');//綁定父節(jié)點(diǎn)
- this.node.addChild(nodeName,zIndex,tag);//添加子節(jié)點(diǎn),可設(shè)置層級(jí)和標(biāo)簽
- this.node.removeChild(nodeName);//移除子節(jié)點(diǎn)
- this.node.removeChildByTag (nodeTag);//通過標(biāo)簽移除子節(jié)點(diǎn)
- this.node.destroy();//銷毀節(jié)點(diǎn)
- this.node.isValid;//判定節(jié)點(diǎn)是否可用
- this.node.removeChild(newNode);//移除節(jié)點(diǎn)中指定的子節(jié)點(diǎn)
- this.node.removeChildByTag(100);//通過標(biāo)簽移除節(jié)點(diǎn)中指定的子節(jié)點(diǎn)
- this.node.removeAllChildren();//移除所有子節(jié)點(diǎn)
- this.node.destroyAllChildren();//銷毀所有子節(jié)點(diǎn)
3,停止播放動(dòng)作以及計(jì)時(shí)器
this.node.cleanup();//停止所有正在播放的動(dòng)作和計(jì)時(shí)器
三、節(jié)點(diǎn)屬性設(shè)置
- node.getPositionX();或 getPositionY() //X軸或Y軸坐標(biāo)
- node.getScaleX(); 或getScaleY() //X軸或Y軸縮放比例
- node.x = 100;//設(shè)置節(jié)點(diǎn)x軸坐標(biāo)
- node.y = 100;//設(shè)置節(jié)點(diǎn)y軸坐標(biāo)
- node.setPosition(x,y); //設(shè)置節(jié)點(diǎn)坐標(biāo)
- node.rotation = 90; //設(shè)置節(jié)點(diǎn)旋轉(zhuǎn)角度
- node.scaleX = 2; //設(shè)置節(jié)點(diǎn)x軸縮放倍數(shù)
- node.scaleY = 2; //設(shè)置節(jié)點(diǎn)y軸縮放倍數(shù)
- node.setScale(2); //設(shè)置節(jié)點(diǎn)整體縮放倍數(shù)
- node.width = 100; //設(shè)置節(jié)點(diǎn)寬度大小
- node.height = 100; //設(shè)置節(jié)點(diǎn)高度大小
- node.setContentSize(100, 100); //設(shè)置節(jié)點(diǎn)寬高尺寸大小
- node.anchorX = 1; //設(shè)置節(jié)點(diǎn)x軸錨點(diǎn)坐標(biāo)
- node.anchorY = 0; //設(shè)置節(jié)點(diǎn)y軸錨點(diǎn)坐標(biāo)
- node.setAnchorPoint(1, 0); //設(shè)置節(jié)點(diǎn)錨點(diǎn)坐標(biāo)
- node.opacity = 255; //設(shè)置節(jié)點(diǎn)透明度大?。?-255)
- node.setOpacity(20); //設(shè)置節(jié)點(diǎn)透明度(0~255)
- node.color = new cc.color(100,100,100,255); //設(shè)置節(jié)點(diǎn)顏色(R,G,B,透明度)
- cc.isValid(this.label.node) //判定節(jié)點(diǎn)是否存在
- node.active = false; //關(guān)閉節(jié)點(diǎn)(隱藏節(jié)點(diǎn))
常駐節(jié)點(diǎn)
- cc.game.addPersistRootNode(myNode); //常駐節(jié)點(diǎn)(全局變量)
- cc.game.removePersistRootNode(myNode); //取消常駐節(jié)點(diǎn)
四、節(jié)點(diǎn)動(dòng)作
- 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)到當(dāng)前節(jié)點(diǎn)(時(shí)間(s),X軸坐標(biāo),Y 軸坐標(biāo))
- node.runAction(cc.scaleTo(1,0.7,0.8));//縮放到當(dāng)前倍數(shù)節(jié)點(diǎn)(時(shí)間(s),X軸倍數(shù),Y 軸倍數(shù))
- node.runAction(cc.rotateTo(1,160,160));//旋轉(zhuǎn)到指定角度(時(shí)間(s),X軸角度,Y 軸角度)
- node.runAction(cc.skewTo(1,5,-5));//變化節(jié)點(diǎn)傾斜度(時(shí)間(s),X軸傾斜度,Y 軸傾斜度)
- node.runAction(cc.fadeTo(2,0));//變化當(dāng)前節(jié)點(diǎn)的透明度(時(shí)間(s),透明度)
- node.runAction(cc.tintTo(2,255,255,0));//變化當(dāng)前節(jié)點(diǎn)顏色(時(shí)間,R,G,B)
- node.stopAllActions();//停止所有動(dòng)作
- var action = cc.moveTo(2, 100, 100);// 創(chuàng)建一個(gè)動(dòng)作(moveTo是移動(dòng))
- node.runAction(action);// 執(zhí)行指定動(dòng)作
- node.stopAction(action);// 停止指定動(dòng)作
- cc.sequence(action1,action2); //按順序連續(xù)執(zhí)行
- cc.spawn(action1,action2); //同時(shí)執(zhí)行
- cc.repeatForever(cc.sequence(action1,action2)); //一直重復(fù)的動(dòng)作
五、計(jì)時(shí)器
start() { // 定時(shí)啟動(dòng) // 在2S以后啟動(dòng) this.scheduleOnce(() => { cc.log("scheduleOnce") }, 2) // 頻率 次數(shù)+1 延遲 this.schedule(() => { cc.log("schedule") }, 1, 3, 5) // 永遠(yuǎn)執(zhí)行 let one = this.schedule(() => { cc.log("schedule") }, 1, cc.macro.REPEAT_FOREVER, 2) // 清除所有定時(shí) this.scheduleOnce(() => { cc.log("scheduleOnce") this.unscheduleAllCallbacks() }, 5) let callb = function () { cc.log("callb") } this.schedule(callb, 0.5) //默認(rèn)永遠(yuǎn)執(zhí)行 this.scheduleOnce(() => { cc.log("scheduleOnce") this.unschedule(callb) }, 2) },
六、事件監(jiān)聽
(開始:‘touchstart',移動(dòng):‘touchmove',結(jié)束:‘touchend',取消:‘touchcancel')
node.on('touchstart',function(event){ this.doSomething(); },this);
- event.getID();//獲取觸點(diǎn)的ID
- event.getLocationX();//獲取觸摸點(diǎn)的坐標(biāo)X
- event.getLocationY();//獲取觸摸點(diǎn)的坐標(biāo)Y
cc.eventManager.addListener({ event: cc.EventListener.KEYBOARD/TOUCH_ONE_BY_ONE,myfunction},self.node);
七、定義全局變量
window.global= “blobal string”;//任意腳本里可定義全局變量
window.G = { a: null, b: null, };
任意腳本里可訪問全局變量(前提是腳本已執(zhí)行過)
G.a = 0;
G.b = 0;
var something = require(‘something'); cc.game.addPersistRootNode(myNode);//常駐節(jié)點(diǎn),必須位于層級(jí)的根節(jié)點(diǎn) module.exports = { config: 123 }
八、分辨率
獲得設(shè)備分辨率
- var equipment= cc.director.getWinSizeInPixels()
- var equipmentW= equipment.width
- var equipmentH= equipment.height
- cc.view.getCanvasSize().width;//獲得設(shè)備分辨率的寬度
- cc.view.getCanvasSize().height;//獲得設(shè)備分辨率的高度
- 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ù)); //設(shè)置背景音樂的音量(范圍是0到1)
cc.audioEngine.setEffectsVolume(參數(shù)); //設(shè)置音效的音量(范圍是0到1)
十、設(shè)備判斷
- cc.sys.isNative //是否是本地
- cc.sys.isBrowser //是否是網(wǎng)頁
- cc.sys.isMobile //是否是移動(dòng)系統(tǒng)
- cc.sys.platform //正在運(yùn)行的平臺(tái)
- cc.sys.language //當(dāng)前運(yùn)行系統(tǒng)的語言
- cc.sys.os //當(dāng)前正在運(yùn)行的系統(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é)點(diǎn)系統(tǒng)事件
- this.node.resumeSystemEvents(true);//恢復(fù)節(jié)點(diǎn)系統(tǒng)事件
- this.node.targetOff(this);//移除所有注冊事件
1、觸摸監(jiān)聽
開始'touchstart',
移動(dòng)'touchmove',
結(jié)束'touchend',
取消'touchcancel'
- var pos = event.getLocation();//獲取觸摸點(diǎn)的坐標(biāo)(包含X和Y)
- var x = event.getLocationX();//獲取觸摸點(diǎn)的X坐標(biāo)
- var y = event.getLocationY();//獲取觸摸點(diǎn)的Y坐標(biāo)
- var a = event.getID();//獲取觸點(diǎn)的ID
2、鼠標(biāo)監(jiān)聽
鼠標(biāo)按下'mousedown',
移入節(jié)點(diǎn)'mouseenter',
節(jié)點(diǎn)中移動(dòng)'mousemove',
移出節(jié)點(diǎn)'mouseleave,
‘松開鼠標(biāo)'mouseup'
- event.getScrollY();//獲取滾輪滾動(dòng)的 Y 軸距離,只有滾動(dòng)時(shí)才有效
- event.getLocation();//獲取鼠標(biāo)位置對(duì)象,對(duì)象包含 x 和 y 屬性
3、輸入框監(jiān)聽
獲得焦點(diǎn)'editing-did-began',
文字變化'text-changed',
失去焦點(diǎn)'editing-did-ended',
按下回車'editing-return'
4,屬性變化監(jiān)聽
位置'position-changed',
寬高 ‘size-changed',
旋轉(zhuǎn)'rotation-changed',
縮放'scale-changed'
5、ScrollView控件監(jiān)聽
滾動(dòng)中'scrolling',
停止?jié)L動(dòng)'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]); 只有自己能夠收到
onLoad: function () { // 接收者 // 事件類型,是你自定義的字符串; // 回掉函數(shù): function(e) {} e--> cc.Event.EventCustom的實(shí)例 this.node.on("pkg_event", function (e) { console.log("pkg_event", e); }, this); // 派發(fā)者,只能傳遞給自己,不會(huì)向上傳遞 this.node.emit("pkg_event", { name: "hanbao" }); },
冒泡派送: dispatchEvent(new cc.Event.EventCustom(“name”, 是否冒泡傳遞));
onLoad: function () { // 接收者 // 事件類型,是你自定義的字符串; // 回掉函數(shù): function(e) {} e--> cc.Event.EventCustom的實(shí)例 this.node.on("pkg_event", function (e) { console.log("pkg_event", e.detail); }, this); }, start: function () { this.node.emit("pkg_event", { name: "hanbao" }); //這里會(huì)派發(fā)一次給自己 // //這里派發(fā)給全局 發(fā)給這個(gè)體系; // true/false, true向上傳遞, false不向向上傳遞 var e = new cc.Event.EventCustom("pkg_event", true); e.detail = { name: "haobao" }; this.node.dispatchEvent(e); },
補(bǔ)充:
- cc.director.pause();//暫停
- cc.director.resume();//繼續(xù)
- cc.director.end();//退出整個(gè)應(yīng)用
- node.getLocalZOrder();//層級(jí)獲取
- node.setLocalZOrder(1);//層級(jí)改變
- cc.find(‘canvas/map' + num)//讀取帶變量的路徑
以上就是整理CocosCreator常用知識(shí)點(diǎn)的詳細(xì)內(nèi)容,更多關(guān)于CocosCreator知識(shí)點(diǎn)的資料請關(guān)注腳本之家其它相關(guān)文章!
- 詳解cocoscreater預(yù)制體prefab
- 如何在CocosCreator中利用常駐節(jié)點(diǎn)做圖層管理
- 游戲開發(fā)中如何使用CocosCreator進(jìn)行音效處理
- CocosCreator ScrollView優(yōu)化系列之分幀加載
- 詳解CocosCreator項(xiàng)目結(jié)構(gòu)機(jī)制
- 如何使用CocosCreator對(duì)象池
- CocosCreator如何實(shí)現(xiàn)劃過的位置顯示紋理
- 全面講解CocosCreator熱更新
- CocosCreator經(jīng)典入門項(xiàng)目之flappybird
- CocosCreator通用框架設(shè)計(jì)之網(wǎng)絡(luò)
- 如何用CocosCreator實(shí)現(xiàn)射擊小游戲
- 怎樣在CocosCreator中使用游戲手柄
相關(guān)文章
使用swiper自定義分頁點(diǎn)擊跳轉(zhuǎn)指定頁面
這篇文章主要介紹了使用swiper自定義分頁點(diǎn)擊跳轉(zhuǎn)指定頁面方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04動(dòng)態(tài)改變div的z-index屬性的簡單實(shí)例
這篇文章介紹了動(dòng)態(tài)改變div的z-index屬性的簡單實(shí)例,有需要的朋友可以參考一下2013-08-08聊聊Javascript中try catch的2個(gè)作用
try...catch 可以測試代碼中的錯(cuò)誤,try 部分包含需要運(yùn)行的代碼,而catch部分包含錯(cuò)誤發(fā)生時(shí)運(yùn)行的代碼,這篇文章主要給大家介紹了關(guān)于Javascript中try catch的2個(gè)作用,需要的朋友可以參考下2021-09-09JavaScript遍歷查找數(shù)組中最大值與最小值的方法示例
這篇文章主要介紹了JavaScript遍歷查找數(shù)組中最大值與最小值的方法,結(jié)合實(shí)例形式分析了javascript基于數(shù)組遍歷、判斷實(shí)現(xiàn)最大值與最小值計(jì)算的相關(guān)操作技巧,需要的朋友可以參考下2019-05-05avalonjs實(shí)現(xiàn)仿微博的圖片拖動(dòng)特效
JavaScript實(shí)現(xiàn)仿微博的圖片拖動(dòng)特效,貌似這些天有不少朋友需要這功能,今天發(fā)現(xiàn)這款是js制作的好,不敢獨(dú)享,希望需要的朋友喜歡哦。2015-05-05JavaScript數(shù)組中reduce方法的應(yīng)用詳解
JavaScript 中的reduce()方法可以用于將數(shù)組元素匯總為單個(gè)值,,所以本文為大家整理了一些JavaScript數(shù)組中reduce方法的應(yīng)用,需要的可以參考一下2023-07-07