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

cocos2dx 事件监听

发布时间:2020-12-14 21:06:11 所属栏目:百科 来源:网络整理
导读:local btnSp=cc.Sprite:create("icon.png") ly:addChild(btnSp) btnSp:setPosition(600,100) local function onTouchBegan(touch,event) local locationInNode = btnSp:convertToNodeSpace(touch:getLocation()) local s = btnSp:getContentSize() local rec
    local btnSp=cc.Sprite:create("icon.png")
    ly:addChild(btnSp)
    btnSp:setPosition(600,100)
    
    local function onTouchBegan(touch,event)
        
        local locationInNode = btnSp:convertToNodeSpace(touch:getLocation())
        local s = btnSp:getContentSize()
        local rect = cc.rect(0,s.width,s.height)

        if cc.rectContainsPoint(rect,locationInNode) then
            print("------touch  me--------")
            return true
        end

        return false    
    end

    local function onTouchMoved(touch,event)
        print("----move----")
    end

    local  function onTouchEnded(touch,event)
           print("----end----")
    end
    
    local eventDispatcher = ly:getEventDispatcher()
        
    local listener = cc.EventListenerTouchOneByOne:create()
    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 )
 
    eventDispatcher:addEventListenerWithSceneGraphPriority(listener,btnSp)
    

(编辑:李大同)

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

    推荐文章
      热点阅读