zoukankan      html  css  js  c++  java
  • (转)quick中自定义事件--EventProxy

    quick中的事件机制在官方的文档中已经讲解得很清楚了  查看这里
    这些方法能处理绝大多数的事件,但如果要实现自定义的事件(例如我们自己实现一个类,对该类添加自定义的事件处理)就需要对类进行扩展了。

    下面讲讲如何使用自定义(扩展)的事件机制。

    首先认识一下类EventProxy,就是这个类实现了自定义的消息处理
    该类有四个函数,分别是


    1

    2

    3

    4

    addEventListener

    removeEventListener

    removeAllEventListenersForEvent

    removeAllEventListeners


    根据名字就能知道这几个函数的作用

    下面以我们定义一个继承自Node的类来了解自定义事件的处理方法
    定义类EXNode如下


    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    local
    EXNode = class("EXNode", function()

        return
    display.newNode()

    end)

     

    function
    EXNode:ctor()

        cc.GameObject.extend(self):addComponent("components.behavior.EventProtocol"):exportMethods()

     

        cc.ui.UIPushButton.new({normal = "button/button_normal.png", pressed = "button/button_selected.png"})

            :align(display.CENTER, display.cx, display.cy)

            :onButtonClicked(function()

                self:getChildEvent()

            end)

            :addTo(self)

    end

     

    function
    EXNode:getChildEvent()

       self:dispatchEvent({name = "MY_NEWS"})

    end

     

    function
    EXNode:onEnter()

        self:setTouchEnabled(true)

    end

     

    function
    EXNode:onExit()

        self:removeAllEventListeners()

    end

     

    return
    EXNode


    最重要的是这句cc.GameObject.extend(self):addComponent("components.behavior.EventProtocol"):exportMethods()
    该句为EXNode类添加了扩展的事件处理方法,现在我们可以使用EventProxy中的函数了,通过这些函数我们可以让EXNode接收到自定义的消息然后进行处理
    在该类中我们创建一个按钮,用按钮来模拟我们游戏中的某些操作,当按钮按下的时候发送消息给其父节点EXNode,我们将该自定义的消息名字设置为MY_NEWS

    我们在Scene中使用EXNode


    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    local
    EXNode = import("..class.EXNode")

     

    local
    EventScene = class("EventScene", function()

        return
    display.newScene("EventScene")

    end)

     

    function
    EventScene:ctor()

        self.exNode = EXNode.new()

        self.exNode:addEventListener("MY_NEWS", handler(self, self.onMynews))

        self:addChild(self.exNode)

    end

     

    function
    EventScene:onMynews()

        printLog("INFO","父Node知道了子Node发送过来的消息")

    end

     

    function
    EventScene:onEnter()

    end

     

    function
    EventScene:onExit()

    end

     

    return
    EventScene



    在EventScene中创建一个EXNode对象,
    self.exNode:addEventListener("MY_NEWS", handler(self, self.onMynews))让exNode对象监听自定义的事件,
    从这句可以看到我们监听的事件信息名称为MY_NEWS(即EXNode中按钮点击后发送的自定义消息),回调函数为EventScene的onMynews函数

    测试程序,点击按钮模拟游戏操作,在EXNode内部分发出名称为MY_NEWS的消息,之后exNode对象就能够接收到消息,然后调用回调函数onMynews进行处理了。


    其实内容很简单,给新手入门用吧,大神自动忽略。

    第一次尝试写这个,如有错误,希望大家指出。 

     

    来源:http://www.cocoachina.com/bbs/read.php?tid-235255.html

     

    原文地址:https://blog.csdn.net/aa294194253/article/details/40385049

  • 相关阅读:
    代理模式第一篇
    命令模式第二篇
    windows service 定时
    postman之asp.net webapi
    qq第三方登录
    core2.1下的identity 汉化
    asp.net core2.0里的Nlog
    Unable to create an object of type 'ApplicationDbContext'.
    identity使用mysql
    二进制位运算解决状态值
  • 原文地址:https://www.cnblogs.com/wodehao0808/p/9103788.html
Copyright © 2011-2022 走看看