键盘监听事件
local function onKeyReleased(keyCode, event) local label = event:getCurrentTarget() if keyCode == cc.KeyCode.KEY_BACK then label:setString("BACK clicked!") elseif keyCode == cc.KeyCode.KEY_MENU then label:setString("MENU clicked!") end end local listener = cc.EventListenerKeyboard:create() listener:registerScriptHandler(onKeyReleased, cc.Handler.EVENT_KEYBOARD_RELEASED ) local eventDispatcher = layer:getEventDispatcher() eventDispatcher:addEventListenerWithSceneGraphPriority(listener, labelTip)
焦点监听
local function onFocusChanged(widgetLostFocus,widgetGetFocus) if nil ~= widgetGetFocus and widgetGetFocus:isFocusEnabled() then widgetGetFocus:setColor(cc.c3b(255, 0, 0)) end if nil ~= widgetLostFocus and widgetLostFocus:isFocusEnabled() then widgetLostFocus:setColor(cc.c3b(255, 255, 255)) end if nil ~= widgetLostFocus and nil ~= widgetGetFocus then print(string.format("on focus change, %d widget get focus, %d widget lose focus", widgetGetFocus:getTag(), widgetLostFocus:getTag())) end end local eventListener = cc.EventListenerFocus:create() eventListener:registerScriptHandler(onFocusChanged) local eventDispatcher = self:getEventDispatcher() eventDispatcher:addEventListenerWithFixedPriority(eventListener, 1)
触摸监听事件
local function onTouchesEnded(touches, event) local count = table.getn(touches) for i = 1, count do local location = touches[i]:getLocation() for j = 1,3 do local node = ConvertToNode_layer:getChildByTag(100 + i - 1) local p1, p2 p1 = node:convertToNodeSpaceAR(location) p2 = node:convertToNodeSpace(location) cclog("AR: x=" .. p1.x .. ", y=" .. p1.y .. " -- Not AR: x=" .. p2.x .. ", y=" .. p2.y) end end end local listener = cc.EventListenerTouchAllAtOnce:create() listener:registerScriptHandler(onTouchesEnded,cc.Handler.EVENT_TOUCHES_ENDED ) local eventDispatcher = ConvertToNode_layer:getEventDispatcher() eventDispatcher:addEventListenerWithSceneGraphPriority(listener, ConvertToNode_layer)
单点触摸监听
local function onTouchBegan(touch, event) return true end local function onTouchMoved(touch, event) if item ~= nil then item:removeFromParent(true) --item:release() --item = nil end end local listener = cc.EventListenerTouchOneByOne:create() listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN ) listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED ) local eventDispatcher = ret:getEventDispatcher() eventDispatcher:addEventListenerWithFixedPriority(listener, -129)
场景层进出事件监听
local function onNodeEvent(event) if event == "exit" then ret:getEventDispatcher():removeEventListener(listener) end end ret:registerScriptHandler(onNodeEvent)
菜单相应事件
-- Font Item local spriteNormal = cc.Sprite:create(s_MenuItem, cc.rect(0,23*2,115,23)) local spriteSelected = cc.Sprite:create(s_MenuItem, cc.rect(0,23*1,115,23)) local spriteDisabled = cc.Sprite:create(s_MenuItem, cc.rect(0,23*0,115,23)) local item1 = cc.MenuItemSprite:create(spriteNormal, spriteSelected, spriteDisabled) local function menuCallback(sender) cclog("menuCallback...") ret:getParent():switchTo(1) end item1:registerScriptTapHandler(menuCallback) -- Image Item local function menuCallback2(sender) ret:getParent():switchTo(2) end local item2 = cc.MenuItemImage:create(s_SendScore, s_PressSendScore) item2:registerScriptTapHandler(menuCallback2)
UI控件监听
local function percentChangedEvent(sender,eventType) if eventType == ccui.SliderEventType.percentChanged then local slider = sender local percent = "Percent " .. slider:getPercent() self._displayValueLabel:setString(percent) end end local slider = ccui.Slider:create() slider:setTouchEnabled(true) slider:loadBarTexture("cocosui/sliderTrack2.png") slider:loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "") slider:loadProgressBarTexture("cocosui/slider_bar_active_9patch.png") slider:setScale9Enabled(true) slider:setCapInsets(cc.rect(0, 0, 0, 0)) slider:setContentSize(cc.size(250, 10)) slider:setPosition(cc.p(widgetSize.width / 2.0, widgetSize.height / 2.0)) slider:addEventListener(percentChangedEvent)
动画事件监听
local function animationEvent(armatureBack,movementType,movementID) local id = movementID if movementType == ccs.MovementEventType.loopComplete then if id == "Fire" then local actionToRight = cc.MoveTo:create(2, cc.p(VisibleRect:right().x - 50, VisibleRect:right().y)) armatureBack:stopAllActions() armatureBack:runAction(cc.Sequence:create(actionToRight,cc.CallFunc:create(callback1))) armatureBack:getAnimation():play("Walk") elseif id == "FireMax" then local actionToLeft = cc.MoveTo:create(2, cc.p(VisibleRect:left().x + 50, VisibleRect:left().y)) armatureBack:stopAllActions() armatureBack:runAction(cc.Sequence:create(actionToLeft, cc.CallFunc:create(callback2))) armatureBack:getAnimation():play("Walk") end end end armature:getAnimation():setMovementEventCallFunc(animationEvent)
时间: 2024-10-07 12:55:30