各类监听时间整理

键盘监听事件

        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

各类监听时间整理的相关文章

Android实现后台长期监听时间变化

1.首先我们的目的是长期监听时间变化,事实上应用程序退出. 通过了解我们知道注冊ACTION_TIME_TICK广播接收器能够监听系统事件改变,可是 查看SDK发现ACTION_TIME_TICK广播事件仅仅能动态注冊: Broadcast Action: The current time has changed. Sent every minute. You can not receive this through components declared in manifests, only

Android 监听开机广播启动服务 动态注册时间变化监听广播

开机广播权限 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 开机广播 注册 <receiver android:name="com.stone.receiver.BootedReceiver"> <intent-filter> <action android:name="android.intent.acti

当AVPlayer在被释放之后,Player一直监听的时间没有被移除,提示错误的解决办法

Xcode Consolu打印出来的提示: An instance 0x156608c0 of class AVPlayer was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSK

Unity3d 中键值监听方法

unity3d的api中没有负责监听键值的方法,不过unity的input类是通过c#类获取各类监听事件,所以我们可以通过c#类监听,方法如下: void OnGUI() { Event e = Event.current; if (e.isKey) { if (Event.current.keyCode == KeyCode.Joystick2Button0){ //你的逻辑} } } 过程很简单,就不多说,注意该事件只能在OnGUI函数中起作用. 转载请标明出处,from 博客园 HemJo

Javascript 的事件监听函数和移除事件监听函数

<html> <head></head> <body> <div id="hello">hello world</div> <script> //w3c中使用addEventListener()函数监听事件,IE中使用attachEvent()函数监听事件 //w3c中使用 removeEventListener ()函数移除事件监听,IE中使用detachEvent()函数来移除事件监听 var do

关于jq一些基本监听事件

一直一来对js不太熟悉,做起东西来感觉很出力.这次做模态框,都折腾了不少时间.期间遇到的问题,记录在此. 背景: bootstrap本身有模态框的调用函数,但是由于无法弄清其原理,实在不知怎么下手.查阅实践后发现: <script type="text/javascript"> $("#Modalname").modal('show'); </script> 这么一条小代码可以实现全屏mask的效果.但是这种mask会取消滑动块,因此,模态框

input标签的监听事件

监听事件的触发是完成交互的一个重要组成部分,现将input标签的监听事件整理如下. onfocus         当input 获取到焦点时触发. onblur            当input失去以获取到焦点时触发. 1.1  onchange      当input失去焦点并且它的value值发生变化时触发. 1.2  onpropertychange    只要当前对象属性发生改变,都会触发事件,IE专属(6.7.8). onkeydown   在 input中有键按住的时候触发事件.

在Javascript中监听flash事件(转)

在Javascript中监听flash事件,其实有两种做法: 1.在特定的环境下(例如专门制作的flash),大家约定一个全局函数,然后在flash的事件中用ExternalInterface.call调用这个全局函数即可.2.在非特定的环境下(例如编写通用的flash插件),是不能限制用户的函数名的,所以根本无法约定全局函数:是否可以通过类似js的回调函数的形式实现事件监听呢? 其实js与flash的通信,一般情况下可以进行一些比较简单的通信,如传递基本的数据类型.传递简单的对象.调用函数等,

ORA-12541:TNS:无监听程序 配置Oracle Myeclipse无法连接上 花费一天时间解决掉的

背景:自己机子做oracle服务器,其他机子可以ping得通我的机子,但是jdbc就是连不上,后来用plsql连出现无监听程序.... 我昨天重新安装Oracle后,用PL/SQL Developer连接oracle时出现ORA-12541:TNS:无监听程序的错误,如下图: 在王鹏师兄的帮助下,发现原来是oracle的监听没有启动,重启监听后就连接成功了,下面跟大家分享一下如何启动oracle的监听. 1.打开Net Configuration Assistant 2.选择监听程序配置,下一步