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) {
// },
});