cc.Button

1:添加按钮的方法
(1)直接创建带Button组件的节点;
(2)先创建节点,再添加组件;
2:按钮组件, 按钮是游戏中最常用的组件, 点击然后响应事件;
3: 按钮的过渡效果:
    过渡: 普通状态, 鼠标滑动到物体上, 按下状态, 禁用状态
(1)没有过渡,只有响应事件;
(2)颜色过渡, 过渡效果中使用颜色;
(3)精灵过渡,使用图片过渡;
(4)缩放过渡, 选项,在disable的时候是否置灰;
4: 按钮禁用;
5: 按钮添加响应事件 --> 节点-->组件 --> 代码的函数;
6: 按钮传递自定义参数; ---> 字符串对象;
7: Button响应这个触摸点击,所以Button所挂的这个节点,一定要有大小,如果你向大小(0, 0)的节点上,挂一个Button,这个是无法响应点击事件;

代码使用cc.Button
1: 代码添加/获取cc.Button组件;
2: 代码里面添加按钮的响应事件;
3: 代码触发按钮指定的回掉函数;
4: Component.EventHandler
var eventHandler = new cc.Component.EventHandler();
eventHandler.target = newTarget;
eventHandler.component = "MainMenu";
eventHandler.handler = "OnClick";
eventHandler.customEventData = "my data";
eventHandler.emit(["param1", "param2", ....]);

    // 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_6666"]);
}
}.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);
},
btn1_click: function () {
console.log("this is button click ");
},

cc.Button的更多相关文章

  1. Cocos Creator cc.Button (脚本事件内容)

    cc.Class({extends: cc.Component,properties: {}, onLoad: function () { var clickEventHandler = new cc ...

  2. cc.Lable组件,RichText组件,AudioSouce组件的使用

    一.cc.Lable组件的使用 1.创建Label的方法 a.通过菜单直接创建Label组件:b.先创建节点,然后在节点上绑定Label组件即可. 2.Label 面板上的属性 String => ...

  3. Sprite组件和Button组件的使用

    一.Sprint组件的使用 1.游戏中显示一张图片,通常我们称之为"精灵" sprite 2.cocos creator如果需要显示一个图片,那么需要在节点上挂一个精灵组件,为这个 ...

  4. 获取Button脚本挂载的事件名

    (function(){ var Super = function(){}; Super.prototype = cc.Button.prototype; //实例化原型 Super.prototyp ...

  5. cocos creator 重写源码按钮Button点击音频封装

    (function(){ var Super = function(){}; Super.prototype = cc.Button.prototype; //实例化原型 Super.prototyp ...

  6. Cocos Creator 为Button添加事件的两种方法

    Button添加事件 Button 目前只支持 Click 事件,即当用户点击并释放 Button 时才会触发相应的回调函数.通过脚本代码添加回调方法一这种方法添加的事件回调和使用编辑器添加的事件回调 ...

  7. cc.Component

    组件入口函数1: onLoad: 组件加载的时候调用, 保证了你可以获取到场景中的其他节点,以及节点关联的资源数据;2: start: 也就是第一次执行 update 之前触发;3: update(d ...

  8. UI 组件 | Button

    最近在与其他自学 Cocos Creator 的小伙伴们交流过程中,发现许多小伙伴对基础组件的应用并不是特别了解,自己在编写游戏的过程中也经常对某个属性或者方法的用法所困扰,而网上也没有比较清晰的用法 ...

  9. iOS 根据字符串来定位地址

    - (void)viewDidLoad { [super viewDidLoad]; self.geocoder = [[CLGeocoder alloc]init]; // 设置地图可缩放 self ...

随机推荐

  1. bzoj 3629: [JLOI2014]聪明的燕姿【线性筛+dfs】

    数论+爆搜 详见这位大佬https://blog.csdn.net/eolv99/article/details/39644419 #include<iostream> #include& ...

  2. bzoj 4568: [Scoi2016]幸运数字【树链剖分+线段树+线性基】

    一眼做法,好处是好想好写坏处是常数大,容易被卡(bzoj loj 洛谷开O2 能AC,不开有90分-- 大概就是树剖之后维护线段树,在线段树的每个节点上上维护一个线性基,暴力\( 60^2 \)的合并 ...

  3. bzoj 1042: [HAOI2008]硬币购物【容斥原理+dp】

    当然是容斥啦. 用dp预处理出\( f[i] \),表示在\( i \)价格时不考虑限制的方案数,转移方程是\( f[i]+=f[i-c[j]] \),用状压枚举不满足的状态容斥一下即可. #incl ...

  4. P4055 [JSOI2009]游戏

    传送门 把这个图给黑白染色然后建二分图,如果有完备匹配那么就gg,否则放在所有的非匹配点都可以 简单来说的话就是放在非匹配点,那么对手的下一步必定移到一个匹配点,然后自己可以把它移到这个匹配点所匹配的 ...

  5. 第二篇(那些JAVA程序BUG中的常见单词)

    Cannot instantiate the type xxx 无法实例化类型xxx instantiate 实例化

  6. 2018SCin tsyzDay2 模拟赛-动态规划(简单的)

    内心OS:简单?????还是我太弱了. 期望得分:100+100+0+0+0+0+随机暴力的点==200 实际得分:0+100+10+0+10+0==120 您知道我第一题为什么错了嘛??文件在混乱中 ...

  7. 安装privoxy后curl 操作

    如果讲privoxy服务关掉 sudo /etc/init.d/privoxy restart 这时候发现 curl www.baidu.com 也没法正常工作,直接显示的是无法连接到相应端口还是 主 ...

  8. Bryce1010的操作系统课程设计

    https://download.csdn.net/download/fire_to_cheat_/10221003 上面是课程设计的代码,下载需要一些积分. 1.作业调度 2.磁盘调度 常见的磁盘调 ...

  9. 利用正则将xml数据解析为数组

    function xml_to_array( $xml ) { $reg = '/<(\w+)[^>]*>([\x00-\xFF]*)<\/\1>/'; if(preg_ ...

  10. 关于python2.7的md5加密遇到的问题(TypeError: Unicode-objects must be encoded before hashing)

    https://blog.csdn.net/u012087740/article/details/48439559 import hashlib import sys def md5s(): m=ha ...