地址:https://github.com/Golangltd/LollipopCreator
框架文档地址:GameAIs.Com
LollipopCreator:ConnectionManager
LollipopCreator游戏框架
游戏客户端开发中,由于制作人或者策划立项的不同,导致前端开发并不像后端框架不变,例如 2D游戏开发,引擎选择就比较多,Unity、cocos、 白鹭等都可以,本系列就是给大家开源一套cocos creator引擎开发的2D游戏框架LollipopGo。 creator 系统为例,给大家梳理下前端2D游戏架构的基础设计思想。
Index
返回首页
如何设计
调用流程
实例代码
如何设计
1. 连接数据加载和处理,主要是网络相关。
调用流程
例子:
checkOffline: function (dt) {
// 是否会登录
if (!this.logined) {
return
}
// 是否是弹框
if (this.asking) {
return
}
// 心跳
this.heartbeat()
this.elapsed += Math.min(0.5, dt)
if (this.elapsed < 10) {
return
}
cc.log("掉线了,马上重连", this.elapsed)
this.elapsed = 0
if (this.tries < 10) {
this.tries++
client.reLogin()
eventMgr.emit('connect-tip', this.tries)
cc.log("网络中断,拼命连接中.....", this.tries)
this.showCommonTip(this.tries)
} else {
cc.log("弹出重连ui", this.tries)
this.popAskDialog()
this.showCommonAsk()
}
},
注意点:检查是否断线
实例代码
cc.Class({
extends: cc.Component,
properties: {
tip: cc.Node,
},
onLoad: function () {
window.connMgr = this
this.logined = false
this.tries = 0
this.asking = false
this.elapsed = 0
this.isreconnect = false
this.tip.active = false
let n = 0
// this.schedule((dt) => {
// this.checkOffline(dt)
// }, 0.5, 1e7, 0)
},
checkOffline: function (dt) {
// 是否会登录
if (!this.logined) {
return
}
// 是否是弹框
if (this.asking) {
return
}
// 心跳
this.heartbeat()
this.elapsed += Math.min(0.5, dt)
if (this.elapsed < 10) {
return
}
cc.log("掉线了,马上重连", this.elapsed)
this.elapsed = 0
if (this.tries < 10) {
this.tries++
client.reLogin()
eventMgr.emit('connect-tip', this.tries)
cc.log("网络中断,拼命连接中.....", this.tries)
this.showCommonTip(this.tries)
} else {
cc.log("弹出重连ui", this.tries)
this.popAskDialog()
this.showCommonAsk()
}
},
heartbeat() {
// if (this.isreconnect) return
client.send({
Protocol: 7,
Protocol2: 81,
Uid: me.UID
})
},
reset(logined) {
eventMgr.emit('connect-tip', 0)
this.isreconnect = false
this.elapsed = 0
this.logined = logined
this.tries = 0
this.tip.active = false
cc.log("reset", this.tries)
if (logined) {
this.heartbeat()
}
},
popAskDialog() {
if (this.asking) {
return
}
this.asking = true
eventMgr.emit('connect-tip', 0)
eventMgr.emit('connect-dialog')
},
reconnect: function () {
this.asking = false
this.tries = 1
this.elapsed = 0
client.reLogin()
eventMgr.emit('connect-tip', this.tries)
cc.log("网络中断,拼命连接中.....", this.tries)
},
showCommonTip: function (tries) {
this.tip.active = (tries > 0)
if (tries > 0) {
this.tip.PathChild('val', cc.Label).string = `网络中断,拼命连接中...(${tries})`
}
},
showCommonAsk: function () {
this.tip.active = false
UIMgr.show("MessageBox", "游戏已断开连接 请重新连接", 'ok', () => {
cc.audioEngine.stopAll()
cc.game.restart()
})
},
});
版权申明:内容来源网络,版权归原创者所有。除非无法确认,我们都会标明作者及出处,如有侵权烦请告知,我们会立即删除并表示歉意。谢谢。
Golang语言社区
ID:Golangweb
www.GameAIs.Com
www.ByteEdu.Com
www.ByteEdu.Org
游戏服务器架构丨分布式技术丨大数据丨游戏算法学