【ife】任务二十六:行星与飞船(一)
来源:互联网 发布:mac office无法删除 编辑:程序博客网 时间:2024/04/30 09:51
function addEventHandler(element, event, hanlder) { if (element.addEventListener) element.addEventListener(event, hanlder, false); else if (element.attachEvent) element.attachEvent("on" + event, hanlder); else element["on" + event] = hanlder;}var speed = 20; var discharge = 5; var charge = 2;var error = 0.3; var spaceships = [];window.onload = function() {var command = document.getElementById('command');var order = document.getElementById('order');var universe = document.getElementById('universe');addEventHandler(command, 'click', function() {event.target.disable = 'true';switch(event.target.innerHTML) {case '新的飞船起飞':Commander.newSpaceship(event.target);break;case '开始飞行':Commander.beginToFly(event.target);break;case '停止飞行':Commander.stopToFly(event.target);break;case '销毁':Commander.destroy(event.target);break;}});}var Spaceship = function(id) {this.id = id,this.state = 'stop',this.deg = 45,this.energy = 100,this.interval = null,this.energy_interval = null};Spaceship.prototype.powerSystem = function() {if (this.state == 'fly') {var that = this;if (this.interval == null) {this.interval = setInterval(function() {that.energy -= discharge;if (that.energy < 0) { that.state = 'stop';clearInterval(that.interval);that.interval = null;}document.getElementById("spaceship" + that.id).getElementsByTagName('span')[0].innerHTML = that.energy + '%';that.deg = (that.deg + speed) % 360;document.getElementById("spaceship" + that.id).style.transform = 'rotate(' + that.deg + 'deg)';}, 1000);}}if (this.state == 'stop') {clearInterval(this.interval);this.interval = null;}};Spaceship.prototype.energySystem = function() {this.energy += charge; if (this.energy > 100) this.energy = 100;document.getElementById("spaceship" + this.id).getElementsByTagName('span')[0].innerHTML = this.energy + '%';};Spaceship.prototype.distroySystem = function() {for (var i = 0; i < universe.getElementsByTagName('div').length; i++) {if (universe.getElementsByTagName('div')[i].innerHTML.toString()[0] == this.id) universe.removeChild(universe.getElementsByTagName('div')[i]);}for (var i = 0; i < spaceships.length; i++) { if (spaceships[i].id == this.id) {clearInterval(spaceships[i].interval); clearInterval(spaceships[i].energy_interval); spaceships.splice(i, 1); break;}}};Spaceship.prototype.signalSystem = function(id, cmd) {if (this.id == id) {switch(cmd) {case 'fly':this.state ='fly';this.powerSystem();this.energySystem();console.log(this.id + '号飞船接收到开始飞行命令');break;case 'stop':this.state = 'stop';this.powerSystem();this.energySystem();console.log(this.id + '号飞船接收到停止飞行命令');break;case 'destroy':this.distroySystem();console.log(this.id + '号飞船接收到销毁命令');break;}}};var Commander = {newSpaceship: function() {var divs = universe.getElementsByTagName('div');var commands = command.getElementsByTagName('div');if (commands.length < 4 ) {var newSpaceship = document.createElement('div');var newCommand = document.createElement('div');for (var i = 1; i <= 4; i++) { if (document.getElementById("spaceship" + i) == undefined)break;}var id = i;newSpaceship.innerHTML = id + '号-<span>100%</span>';newSpaceship.className = 'spaceship';universe.appendChild(newSpaceship);newSpaceship.setAttribute("id", "spaceship" + id);newSpaceship.style.top = 135 - (id - 1) * 50 + 'px';newSpaceship.style.transformOrigin = 'center' + ' ' + (170 + (id - 1) * 50) + 'px';newCommand.innerHTML = '<span>对' + id + '号飞船下达命令:</span><button>开始飞行</button><button>停止飞行</button><button>销毁</button>';command.insertBefore(newCommand,command.lastElementChild);display('指挥官:创建' + id + '号飞船成功<br>');var spaceship = new Spaceship(id); spaceships.push(spaceship);spaceship.energy_interval = esetInterval(function() {spaceship.energySystem();}, 1000);} else display('指挥官:创建失败,飞船数量已达上限4');},beginToFly: function(e) { var id = e.parentNode.firstChild.innerHTML.toString()[1];display('指挥官:命令' + id + '号飞船开始飞行');mediator('{"id":"' + id +'", "command": "fly"}');},stopToFly: function(e) { var id = e.parentNode.firstChild.innerHTML.toString()[1];display('指挥官:命令' + id + '号飞船停止飞行');mediator('{"id":"' + id +'", "command": "stop"}');},destroy: function(e) { var id = e.parentNode.firstChild.innerHTML.toString()[1];display('指挥官:命令' + id + '号飞船销毁');for (var i = 0; i < command.getElementsByTagName('div').length; i++) {if (command.getElementsByTagName('span')[i].innerHTML.toString()[1] == id) command.removeChild(command.getElementsByTagName('div')[i]);}mediator('{"id":"' + id +'", "command": "destroy"}');}}function display(signal) {var order = document.getElementById('order');order.innerHTML += signal;}function mediator (jsonStr) { var jsonObj = JSON.parse(jsonStr); if (Math.random() <= error) display(',信息传送失败<br>');else {setTimeout(function() {display(',信息传送成功<br>');var spaceship = [];for (var i = 0; i < spaceships.length; i++) spaceship.push(spaceships[i]); for (var i = 0; i < spaceship.length; i++) { if (typeof spaceship[i].signalSystem === 'function') spaceship[i].signalSystem(jsonObj.id, jsonObj.command); }}, 1000);}}
1 0
- 【ife】任务二十六:行星与飞船(一)
- 【ife】任务二十七:行星与飞船(二)
- 【ife】任务二十八:行星与飞船(三)
- 行星和飞船——项目拾金(一)
- 【ife】任务二十二:JavaScript和树(一)
- 【ife】任务十六:零基础JavaScript编码(四)
- 【ife】任务二:零基础HTML及CSS编码(一)
- 【ife】任务二十九:表单(一)单个表单项的检验
- 【ife】任务二十三:JavaScript和树(二)
- IFE-JS 任务一:零基础JavaScript编码(一)
- 【ife】任务十四:零基础JavaScript编码(二)
- 【ife】任务十九:基础JavaScript练习(二)
- 【ife】任务二十:基础JavaScript练习(三)
- 【ife】任务二十一:基础JavaScript练习(四)
- 【ife】任务二十四:JavaScript和树(三)
- 【ife】任务二十五:JavaScript和树(四)
- 【ife】任务三十四:听指令的小方块(二)
- 【ife】任务四十一:UI组件之日历组件(二)
- Box2D v2.3.0 用户指南(第七章)
- Oracle列转行函数 Listagg() 语法详解及应用实例
- gdb调试(c++)
- Box2D v2.3.0 用户指南(第八章)
- Hadoop Yarn内存资源隔离实现原理——基于线程监控的内存隔离方案
- 【ife】任务二十六:行星与飞船(一)
- Box2D v2.3.0 用户指南(第九章)
- FFMPEG 解码音频
- Box2D v2.3.0 用户指南(第十章)
- C++Primer第五版 第五章习题答案(21~25)
- 51Node Treecnt 1月1日
- 找到的关于java UUID的讲解
- 【C++】学习笔记二十三——字符函数库cctype
- 关于生成抽样随机数