??簡單的整理
給sprite動態改變圖片
首先將存放圖片最外層文件夾命名為resources
changeBj: function(){
var url = 'globalUI/video/gVideoPlayClick';
var _this = this;
cc.loader.loadRes(url,cc.SpriteFrame,function(err,spriteFrame){
_this.isPlay.spriteFrame = spriteFrame;
});
}
****跳轉****
cc.director.loadScene('HomePage');
****打印****
cc.log(變量);//輸出日志(若輸出固定文本,需要加上“”)
cc.director.loadScene('場景名稱');//場景跳轉
*****節點的一些常用功能*****
var a = cc.find("Canvas/1").getComponent(cc.Sprite);//獲取組件(該組件的getComponent里的類型必須存在)
var a = this.node.getPositionX();//獲取節點的X軸位置
this.node.setPosition(x,y);//設置節點位置
this.node.setOpacity(20);//設置節點透明度(0~255)
this.node.color = new cc.color(100,100,100,255);//設置節點顏色(R,G,B,透明度)
****動畫效果****
this.node.runAction(cc.moveTo(1,cc.p(0,0)));//移動當前節點(移動指定距離用moveBy)
this.node.runAction(cc.scaleTo(1,0.7,0.8));/縮放當前節點
this.node.runAction(cc.rotateTo(1,160,160));//旋轉當前節點(旋轉指定角度用rotateBy)
this.node.stopAllActions();//停止所有動作
if (cc.isValid(this.label.node) )//判定節點是否存在
this.node.destroy();//銷毀節點
*****計時器的一些運用*****
//計算1次的計時器,2秒后執行
this.scheduleOnce(function(){
this.doSomething();
},2);
//每隔5秒執行1次
this.schedule(function(){
this.doSomething();
},5);
//計算多次的計時器(1秒后,以0.1秒的執行間隔,執行10次)
this.schedule(function(){
this.doSomething();
},0.1,10,1);
this.unscheduleAllCallbacks(this);//停止某組件的所有計時器
*****音頻的一些控制*****
cc.audioEngine.playMusic(this.BGAudio,true);//播放音樂(true代表循環)
cc.audioEngine.stopMusic()//停止播放背景音樂
cc.audioEngine.playEffect(this.ClickAudio,false);//播放音效(false代表只播放一次)
cc.audioEngine.stopEffect(音效變量名);//停止指定音效(需要先把音效賦值給變量)
cc.audioEngine.AllEffects();//停止所有音效
cc.audioEngine.setMusicVolume(參數); //設置背景音樂的音量(該參數范圍是0到1)
cc.audioEngine.setEffectsVolume(參數); //設置音效的音量(該參數范圍是0到1)
******數據存儲******
cc.sys.localStorage.setItem('存儲標識名',變量名);//存儲存檔數據
var a = cc.sys.localStorage.getItem('存儲標識名');//讀取存檔數據
cc.sys.localStorage.removeItem('存儲標識名');//擦除存檔數據
this.node.getLocalZOrder();//層級獲取
this.node.setLocalZOrder(1);//層級改變
****觸摸監聽(START:開始,MOVE:移動,END:結束,CANCEL:取消)****
this.node.on(cc.Node.EventType.TOUCH_MOVE,function(event){
this.doSomething();
},this);
var a = getID();//獲取觸點的ID
var a = event.getLocationX();//獲取觸摸點的坐標X
cc.find('canvas/map' + num)//讀取帶變量的路徑
****定義數組****
var a= ['java','c++','c#'];
var a={}
var a=new Array(40);
****獲得設備分辨率****
var b = cc.director.getWinSizeInPixels()
var bx = b.width
var by = b.height
node.active = false;//激活節點
****包含其他腳本***
const Polyglot = require('polyglot');
Polyglot.a = 1;
****任意腳本里可定義全局變量****
window.G = {
a: null,
b: null,
};
//任意腳本里可訪問全局變量(切記定義全局變量的那個腳本已執行過)
G.a = 0;
G.b = 0;
****函數****
Math.round(num)//四舍五入
Math.floor(num)//小于等級num的整數
Math.ceil(num)//大于等級num的整數