키보드 이벤트, 사용자 정의 이벤트

1969 단어 cocos-js
/**
 * Created by cena on 2017/4/4.
 */
var MyLayer=cc.Layer.extend({
    _topDisplayLabel:null,
    _item1Count:null,
    ctor:function() {
        this._super();
        var size=cc.winSize;
         var button =new ccui.Button();
         button.attr({
             x:size.width/2,
             y:size.height/2
         });
         this._topDisplayLabel=new cc.LabelTTF("this is a button test","Arial",32);
         this._topDisplayLabel.attr({
             x:size.width/2,
             y:size.height/2+150
         });
         this.addChild(this._topDisplayLabel);
         this._item1Count=1;

        var _listener=cc.EventListener.create({
            event:cc.EventListener.CUSTOM,
            eventName:"game_custom_event",
            callback:function (event) {
                var label=event.getCurrentTarget();
                label.setString("Custom event "+event.getUserData()+" received times");
                cc.log("received");
            }
        });
        cc.eventManager.addListener(_listener,this._topDisplayLabel);

        cc.eventManager.addListener({
             event:cc.EventListener.KEYBOARD,
             onKeyPressed:function (keycode,event) {
                 var label=event.getCurrentTarget();
                 label.setString("key pressed");

                 var event = new cc.EventCustom("game_custom_event");
                 event.setUserData("1");
                 cc.eventManager.dispatchEvent(event);
             },
             onKeyReleased:function (keycode,event) {
                 var label=event.getCurrentTarget();
                 label.setString("key released");
             }
         },this._topDisplayLabel);

        return true;
    }
});
var  myscene=cc.Scene.extend({
    onEnter:function()
    {
        this._super();
        var layer=new MyLayer();
        this.addChild(layer);
    }
});

좋은 웹페이지 즐겨찾기