加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx 事件处理机制

发布时间:2020-12-14 16:58:36 所属栏目:百科 来源:网络整理
导读:事件处理机制: 1.事件 2.事件源 3.事件处理者1.事件 (EventTouch 触摸事件 EventMouse 鼠标事件 EventKeyboard 键盘事件 EventAcceleration 加速度事件 EventCustom 自定义事件)2.事件源 精灵 层 菜单 等节点对象(事件的发出者)3.事件处理者 (EventList
事件处理机制:  1.事件  2.事件源  3.事件处理者


1.事件  (EventTouch 触摸事件  EventMouse  鼠标事件 EventKeyboard 键盘事件  EventAcceleration  加速度事件  EventCustom  自定义事件)
2.事件源  精灵  层  菜单  等节点对象(事件的发出者)
3.事件处理者  (EventListener类) 它的子类有EventTouchOneByOne 单点触摸监听器 EventTouchAllAtOnce 多点触摸监听器  EventListenerKeyboard 键盘事件监听器    EnventListenerMouse  鼠标事件监听器  EventListenerCustom 自定义事件监听器 EventListenerAcceleration 加速器时间监听器




事件分发器 EventDispatcher:  事件和事件监听器之间的一一对应的联系,需要用事件分发器这样的一个类进行管理。他负责将监听器和事件进行绑定或解除绑定(注册和注销)


有两个函数可以用来绑定:
1.addEventListenerWithFixedPriority(Listener,FixedPriority)  通过指定的优先级来来决定响应的优先级别
2.addEventListenerWithGraphPriority(Listener,GraphPriority)  通过节点的显示优先级来决定响应的优先级别


注销 removeEventListener(listener)  removeAllEventListener()  removeCustomEventListener(customEventListenerName)


当Node销毁时,监<span style="font-family: Arial,Helvetica,sans-serif;">听器也会销毁掉</span><pre name="code" class="plain">function MainLayer:init()
    --主界面背景
    local bg = ccui.ImageView:create("UI/bg_hud.jpg")
    bg:setAnchorPoint(0.5,0.5)
    bg:setPosition(GameTools.winCenter())
    self:addChild(bg)
    
    local img001 = ccui.ImageView:create("UI/bg_menu.png")
    img001:setAnchorPoint(0,0)
    img001:setPosition(200,200)
    bg:addChild(img001)
    
    self.touchRect_up = cc.rect(0,568,640,568)
    self.touchRect_down = cc.rect(0,568)
    
    local  listenner_up = cc.EventListenerTouchOneByOne:create()
    listenner_up:setSwallowTouches(true)
    
    local  listenner_down = cc.EventListenerTouchOneByOne:create()
    listenner_down:setSwallowTouches(true)
    
    local imageLine = ccui.ImageView:create("UI/white.png")
    imageLine:setScale(640,1)
    imageLine:setAnchorPoint(0.5,0.5)
    imageLine:setPosition(0,568)
    bg:addChild(imageLine)


    ----up
    listenner_up:registerScriptHandler(function(touch,event)
        if not cc.rectContainsPoint(self.touchRect_up,cc.p(touch:getLocation().x,touch:getLocation().y) ) then
            return false
        end
        
        print("up"..touch:getLocation().x,touch:getLocation().y)
        return true
        
    end,cc.Handler.EVENT_TOUCH_BEGAN)
    
    listenner_up:registerScriptHandler(function(touch,event)
        print("up"..touch:getLocation().x,touch:getLocation().y)
    end,cc.Handler.EVENT_TOUCH_MOVED )
    
    listenner_up:registerScriptHandler(function(touch,cc.Handler.EVENT_TOUCH_ENDED )

    local eventDispatcher = img001:getEventDispatcher()  
    eventDispatcher:addEventListenerWithSceneGraphPriority(listenner_up,img001)
    
    
    ----down
    listenner_down:registerScriptHandler(function(touch,event)
        if not cc.rectContainsPoint(self.touchRect_down,touch:getLocation().y) ) then
            return false
        end

        print("down"..touch:getLocation().x,touch:getLocation().y)
        return true

    end,cc.Handler.EVENT_TOUCH_BEGAN)

    listenner_down:registerScriptHandler(function(touch,event)
        print("down"..touch:getLocation().x,cc.Handler.EVENT_TOUCH_MOVED )

    listenner_down:registerScriptHandler(function(touch,cc.Handler.EVENT_TOUCH_ENDED )


    local eventDispatcher = img001:getEventDispatcher()  
    eventDispatcher:addEventListenerWithSceneGraphPriority(listenner_down,img001)



end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读