cocos2d lua 设置触摸回调
发布时间:2020-12-14 16:22:06 所属栏目:百科 来源:网络整理
导读: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,di
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(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |