关闭

Cocos2d-x Lua实现长按事件

标签: cocos2d-xlua
681人阅读 评论(0) 收藏 举报
分类:
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

长按弹出层

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:20052次
    • 积分:401
    • 等级:
    • 排名:千里之外
    • 原创:19篇
    • 转载:12篇
    • 译文:0篇
    • 评论:0条
    文章分类