zoukankan      html  css  js  c++  java
  • Cocos Creator 键盘监听事件

    键盘事件
    键盘、设备重力传感器此类全局事件是通过函数 cc.systemEvent.on(type, callback, target) 注册的。
    cc.SystemEvent.EventType.KEY_DOWN (键盘按下)
    cc.SystemEvent.EventType.KEY_UP (键盘释放)
    cc.SystemEvent.EventType.DEVICEMOTION (设备重力传感)

    cc.Class({
    extends: cc.Component,
    onLoad: function () {
    // add key down and key up event
    cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
    cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    },

    onDestroy () {
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    },
    
    onKeyDown: function (event) {
        switch(event.keyCode) {
            case cc.KEY.a:
                console.log('Press a key');
                break;
        }
    },
    
    onKeyUp: function (event) {
        switch(event.keyCode) {
            case cc.KEY.a:
                console.log('release a key');
                break;
        }
    }

    });

    cc.Class({
    extends: cc.Component,
    onLoad () {
    // open Accelerometer
    cc.inputManager.setAccelerometerEnabled(true);
    cc.systemEvent.on(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);
    },

    onDestroy () {
        cc.systemEvent.off(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);
    },
    
    onDeviceMotionEvent (event) {
        cc.log(event.acc.x + "   " + event.acc.y);
    },

    });

  • 相关阅读:
    loj #143. 质数判定
    Quadratic Residues POJ
    P2155 [SDOI2008]沙拉公主的困惑
    P3868 [TJOI2009]猜数字
    P3704 [SDOI2017]数字表格
    P4449 于神之怒加强版
    P2568 GCD
    P1891 疯狂LCM
    loj#6229 这是一道简单的数学题
    P3768 简单的数学题 杜教筛+推式子
  • 原文地址:https://www.cnblogs.com/luorende/p/8367934.html
Copyright © 2011-2022 走看看