cocosCreator基礎知識總結

weixin_34146805發表於2016-10-27

❤️簡單的整理

給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的整數

相關文章