cocos creator基础-cc.Node(二)事件响应

触摸事件

1: 触摸事件类型: START, MOVED, ENDED(物体内), CANCEL(物体外);

2: 监听触摸事件: node.on(类型, callback, target(回掉函数的this), [useCapture]);

3: 关闭触摸事件: node.off(类型, callback, target(回掉函数的this), [useCapture]);

4: targetOff (target): 移除所有的注册事件;

5: 回掉函数的参数设置 function(t(cc.Touch))

6: cc.Touch: getLocation返回触摸的位置;getDelta返回距离上次的偏移;

7: cc.Event: stopPropagationImmediate/stopPropagation 停止事件的传递;

8: 事件冒泡: 触摸事件支持节点树的事件冒泡,会从当前前天往上一层一层的向父节点传送;

9: 完成物体跟随手指触摸的案例;

// touch事件

cc.Class({

    extends: cc.Component,

    properties: {

    },

    /*

    t: --> cc.Touch

    触摸的位置: 屏幕坐标,左小角(0, 0); getLocation();

    */

    on_touch_move: function(t) {

        // 位置

        console.log("cc.Node.EventType.TOUCH_MOVE called");

        console.log(t.getLocation());

        var w_pos = t.getLocation(); // cc.Vec2 {x, y}

        console.log(w_pos, w_pos.x, w_pos.y);

        // 距离上一次触摸变化了多少;

        var delta = t.getDelta(); // x, y各变化了多少cc.Vec2(x, y)

        this.node.x += delta.x;

        this.node.y += delta.y;

    },

    // use this for initialization

    onLoad: function () {

        // (1) 监听对应的触摸事件: 像引擎底层注册一个回掉函数,当有触摸事件发生的时候掉这个回掉函数;

        // cc.Node.EventType.TOUCH_START: 触摸开始

        // cc.Node.EventType.TOUCH_MOVE: 触摸移动

        // cc.Node.EventType.TOUCH_END: 触摸结束, (物体内部结束)

        // cc.Node.EventType.TOUCH_CANCEL: 触摸结束, (物体外部结束)

        /*

        (2) 回掉函数的格式  function(t)  --> cc.Touch对象触摸事件对象 {触摸信息,事件信息}

        call --> this, this指向谁就是这个target;你要绑那个对象作为你回掉函数的this, 可以为空

        function () {}.bind(this); 


        */

        this.node.on(cc.Node.EventType.TOUCH_START, function(t) {

            console.log("cc.Node.EventType.TOUCH_START called");

            // this 函数里面的this,

            // 停止事件传递

            t.stopPropagationImmediate(); 

        }, this);

        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.on_touch_move, this);


        this.node.on(cc.Node.EventType.TOUCH_END, function(t) {

            console.log("cc.Node.EventType.TOUCH_END called");

        }, this);

        this.node.on(cc.Node.EventType.TOUCH_CANCEL, function(t) {

            console.log("cc.Node.EventType.TOUCH_CANCEL called");

        }, this);

        // 移除

        // this.node.off(cc.Node.EventType.TOUCH_MOVE, this.on_touch_move, this);

        // 移除target上所有的注册事件

        // this.node.targetOff(this);

    },

    // called every frame, uncomment this function to activate update callback

    // update: function (dt) {

    // },

});

键盘事件

1:cc.SystemEvent.on(type, function, target, useCapture);

type: cc.SystemEvent.EventType.KEY_DOWN 按键按下;

cc.SystemEvent.EventType.KEY_UP 按键弹起;

2: cc.SystemEvent.on(type, function, target, useCapture);

3: 监听的时候,我们需要一个cc.SystemEvent类的实例,我们有一个全局的实例cc.systemEvent,小写开头

3: 键盘回掉函数: function(event) {

event.keyCode [cc.KEY.left, ...cc.KEY.xxxx]

// kb_event.js 注册引擎事件

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

        // },

        // ...

    },

    // use this for initialization

    // (1)向引擎注册一个事件类型的回掉函数, 

    // (2) 按键时间的类型: cc.SystemEvent.EventType.KEY_DOWN, cc.SystemEvent.EventType.KEY_UP;

    // (3) 配置的回掉函数: function(event) {} target, 目标

    // (4) 每一个按键,都会对应一个按键码, space, A, B, C, event对象 event.keyCode;

    // (5) cc.systemEvent 小写开头,不是大写, 大写SystemEvent类, systemEvent 全局一个实例

    onLoad: function () {

        // console.log(cc.systemEvent);

        // 按键被按下

        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.on_key_down, this);

        // 按键弹起

        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.on_key_up, this);

    },

    on_key_down: function(event) {

        switch(event.keyCode) {

            case cc.KEY.space:

                console.log("space key down!");

            break;

        }

    },

    on_key_up: function(event) {

        switch(event.keyCode) {

            case cc.KEY.space:

                console.log("space key up!");

            break;

        }

    },

    // called every frame, uncomment this function to activate update callback

    // update: function (dt) {

    // },

});

自定义事件

1:监听: this.node.on(“自定义事件名称”, function, target, useCapture);

2: 自派送: emit(“事件名称”, [detail]); 只有自己能够收到;

3: 冒泡派送: dispatchEvent(new cc.Event.EventCustom(“name”, 是否冒泡传递));

// custom_event.js 发送消息

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

        // },

        // ...

    },

    // use this for initialization

    onLoad: function () {

        // 接收者

        // 事件类型,是你自定义的字符串;

        // 回掉函数: function(e) {} e--> cc.Event.EventCustom的实例

        this.node.on("pkg_event", function(e){

            console.log("pkg_event", e.detail);

        }, this);

        // end

    },

    start: function() {

        // 派发者,只能传递给自己,不会向上传递

        this.node.emit("pkg_event", {blake: "huang"});

        // end  

        // 派送者,不只是发给自己,发给我们这个体系;

        // true/false, true向上传递, false不向向上传递

        var e = new cc.Event.EventCustom("pkg_event", true);

        e.detail = {blake: "huang"};

        this.node.dispatchEvent(e);

    },

    // called every frame, uncomment this function to activate update callback

    // update: function (dt) {

    // },

});

recv_event.js

// recv_event.js 父类接收消息

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

        // },

        // ...

    },

    // use this for initialization

    onLoad: function () {

        this.node.on("pkg_event", function(e){

            console.log("recv pkg_event", e.detail);

        }, this);

    },

    // called every frame, uncomment this function to activate update callback

    // update: function (dt) {

    // },

});

我也创建了个cocos creator的学习交流群欢迎大家一起来讨论点击链接加入群聊【cocos/unity交流群】

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,408评论 4 371
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,690评论 2 307
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,036评论 0 255
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,726评论 0 221
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,123评论 3 296
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,037评论 1 225
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,178评论 2 318
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,964评论 0 213
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,703评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,863评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,333评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,658评论 3 263
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,374评论 3 244
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,195评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,988评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,167评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,970评论 2 279

推荐阅读更多精彩内容