各类监听时间整理
键盘监听事件
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)
EditBox事件监听
local editBoxSize = cc.size(visibleSize.width - 100, 60) local EditName = nil local EditPassword = nil local EditEmail = nil local function editBoxTextEventHandle(strEventName,pSender) local edit = pSender local strFmt if strEventName == "began" then strFmt = string.format("editBox %p DidBegin !", edit) print(strFmt) elseif strEventName == "ended" then strFmt = string.format("editBox %p DidEnd !", edit) print(strFmt) elseif strEventName == "return" then strFmt = string.format("editBox %p was returned !",edit) if edit == EditName then TTFShowEditReturn:setString("Name EditBox return !") elseif edit == EditPassword then TTFShowEditReturn:setString("Password EditBox return !") elseif edit == EditEmail then TTFShowEditReturn:setString("Email EditBox return !") end print(strFmt) elseif strEventName == "changed" then strFmt = string.format("editBox %p TextChanged, text: %s ", edit, edit:getText()) print(strFmt) end end -- top EditName = cc.EditBox:create(editBoxSize, cc.Scale9Sprite:create("extensions/green_edit.png")) EditName:setPosition(cc.p(visibleOrigin.x+visibleSize.width/2, visibleOrigin.y+visibleSize.height*3/4)) local targetPlatform = cc.Application:getInstance():getTargetPlatform() if kTargetIphone == targetPlatform or kTargetIpad == targetPlatform then EditName:setFontName("Paint Boy") else EditName:setFontName("fonts/Paint Boy.ttf") end EditName:setFontSize(25) EditName:setFontColor(cc.c3b(255,0,0)) EditName:setPlaceHolder("Name:") EditName:setPlaceholderFontColor(cc.c3b(255,255,255)) EditName:setMaxLength(8) EditName:setReturnType(cc.KEYBOARD_RETURNTYPE_DONE ) --Handler EditName:registerScriptEditBoxHandler(editBoxTextEventHandle) newLayer:addChild(EditName) --middle EditPassword = cc.EditBox:create(editBoxSize, cc.Scale9Sprite:create("extensions/orange_edit.png")) EditPassword:setPosition(cc.p(visibleOrigin.x+visibleSize.width/2, visibleOrigin.y+visibleSize.height/2)) if kTargetIphone == targetPlatform or kTargetIpad == targetPlatform then EditPassword:setFont("American Typewriter", 30) else EditPassword:setFont("fonts/American Typewriter.ttf", 30) end EditPassword:setFontColor(cc.c3b(0,255,0)) EditPassword:setPlaceHolder("Password:") EditPassword:setMaxLength(6) EditPassword:setInputFlag(cc.EDITBOX_INPUT_FLAG_PASSWORD) EditPassword:setInputMode(cc.EDITBOX_INPUT_MODE_SINGLELINE) EditPassword:registerScriptEditBoxHandler(editBoxTextEventHandle) newLayer:addChild(EditPassword) --bottom EditEmail = cc.EditBox:create(cc.size(editBoxSize.width, editBoxSize.height), cc.Scale9Sprite:create("extensions/yellow_edit.png")) EditEmail:setPosition(cc.p(visibleOrigin.x+visibleSize.width/2, visibleOrigin.y+visibleSize.height/4)) EditEmail:setAnchorPoint(cc.p(0.5, 1.0)) EditEmail:setPlaceHolder("Email:") EditEmail:setInputMode(cc.EDITBOX_INPUT_MODE_EMAILADDR) EditEmail:registerScriptEditBoxHandler(editBoxTextEventHandle) newLayer:addChild(EditEmail) newLayer:setPosition(cc.p(10, 20)) newScene:addChild(newLayer)