cocos2d-js tips

  1. 监控 Android 上的返回按钮,v3.8.1 验证。
cc.eventManager.addListener({
    event : cc.EventListener.KEYBOARD,
    onKeyPressed : function(k, e) {
        if (k == cc.KEY.back) {
            console.log("backspace");
        }
    }
}, this);
  1. 关于 js 闭包

代码片段

sdkbox.PluginFacebook.setListener({
    onFetchFriends: function(ok, msg) {
      var friends = sdkbox.PluginFacebook.getFriends();
      for (var i = 0; i < friends.length; i++) {
        var friend = friends[i];

        var foo = ( function() {
            var uid = friend.uid; // 这里访问不到 friends, 只能访问到同级的变量 friend
            return {
                onClick: function () {
                    var params = new Object();
                    params.redirect = "false";
                    params.type = "small";
                    sdkbox.PluginFacebook.api(uid+"/picture", "GET", params, "__fetch_picture_tag__");
                }
            };
        } () );

        // create menu
        var item = new cc.MenuItemLabel(label, foo.onClick);
      }
    }
});

你可能感兴趣的:(cocos2d-js tips)