cocos2d lua 设置触摸回调

前端之家收集整理的这篇文章主要介绍了cocos2d lua 设置触摸回调前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


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,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,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

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