Commit 461a7523 by Mike Zhu

更新所有玩家倒计时

parent 9ff81dfe
...@@ -109,20 +109,23 @@ class Main extends egret.DisplayObjectContainer { ...@@ -109,20 +109,23 @@ class Main extends egret.DisplayObjectContainer {
}) })
this.createGameScene(); this.createGameScene();
platform.onSocketMessage(function(res) { platform.onSocketMessage(function (res) {
console.log('收到服务器内容:' + res.data) console.log('收到服务器内容:' + res.data)
const state = Store.onMessage(res.data) const state = Store.onMessage(res.data)
let data = res.data let data = res.data
if (typeof res.data === 'string') { if (typeof res.data === 'string') {
data = JSON.parse(res.data) data = JSON.parse(res.data)
} switch (data.type) {
case 'entry':
if(data.type === 'entry') { Store.onMessage({
Store.onMessage({ type: 'updateCid',
type: 'updateCid', payload: data
payload: data })
}) SceneManager.getInstance().roomScene.update(data)
SceneManager.getInstance().roomScene.update(data) break;
case 'game_ready':
SceneManager.getInstance().roomScene.update(data)
}
} }
}) })
platform.onSocketError((e) => { platform.onSocketError((e) => {
...@@ -170,5 +173,5 @@ class Main extends egret.DisplayObjectContainer { ...@@ -170,5 +173,5 @@ class Main extends egret.DisplayObjectContainer {
result.texture = texture; result.texture = texture;
return result; return result;
} }
} }
...@@ -26,11 +26,13 @@ class Store { ...@@ -26,11 +26,13 @@ class Store {
..._state, ..._state,
userInfo: payload userInfo: payload
} }
break;
case 'updateCid': case 'updateCid':
_state = { _state = {
..._state, ..._state,
cid: payload.cid cid: payload.cid
} }
break;
default: default:
break; break;
} }
......
...@@ -19,15 +19,39 @@ class RoomScene extends egret.Sprite { ...@@ -19,15 +19,39 @@ class RoomScene extends egret.Sprite {
} }
public update(data) { public update(data) {
const payload = data.payload switch (data.type) {
if (payload.seat == 0) { case 'entry':
this.startLabel.text = '开始' const payload = data.payload
this.startLabel.touchEnabled = true if (payload.seat == 0) {
this.startLabel.addEventListener(egret.TouchEvent.TOUCH_TAP, this.startHandler, this) this.startLabel.text = '开始'
} else { this.startLabel.touchEnabled = true
this.startLabel.text = '等待房主点击开始' this.startLabel.addEventListener(egret.TouchEvent.TOUCH_TAP, this.startHandler, this)
this.startLabel.touchEnabled = false } else {
this.startLabel.text = '等待房主点击开始'
this.startLabel.touchEnabled = false
}
break;
case 'game_ready':
this.countDown(payload)
default:
break;
} }
}
private countDown(res) {
this.startLabel.text = '3'
const timer = setInterval(() => {
if (this.startLabel.text == '1') {
return this.startLabel.text = 'Go!'
}
if (this.startLabel.text == 'Go!') {
clearInterval(timer)
SceneManager.getInstance().changeScene('gameScene')
return
}
this.startLabel.text = `${Number(this.startLabel.text) - 1}`
}, 1000)
} }
private initBg() { private initBg() {
...@@ -67,21 +91,10 @@ class RoomScene extends egret.Sprite { ...@@ -67,21 +91,10 @@ class RoomScene extends egret.Sprite {
} }
}), }),
success: res => { success: res => {
this.startLabel.text = '3'
const timer = setInterval(() => {
if(this.startLabel.text == '1') {
return this.startLabel.text = 'Go!'
}
if(this.startLabel.text == 'Go!') {
clearInterval(timer)
SceneManager.getInstance().changeScene('gameScene')
return
}
this.startLabel.text = `${Number(this.startLabel.text) - 1}`
}, 1000)
}, },
fail: res => { fail: res => {
this.startLabel.text = '启动失败' this.startLabel.text = '启动失败'
console.error('start error: ', res)
} }
}) })
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment