Browse Source

与父窗口通信

master
song 4 years ago
parent
commit
a4c46863b5
  1. 8
      src/classes/suspend.js
  2. 32
      src/classes/time.js
  3. 9
      src/custom.js
  4. 99
      src/message.js

8
src/classes/suspend.js

@ -24,15 +24,15 @@ Suspend.prototype.init = function () {
Suspend.prototype.suspendGame = function () { Suspend.prototype.suspendGame = function () {
const lib = this.lib; const lib = this.lib;
const suspend = new lib.suspend(); const suspend = new lib.suspend();
suspend.x = 180; suspend.x = 50;
suspend.y = lib.properties.height - 100; suspend.y = lib.properties.height - 100;
this.suspend = suspend; this.suspend = suspend;
// 关闭演示模式 // 继续游戏
this.suspend.addEventListener( this.suspend.addEventListener(
'click', 'click',
function () { function () {
console.log('暂停游戏'); stage.removeChild(suspend);
stage.removeChild(stopDemo); continueMessage();
}, },
false, false,
); );

32
src/classes/time.js

@ -43,20 +43,34 @@ Time.of = function (endCallback, duration = config.duration || 60, count = confi
// 初始化 // 初始化
Time.prototype.init = function () { Time.prototype.init = function () {
console.log('初始化');
this.renderBg(); this.renderBg();
this.renderText(this.duration); this.renderText(this.duration);
}; };
// 设置时长
Time.prototype.setDuration = function (duration) {
this.duration = duration;
this.renderText(this.duration);
};
// 开始游戏 开始倒计时 // 开始游戏 开始倒计时
Time.prototype.start = function (startTime = Date.now()) { Time.prototype.start = function (startTime = Date.now()) {
this.started = true; this.started = true;
state = 1; state = 1;
this.startTime = startTime; this.startTime = startTime;
this.duration = leftDuration ? leftDuration : this.duration;
this.update(); this.update();
}; };
// 开始游戏 开始倒计时 // 暂停游戏 暂停倒计时
Time.prototype.pause = function () {
this.started = false;
state = 3;
clearTimeout(this.timerId);
this.timerId = null;
};
// 结束游戏 结束倒计时
Time.prototype.end = function () { Time.prototype.end = function () {
this.started = false; this.started = false;
this.timerId && clearTimeout(this.timerId); this.timerId && clearTimeout(this.timerId);
@ -78,6 +92,7 @@ Time.prototype.renderBg = function () {
// 渲染文本 // 渲染文本
Time.prototype.renderText = function (time) { Time.prototype.renderText = function (time) {
if (this.text) stage.removeChild(this.text);
const text = new createjs.Text(time, 'bold 40px Arial', '#823d16'); const text = new createjs.Text(time, 'bold 40px Arial', '#823d16');
text.x = this.lib.properties.width - 100; text.x = this.lib.properties.width - 100;
text.y = 100; text.y = 100;
@ -90,15 +105,16 @@ Time.prototype.renderText = function (time) {
// 更新文本 // 更新文本
Time.prototype.update = function () { Time.prototype.update = function () {
if (this.timerId || !this.started) return; if (this.timerId || !this.started) return;
// const endTime = this.startTime + this.count * 1000 + this.duration * 1000; // const endTime = this.startTime + this.duration * 1000;
const endTime = this.startTime + this.duration * 1000;
this.timerId = setInterval(() => { this.timerId = setInterval(() => {
let leftTime = Math.round((endTime - Date.now()) / 1000); // let leftTime = Math.round((endTime - Date.now()) / 1000);
this.duration--;
leftDuration = this.duration;
this.text && stage.removeChild(this.text); this.text && stage.removeChild(this.text);
if (leftTime <= 0) { if (this.duration <= 0) {
leftTime = 0; this.duration = 0;
this.end(); this.end();
} }
this.renderText(leftTime); this.renderText(this.duration);
}, 1000); }, 1000);
}; };

9
src/custom.js

@ -1,14 +1,18 @@
const config = { const config = {
count: 5, // 默认倒计时时长 count: 5, // 默认倒计时时长
duration: 60, // 总时长 s duration: 60, // 总时长 s
level: 1, // 游戏难度级别
total: 100, // 总分 total: 100, // 总分
times: 20, // 动作次数 times: 20, // 动作次数
level: 1, // 游戏难度级别
currentScore: 0, // 当前得分
currentTimes: 0, // 当前次数
config: {}, // 得分配置
}; };
let library = null; let library = null;
let state = 0; // 游戏状态 0->未开始 1->进行中 2->结束 let state = 0; // 游戏状态 0->未开始 1->进行中 2->结束 3->暂停
let isDemo = false; // 是不是演示模式 let isDemo = false; // 是不是演示模式
let leftDuration = null; // 暂停时的时间
function initStage(lib) { function initStage(lib) {
library = lib; library = lib;
@ -28,5 +32,6 @@ function gameOver() {
const score = parseInt((config.total / config.times) * times); const score = parseInt((config.total / config.times) * times);
setTimeout(() => { setTimeout(() => {
End.of(score); End.of(score);
finishMessage(score, times);
}, 2000); }, 2000);
} }

99
src/message.js

@ -1,7 +1,102 @@
window.addEventListener( window.addEventListener(
'message', 'message',
function (event) { function (e) {
console.log('event: ', event); const res = e.data;
switch (res.event) {
case 'start':
startGame(res.data);
return;
case 'play':
playGame(res.data);
return;
case 'pause':
pauseGame(res.data);
return;
case 'continue':
continueGame(res.data);
return;
default:
finishGame(res.data);
return;
}
function startGame(data) {
const { count, game, status, param } = data;
config.count = count.duration;
config.duration = game.duration;
config.total = game.totalScore;
config.times = game.totalTimes;
config.level = game.level;
config.config = game.config;
state = status;
window.timeInstance.setDuration(game.duration);
// 开始倒计时
Count.of(countOver);
}
function playGame(data) {
const { score, times, status, param } = data;
state = status;
config.currentScore = score;
config.currentTimes = times;
if (config.config.directions[times - 1] === param.direction) {
main.play();
} else {
alert('动作不匹配');
}
}
function pauseGame(data) {
state = data.status;
Suspend.of();
window.timeInstance.pause();
}
function continueGame(data) {
state = data.status;
window.timeInstance.start();
}
function finishGame(data) {
const { score, times, status, param } = data;
state = status;
config.total = score;
config.times = times;
}
}, },
false, false,
); );
// 发消息
function sendMessage(data, src) {
if (!data) {
return alert('错误: 发送消息数据为空');
}
top.postMessage(data, src);
}
// 发送继续游戏消息
function continueMessage() {
const data = {
event: 'continue',
data: {
status: 1, // 1 -> 进行中
},
};
sendMessage(data, document.referrer);
}
// 发送游戏结束消息
function finishMessage(score, times) {
const data = {
event: 'finish',
data: {
score: score, // 得分
times: times, // 次数
status: 2, // 游戏状态 0 1 2
param: {}, // 额外个性化参数
},
};
sendMessage(data, document.referrer);
}

Loading…
Cancel
Save