Cocos2d-x Lua实现长按事件
local MainScene = class("MainScene", cc.load("mvc").ViewBase)

-- 弹出的层
local InfoLayer = class('InfoLayer',function ()
    return cc.Layer:create()
end)

function InfoLayer:ctor()
    self:setScale(0)
    local winSize = cc.Director:getInstance():getWinSize()
    local bg = cc.Sprite:create('HelloWorld.png')
    bg:setScale(0.8)
    bg:setPosition(winSize.width/2,winSize.height/2)
    self:addChild(bg)

    self:enableNodeEvents()
end

function InfoLayer:onEnter()
    self:runAction(cc.EaseBackOut:create(cc.ScaleTo:create(0.3,1)))
end

function InfoLayer:onExit()

end

-- 实现长按事件的节点
local touchNode = class('touchNode',function ()
    return cc.Node:create()
end)

local longPressTime = 1.5
local preAccTime = 0.5
function touchNode:ctor(Img)
    local bg = ccui.ImageView:create(Img)
    self:addChild(bg)
    local listener = cc.EventListenerTouchOneByOne:create()
    listener:registerScriptHandler(function(touch,event)
        self.beganPoint = touch:getLocation()
        local point = bg:convertToNodeSpace(self.beganPoint)
        local rect = cc.rect(0,0,bg:getContentSize().width,bg:getContentSize().height)
        if cc.rectContainsPoint(rect,point) then
            self.tick = 0
            self.moved = false
            self.ended = false
            local seq = cc.Sequence:create(cc.CallFunc:create(function() self:accumate() end),cc.DelayTime:create(preAccTime))
            self.acc = self:runAction(cc.RepeatForever:create(seq))
        end
        return true
    end,cc.Handler.EVENT_TOUCH_BEGAN)
    listener:registerScriptHandler(function(touch,event)
        self.movedPoint = touch:getLocation()
        self.moved = true
    end,cc.Handler.EVENT_TOUCH_MOVED)
    listener:registerScriptHandler(function(touch,event)
        self:stopAction(self.acc)
        self.ended = true
    end,cc.Handler.EVENT_TOUCH_ENDED)
    self:getEventDispatcher():addEventListenerWithSceneGraphPriority(listener,self)
    local function onNodeEvent(event)
        if event == 'exit' then
            self:getEventDispatcher():removeEventListener(listener)
        end     
    end
    self:registerScriptHandler(onNodeEvent)                 
end

function touchNode:accumate()
    self.tick = self.tick + preAccTime 
    if self.tick > longPressTime - preAccTime / 2 and self.tick <= longPressTime + preAccTime / 2 then
        self:stopAction(self.acc)
        if not self.ended then
            ---------long press---------
            local function longPress()
                local layer = InfoLayer.new()
                cc.Director:getInstance():getRunningScene():addChild(layer)              
            end
            if self.moved then
                if math.abs(self.beganPoint.x - self.movedPoint.x) < 10 and math.abs(self.beganPoint.y - self.movedPoint.y) < 10 then
                    longPress()
                end
            else
                longPress()
            end
        end
    end
end

function MainScene:ctor()
    local winSize = cc.Director:getInstance():getWinSize()
    local touchNode = touchNode.new('DS07.png')
    touchNode:setPosition(winSize.width/2,winSize.height/2)
    self:addChild(touchNode)
end

return MainScene

长按弹出层

阅读更多
版权声明:原创文章,欢迎转载。 https://blog.csdn.net/forestsenlin/article/details/51548616
文章标签: cocos2d-x lua
个人分类: Cocos2d-x
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Cocos2d-x Lua实现长按事件

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭