微信小遊戲-CocosCreator 基礎(八)
Button 勾選SCALE Enable Auto Grag :內建shader變灰 ,當按鈕不可用時 列印 F12 斷點除錯 :後續講解 js資料型別轉換 :http://www.runoob.com/js/js-type-conversion.html
undefined 與 null 的區別: 表面上 undefined 與 null 都是什麼都沒有的意思,但是實際上 undefined 是未定義(就是變數沒有初始化),null 是一個變數初始化了,但是什麼值都沒給,只給了一個空物件;進一步說,undefined 與 null是值相等,型別不相等。
typeof和instanceof 我們可以使用 typeof 來獲取一個變數是否存在,如 if(typeof a!="undefined"){},而不要去使用 if(a) 因為如果 a 不存在(未宣告)則會出錯。 正因為 typeof 遇到 null,陣列,物件時都會返回 object 型別,所以當我們要判斷一個物件是否是陣列時。 或者判斷某個變數是否是某個物件的例項則要選擇使用另一個關鍵語法 instanceof。 instanceof可通過 instanceof 操作符來判斷物件的具體型別,語法格式: var result = objectName instanceof objectType 返回布林值,如果是指定型別返回 true,否則返回 false: 例: arr = [1,2,3]; if(arr instanceof Array){ document.write("arr 是一個數組"); } else { document.write("arr 不是一個數組"); } ==================================================== cc .Button 繫結事件: 1.設定Button的元件=》在元件上設定方法和傳入的引數 2.獲得Button元件(兩個方法)沒有的新增Button元件=》新增響應函式 3.Button的節點size大小不能為0 ============================================= Buttonjs: cc.Class({ extends: cc.Component,
properties: { // foo: { // default: null, // The default value will be used only when the component attaching // to a node for the first time // url: cc.Texture2D, // optional, default is typeof default // serializable: true, // optional, default is true // visible: true, // optional, default is true // displayName: 'Foo', // optional // readonly: false, // optional, default is false // }, // ... // 直接在編輯器裡面繫結 button: { type: cc.Button, // default: null, }, },
// use this for initialization onLoad: function () { // 獲取button元件 this.start_button = this.node.getChildByName("ks_up").getComponent(cc.Button);
// 新增button元件 this.red_button = this.node.getChildByName("red_button").addComponent(cc.Button); // 新增一個響應函式 var click_event = new cc.Component.EventHandler(); click_event.target = this.node; click_event.component = "game_scene"; click_event.handler = "on_red_button_click"; click_event.customEventData = "red_button_data_77777"; // this.red_button.clickEvents = [click_event]; this.red_button.clickEvents.push(click_event); // end
// 程式碼觸發按鈕的響應事件,而不用自己去觸控 this.scheduleOnce(function() { var click_events = this.red_button.clickEvents; for(var i = 0; i < click_events.length; i ++) { var comp_env_handle = click_events[i]; // 在程式碼裡面觸發按鈕的響應函式 comp_env_handle.emit(["", "red_button_data_77777"]);//和引數對應 } }.bind(this), 3); // end },
on_red_button_click: function(e, custom) { console.log("on_red_button_click: ", custom); }, // 關卡按鈕1-10, 第幾關 // e 本次觸控的觸控事件 // customEventData is String; on_button_click: function(e, level) { level = parseInt(level); console.log("on_button_click called:", level); },
// called every frame, uncomment this function to activate update callback // update: function (dt) {
// }, });