cocos2d lua 设置触摸回调

来源:互联网 发布:网络自制剧简称网剧 编辑:程序博客网 时间:2024/06/05 22:52


local MainScene = class("MainScene", function()
    return display.newScene("MainScene")
end)


function MainScene:ctor()
    self.layer = cc.Layer:create()
cc.ui.UILabel.new({
            UILabelType = 2, text = "Hello, World", size = 64})
        :align(display.CENTER, display.cx, display.cy)
        :addTo(self)
    self.map = cc.TMXTiledMap:create("test.tmx")
    self.layer:addChild(self.map, 0,1)
self.layer:addTo(self)

    local function onTouchesMoved(touches, event )
        --dump(event)
        local  n=#touches
        local diff = touches[1]:getDelta()
        local node = self.layer:getChildByTag(1)
        local currentPosX, currentPosY= node:getPosition()
        node:setPosition(cc.p(currentPosX + diff.x, currentPosY + diff.y))
    end
    

local listener = cc.EventListenerTouchAllAtOnce:create()

--关键的地方handler(self,self.onTouchesMoved1),直接设置listener:registerScriptHandler(self.onTouchesMoved1,cc.Handler.EVENT_TOUCHES_MOVED) 运行时会出错

    listener:registerScriptHandler(handler(self,self.onTouchesMoved1),cc.Handler.EVENT_TOUCHES_MOVED) 
    local eventDispatcher = self:getEventDispatcher()
    eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self.layer)


end




function MainScene:onTouchesMoved1(touches, event )
    --dump(event)
    local  n=#touches
    local diff = touches[1]:getDelta()
    local node = self.layer:getChildByTag(1)
    local currentPosX, currentPosY= node:getPosition()
    node:setPosition(cc.p(currentPosX + diff.x, currentPosY + diff.y))
end


function MainScene:onEnter()
end


function MainScene:onExit()
end


return MainScene
1 0
原创粉丝点击