发表于: 2020-10-27 22:50:50
0 1131
今天完成的事情
任务4:
学习有限状态机使用
// 获取有限状态机变量
var pro1 = document.getElementsByClassName('word')[0];
var pro2 = document.getElementsByClassName('word')[1];
var pro3 = document.getElementsByClassName('word')[2];
var pro4 = document.getElementsByClassName('word')[3];
var fsm = new StateMachine({
init: 'step1',
transitions: [
{ name: 'pro1', from: 'step1', to: 'step2' },
{ name: 'pro2', from: 'step2', to: 'step3' },
{ name: 'pro3', from: 'step3', to: 'step4' },
{ name: 'pro4', from: 'step4', to: 'step1' }
],
methods: {
onInvalidTransition: function (transition, from, to) {
switch (from) {
case "step1":
alert("请按游戏顺序进行,请点击'杀手杀人'!");
break;
case "step2":
alert("请按游戏顺序进行,请点击'亡者'!");
break;
case "step3":
alert("请按游戏顺序进行,请点击'玩家'!");
break;
case "step4":
alert("请按游戏顺序进行,请点击'投票'!");
break;
}
},
},
});
box1.onclick = function () {
if (fsm.state === 'step1') {
// 在id为box1的div中创建一个div
$("div#box1").after("<div class='process' id='add'>" + playnum + "号被杀死,真实身份是" + arr[playnum - 1] + "</div>");
var killNum = playnum
console.log(playnum)
console.log(arr[playnum - 1])
window.sessionStorage.setItem('killNum', killNum);
pro1.style.backgroundColor = 'rgb(255, 255, 0)'
}
fsm.pro1();
}
pro2.onclick = function () {
if (fsm.state === 'step2') {
confirm('请死者亮明身份发表遗言!')
pro2.style.backgroundColor = 'rgb(0, 255, 0)';
}
fsm.pro2();
}
pro3.onclick = function () {
if (fsm.state == 'step3') {
confirm("玩家依次讨论!");
pro3.style.backgroundColor = 'rgb(0, 255, 255)';
}
fsm.pro3();
}
// 投票
pro4.onclick = function () {
if (fsm.state === 'step4') {
// location.href = 'js任务4-1.html';
// 在id为box1的div中创建一个div
$("div#box4").after("<div class='process' id='add'>" + playnum + "号被投死,真实身份是" + arr[playnum - 1] + "</div>");
var voteNum = playnum
console.log(arr.length)
console.log(playnum)
console.log(arr[playnum - 1])
window.sessionStorage.setItem('voteNum', voteNum);
}
fsm.pro4();
}
}
评论