1.默认开机进入的是startScene,加载了AppStart脚本,其中有一个非常重要的方法,初始化了游戏内用到的所有管理器,另外还请求了服务器的版本信息(次要)
function initMgr(){
cc.vv = {};
var UserMgr = require("UserMgr");
cc.vv.userMgr = new UserMgr();
var ReplayMgr = require("ReplayMgr");
cc.vv.replayMgr = new ReplayMgr();
cc.vv.http = require("HTTP");
cc.vv.global = require("Global");
cc.vv.net = require("Net");
var GameNetMgr = require("GameNetMgr");
cc.vv.gameNetMgr = new GameNetMgr();
cc.vv.gameNetMgr.initHandlers();
var AnysdkMgr = require("AnysdkMgr");
cc.vv.anysdkMgr = new AnysdkMgr();
cc.vv.anysdkMgr.init();
var VoiceMgr = require("VoiceMgr");
cc.vv.voiceMgr = new VoiceMgr();
cc.vv.voiceMgr.init();
var AudioMgr = require("AudioMgr");
cc.vv.audioMgr = new AudioMgr();
cc.vv.audioMgr.init();
var Utils = require("Utils");
cc.vv.utils = new Utils();
//var MJUtil = require("MJUtil");
//cc.vv.mjutil = new MJUtil();
cc.args = urlParse();
}
2.接下来加载loading场景,进度条看代码是假的
3.接下来加载login场景,在start里面检查了本地是否存在微信账号和签名,如果存在,就自动登录,这儿是我可以借鉴的自动登录的地方,
接下来有两种登录方法,一种是微信,一种是游客,不过不管哪种登录,最后都会调用到UserMgr的onAuth方法
onAuth:function(ret){
var self = cc.vv.userMgr;
if(ret.errcode !== 0){
console.log(ret.errmsg);
}
else{
self.account = ret.account;
self.sign = ret.sign;
cc.vv.http.url = "http://" + cc.vv.SI.hall;
self.login();
}
},
最后执行login方法,发送http请求到账号服务器/login,{account:this.account,sign:this.sign},然后根据返回的数据判断是否有uid,如果没有,进入创建角色界面,否则直接进入大厅
4.在进入大厅的时候执行了一个很重要的方法cc.vv.userMgr.enterRoom
enterRoom:function(roomId,callback){
var self = this;
var onEnter = function(ret){
if(ret.errcode !== 0){
if(ret.errcode == -1){
setTimeout(function(){
self.enterRoom(roomId,callback);
},5000);
}
else{
cc.vv.wc.hide();
if(callback != null){
callback(ret);
}
}
}
else{
cc.vv.wc.hide();
if(callback != null){
callback(ret);
}
cc.vv.gameNetMgr.connectGameServer(ret);
}
};
var data = {
account:cc.vv.userMgr.account,
sign:cc.vv.userMgr.sign,
roomid:roomId
};
cc.vv.wc.show("正在进入房间 " + roomId);
cc.vv.http.sendRequest("/enter_private_room",data,onEnter);
},
进入房间后,如果没有错误,就实际执行力连接socket服务器的操作 ,
connectGameServer:function(data){
this.dissoveData = null;
cc.vv.net.ip = data.ip + ":" + data.port;
console.log(cc.vv.net.ip);
var self = this;
var onConnectOK = function(){
console.log("onConnectOK");
var sd = {
token:data.token,
roomid:data.roomid,
time:data.time,
sign:data.sign,
};
cc.vv.net.send("login",sd);
};
var onConnectFailed = function(){
console.log("failed.");
cc.vv.wc.hide();
};
cc.vv.wc.show("正在进入房间");
cc.vv.net.connect(onConnectOK,onConnectFailed);
}
里面两个回调函数,如果连接成功,则执行登录操作
cc.vv.net.addHandler("login_result",function(data){
console.log(data);
if(data.errcode === 0){
var data = data.data;
self.roomId = data.roomid;
self.conf = data.conf;
self.maxNumOfGames = data.conf.maxGames;
self.numOfGames = data.numofgames;
self.seats = data.seats;
self.seatIndex = self.getSeatIndexByID(cc.vv.userMgr.userId);
self.isOver = false;
}
else{
console.log(data.errmsg);
}
self.dispatchEvent('login_result');
});
cc.vv.net.addHandler("login_finished",function(data){
console.log("login_finished");
cc.director.loadScene("mjgame",function(){
cc.vv.net.ping();
cc.vv.wc.hide();
});
self.dispatchEvent("login_finished");
});
登录完成设置一些本地相关的用户参数,隐藏进度对话框,进入游戏界面,在登录完成的时候还设置了断线重连的函数