Cocos2D-JS v3.1 Is there any way to have a permanent Keyboard handler? (Scene Transitions Kill My Handler)

Hi all.
When I add a keyboard handler on my Layer
then I lose some keyboard press/release events during the SCENE transition.
(You know, I have to create a new layer for my next Scene.)

Is there any workaround to miss no key events during scene swapping?

if (cc.sys.capabilities.hasOwnProperty('keyboard'))
    cc.eventManager.addListener({
        event: cc.EventListener.KEYBOARD,
        onKeyPressed:function (key, event) {
            waw.KEYS[key] = true;
        },
        onKeyReleased:function (key, event) {
            waw.KEYS[key] = false;
        }
    }, this);

Are you binding your event handler to a layer or a node of the current scene ?
If yes, just replace the second parameter of addListener to a fixed priority, like 1:

if (cc.sys.capabilities.hasOwnProperty('keyboard'))
    cc.eventManager.addListener({
        event: cc.EventListener.KEYBOARD,
        onKeyPressed:function (key, event) {
            waw.KEYS[key] = true;
        },
        onKeyReleased:function (key, event) {
            waw.KEYS[key] = false;
        }
    }, 1);

Thank you.
You were right. I used to bind it to my layer.

Your advice did not fix the problem.
(the binding to “1” works fine. Now I do it In the very beginning of my application.)

I still LOSE the keyboard listening during the SCENE TRANSITION TIME.
After the transition, the keyboard Listener works fine again.

transition = cc.TransitionSlideInT;
var nextScene = new cc.Scene();
var nextLayer = new waw.MainLayer();
nextLayer.init();
nextScene.addChild(nextLayer);
cc.director.runScene(new transition(0.5, nextScene));  //1st arg = duration of the transition

Basically, I lose control during 0.5 seconds.

It gives our game “arrow keys stuck” effect.
I just want to fix it.


Here is the playable game online: http://famesoft.ru/a/
Using arrow keys go to another room and try to RELEASE the arrow button during the rooms scrolling transition.
You’ll see the effect.

Here is the source code http://famesoft.ru/a/waw.zip

PS Thank for your effort.

I see, it’s because TransitionScene disabled cc.eventManager during transition. Imagine the case that while entering the right side door, user released right key, then very quickly press and released left key, should your scene be transited back to the original room ? Unfortunately, we don’t support transition scene from a transition scene. Key control is an example, there could be other type of control, for example, in our test case, scene transition is controlled by a button. If event is available during scene transition, then we will face the same problem.

Here is the implementation of transition scene:

cc.TransitionScene = cc.Scene.extend(/** @lends cc.TransitionScene# */{

...

    onEnter:function () {
        cc.Node.prototype.onEnter.call(this);

        // disable events while transitions
        cc.eventManager.setEnabled(false);

        // outScene should not receive the onEnter callback
        // only the onExitTransitionDidStart
        this._outScene.onExitTransitionDidStart();

        this._inScene.onEnter();
    },

    onExit:function () {
        cc.Node.prototype.onExit.call(this);

        // enable events while transitions
        cc.eventManager.setEnabled(true);

        this._outScene.onExit();

        // _inScene should not receive the onEnter callback
        // only the onEnterTransitionDidFinish
        this._inScene.onEnterTransitionDidFinish();
    },
...

})

My suggestion is to stop your role’s action when he enter a new scene.

1 Like

Well I saw the source code after your 1st reply.
I understand the reason now.

I will think what to do in my game.
So it is not a bug. It is a peculiarity of the engine.

In the previous engine Cocos2D 2.2.2 in my game I disabled keyboard/touch managers
on transition start. Because I had the same bug you just told me ))))
(I’m migrating from cocos2d 2.2.2, so I see some changes)

Thank you for wide answers. You help me very much.

1 Like

You are welcome

1 Like

Well, this seems all logical, for user interactions etc.
But i am using the EventDispatcher for callbacks from native components, like “In App Billing”.
And unfortunately some essential events are swallowed because they occur just during a transition. I call dispatchEvent and they are suppressed without any notice. (by _isEnabled == false in EventDispatcher)
My question:
Is EventDispatcher the right tool for receiving system callbacks?
How would i work around this?
What is the right way to dispatch system callbacks?
Regards,
Stephan