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

cocos2d-x lua 触摸事件

发布时间:2020-12-14 21:26:10 所属栏目:百科 来源:网络整理
导读:cocos2d-x lua 触摸事件 version: cocos2d-x 3.6 1.监听 function GameLayer : onEnter () local eventDispatcher = self:getEventDispatcher() local function onTouchBegan ( touch , event ) local locationInNode = self:convertToNodeSpace(touch:getLo

cocos2d-x lua 触摸事件

version: cocos2d-x 3.6

1.监听

function GameLayer:onEnter()
    local eventDispatcher = self:getEventDispatcher()

    local function onTouchBegan(touch,event)
        local locationInNode = self:convertToNodeSpace(touch:getLocation())
        local s = self:getContentSize()
        local rect = cc.rect(0,0,s.width,s.height)

        if cc.rectContainsPoint(rect,locationInNode) then
            self:setColor(cc.c3b(255,0))
            return true
        end

        return false    
    end

    local function onTouchMoved(touch,event)

    end

    local  function onTouchEnded(touch,event)
        self:setColor(cc.c3b(255,255,255))
    end

    local listener = cc.EventListenerTouchOneByOne:create()
    self._listener = listener
    listener:setSwallowTouches(true)

    listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
    listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )
    listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED )

    if 0 == self._fixedPriority then
        eventDispatcher:addEventListenerWithSceneGraphPriority(listener,self)
    else
        eventDispatcher:addEventListenerWithFixedPriority(listener,self._fixedPriority)
    end
end

2.移除

function TouchableSpriteWithFixedPriority:onExit()
    local eventDispatcher = self:getEventDispatcher()
    eventDispatcher:removeEventListener(self._listener)
end

3.注意

onEnter和onExit在lua中不会因节点别add和remove而直接被调用,当子节点被父节点add和remove时,会发送enter和exit的消息,所以需要再初始化节点的时候,监听消息,并在收到消息后调用onEnter或onExit。
http://www.52php.cn/article/p-dqqlstfl-dp.html

(编辑:李大同)

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

    推荐文章
      热点阅读