监听触摸事件
function MainScene:onCreate()

    self.uiRoot = cc.CSLoader:createNode("MainScene.csb")
    self.uiRoot:addTo(self)

    self.myImage = self.uiRoot:getChildByName("HelloWorld_2")

    local imgae = self.uiRoot:getChildByName("Image_2")
    imgae:setVisible(false)

    self.spriteFrame = cc.SpriteFrameCache:getInstance()
    self.spriteFrame:addSpriteFrames("plist/testPlist.plist")

    self.sprite = cc.Sprite:createWithSpriteFrameName("logo/HelloWorld.png")
    self:addChild(self.sprite)
    self.sprite:setPosition(cc.p(display.cx,display.cy - 200))

    local listener = cc.EventListenerTouchOneByOne:create()
    listener:setSwallowTouches(true)
    listener:registerScriptHandler(handler(self,self.touchBegin), cc.Handler.EVENT_TOUCH_BEGAN)
    listener:registerScriptHandler(handler(self,self.touchMove),cc.Handler.EVENT_TOUCH_MOVED)
    listener:registerScriptHandler(handler(self,self.touchEnd),cc.Handler.EVENT_TOUCH_ENDED)

    local dispatch = cc.Director:getInstance():getEventDispatcher()
    dispatch:addEventListenerWithSceneGraphPriority(listener,self.sprite)

end

function MainScene:touchBegin(touch,event)
    dump(touch:getLocation(),"touch")  --点击位置
    local node = event:getCurrentTarget() --监听目标,不已目标的大小为触摸区域,无论怎么样都是整个屏幕
    return true --返回true吞没事件 move和end就可以触发了
end

function MainScene:touchMove(touch,event)
    print("7777")
end

function MainScene:touchEnd(touch,event)
    print(88888588)
end

首页 我的博客
粤ICP备17103704号