Cocos2d-x Lua实现长按事件

前端之家收集整理的这篇文章主要介绍了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://www.f2er.com/cocos2dx/339644.html

猜你在找的Cocos2d-x相关文章