上一篇文章介绍了主域和子域的概念,微信小游戏:主域子域,这一篇介绍一下怎样在微信小游戏中制做好友排行榜node
因为开放数据域是一个封闭、独立的 JavaScript 做用域,因此开发者须要建立两个项目:web
在个人示例中client是主域工程,children是子域工程,工程目录能够不相关
发布设置有几点注意事项微信
这段代码能够写在你的游戏结算函数中,告诉子域游戏要结算了svg
if(cc.sys.platform == cc.sys.WECHAT_GAME) { wx.postMessage({score : this.score, type : "open"}); }
var self = null; cc.Class({ extends: cc.Component, properties: { scroll_view_content : cc.Node, score_num_txt : cc.Label, max_score_notice : cc.Node, scroll_item_prefab : cc.Prefab, root_node : cc.Node, }, start () { this.max_score = 0; if(cc.sys.platform == cc.sys.WECHAT_GAME) { wx.onMessage(this.on_message_come); wx.getUserCloudStorage({ keyList : ["max_score"], success : this.get_user_data_success, fail : this.get_user_data_fail, }); wx.getFriendCloudStorage({ keyList : ["max_score"], success : this.get_friend_data_success, fail : this.get_friend_data_fail, }); } this.root_node.active = false; this.cached_scroll_item = []; this.cached_data = {}; self = this; }, on_message_come : function(data) { if(data["type"] == "open") { var score = data["score"]; self.refresh(score); if(score > self.max_score) { wx.setUserCloudStorage({ KVDataList : [{key : "max_score", value : score.toString()}], success : self.set_data_success, fail : self.set_data_fail, }); wx.getFriendCloudStorage({ keyList : ["max_score"], success : self.get_friend_data_success, fail : self.get_friend_data_fail, }); self.max_score = score; } } }, refresh : function(score) { this.score_num_txt.string = score; if(score > this.max_score) { this.max_score_notice.active = true; } else { this.max_score_notice.active = false; } var items_d = []; for(var k in this.cached_data) { items_d.push(this.cached_data[k]); } items_d.sort(function(a, b){return b["max_score"] - a["max_score"];}); for(var i = 0; i < items_d.length; i++) { if(i < this.cached_scroll_item.length) { this.cached_scroll_item[i].refresh(i, items_d[i]); } else { var new_node = cc.instantiate(this.scroll_item_prefab); new_node.parent = this.scroll_view_content; var controller = new_node.getComponent("RankItemController"); controller.refresh(i, items_d[i]); this.cached_scroll_item.push(controller); } } }, set_data_success : function(result) { console.log(result); }, set_data_fail : function(result) { console.log(result); }, get_user_data_success : function(result) { console.log(result); var data = result["KVDataList"]; for(var item in data) { if(data[item]["key"] == "max_score") { self.max_score = parseInt(data[item]["value"]); break; } } }, get_user_data_fail : function(result) { console.log(result); }, get_friend_data_success : function(result) { var data = result["data"]; for(var k in data) { var final_d = {}; var item_d = data[k]; var kv_list = item_d["KVDataList"] final_d["url"] = item_d["avatarUrl"]; final_d["nickname"] = item_d["nickname"]; for(var kv in kv_list) { if(kv_list[kv]["key"] == "max_score") { final_d["max_score"] = parseInt(kv_list[kv]["value"]); break; } } self.cached_data[item_d["openid"]] = final_d; } console.log(result); console.log(self.cached_data); }, get_friend_data_fail : function(result) { console.log(result); } });
下一篇文章将介绍一些微信小游戏中遇到的一些问题及解决方式函数
1.开发工具:https://www.cocos.com/工具
推广一下本身作的简单的小游戏
建立了一个小游戏交流群,若是过时或者满了或者有问题交流 能够加我微信 备注“微信小游戏”
post