I am trying to handle back button press in my Cocos2D javascript game. I am using the following code in my Layer class.
var self = this;
cc.eventManager.addListener
({
event: cc.EventListener.KEYBOARD,
onKeyPressed: function(keyCode, event)
{
if (keyCode == cc.KEY.back)
{
self.handleBackPress();
}
}
},
this);
But it is not working. I am getting no response upon pressing back button. What am I doing wrong?
alexgg
#2
I have this and it works:
cc.eventManager.addListener({
event: cc.EventListener.KEYBOARD,
onKeyReleased: this.onKeyReleased
}, this);
onKeyReleased: function (key, event) {
var target = event.getCurrentTarget();
switch(key) {
case cc.KEY.back:
//code
break;
case cc.KEY.home:
//code
break;
}
}
May be the problem is in the self var ? I would use target, as in my code, to get the layer.
Edit: onkeyreleased is a function of the layer.
2 Likes
Thank you. It worked when I replaced onKeyPressed with onKeyReleased. Any idea why onKeyPressed was not working?
I am guessing that the Android code doesn’t pass pressed states for back buttons.
I have noticed this comparing Windows and Android implementations in 2.2.6, may very well be true on latest.