javascript - Cocos2d js touch events - Stack Overflow

I'm building an app in cocos2d-js, and I've got a problem with listening to keyboard events.

I'm building an app in cocos2d-js, and I've got a problem with listening to keyboard events. I expect a method called setKeyboardEnabled to exists but when I call it i get an error that setKeyboardEnabled is not a function, Am I missing something?

var AnimationLayer = cc.Layer.extend({
ctor : function() {
    this._super();
    this.setKeyboardEnabled(true);
},


onKeyDown : function(key) {
   cc.log("action");
}
...
...
)}

The same thing happens when I try to listen to touch events.

I'm building an app in cocos2d-js, and I've got a problem with listening to keyboard events. I expect a method called setKeyboardEnabled to exists but when I call it i get an error that setKeyboardEnabled is not a function, Am I missing something?

var AnimationLayer = cc.Layer.extend({
ctor : function() {
    this._super();
    this.setKeyboardEnabled(true);
},


onKeyDown : function(key) {
   cc.log("action");
}
...
...
)}

The same thing happens when I try to listen to touch events.

Share Improve this question edited Aug 24, 2014 at 18:15 CodeSmile 64.5k20 gold badges134 silver badges219 bronze badges asked Aug 24, 2014 at 18:04 Edgar ZakaryanEdgar Zakaryan 5765 silver badges11 bronze badges 1
  • I saw post saying that this method is no longer available here discuss.cocos2d-x/t/… – Pablo Jomer Commented Aug 24, 2014 at 18:11
Add a ment  | 

1 Answer 1

Reset to default 7

In Cocos2D v3 the way events are handled has changed. For a proper explanation of the whole system you should read New event manager in Cocos2d-Html5 v3.0.

In short, if you used to have:

var AnimationLayer = cc.Layer.extend({
    ctor : function() {
        this._super();
        this.setKeyboardEnabled(true);
    },

    onKeyDown : function(key) {
       cc.log("action");
    }
    ...
    ...
)}

You have to turn it into something like:

var AnimationLayer = cc.Layer.extend({
    ctor : function() {
        this._super();

        cc.eventManager.addListener({
            event: cc.EventListener.KEYBOARD,
            onKeyDown : function(key) {
                cc.log("action");
            }
        }, this);
    },        

    ...
    ...
)}

This can sound a bit plicated at first, but this new mechanism allows you to attach these event listeners to any kind of cc node, not just layers. So for example you could do something like this to make a Sprite fade when the mouse is above it:

var hoverHandler = cc.EventListener.create({
    event: cc.EventListener.MOUSE,
    onMouseMove: function (event) {
      var target = event.getCurrentTarget();
      var locationInNode = target.convertToNodeSpace(event.getLocation());
      var s = target.getContentSize();
      var rect = cc.rect(0, 0, s.width, s.height);

      if (cc.rectContainsPoint(rect, locationInNode)) {
        cc.log("It's hovering! x = " + locationInNode.x + ", y = " + locationInNode.y);
        target.opacity = 180;
        return true;
      } else {
        target.opacity = 255;
        return false;
      }
  }
});

var MyLayer = cc.Layer.extend({
    init: function() {
        this._super();

        var mySpriteThatFadesWhenHovered = cc.Sprite.create(...);
        this.addChild(mySpriteThatFadesWhenHovered);

        cc.eventManager.addListener(hoverHandler.clone(), mySpriteThatFadesWhenHovered );
    }
)}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744001684a4541620.html

相关推荐

  • javascript - Cocos2d js touch events - Stack Overflow

    I'm building an app in cocos2d-js, and I've got a problem with listening to keyboard events.

    10天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信