<!DOCTYPE html>
Document
<br> body{<br> background-color: rgb(0, 128, 128);<br> }<br> #all{<br> position: relative;<br> width: 1000px;<br> height: 670px;<br> background-image: url(./images/background1.jpg);</p> <pre><code> } #boss{ position: absolute; right: -16px; top:305px; } #plant{ position: absolute; left: 251px; top:380px; } /*.per{ position: absolute; left: 300px; top:378px; }*/ </style> </code></pre> <p></head><br> <body><br> <!-- <embed src="./music/bgm.mp3" autostart="true" loop="true" hidden="true" /> --><br> <div id="all"><img src="./images/plant1.gif" id="plant" onclick="make()"><img src="./images/Zombie.gif" id="boss"><!-- <img src="./images/hit1.gif" id="per" class="per"> --></div><br> </body></p> <script type="text/javascript"> var count=0; var hitm=0; var mit=0; var mit1=0; function walk(){ //僵尸行走 if(count<630){ count+=5; var boss=document.getElementById('boss'); boss.style.right =count+'px'; mit=setTimeout(walk,300);} else{ clearTimeout(mit); document.getElementById('plant').style.display = 'none'; var aimg=document.createElement('img'); aimg.src='./images/over.png'; document.getElementById('all'); all.appendChild(aimg); } } function make(){ //创建子弹 hitm=280; var all=document.getElementById('all'); // var img=all.lastChild; // var Cimg=img.cloneNode(true); // all.appendChild(Cimg); var img=document.createElement('img'); img.style.position = 'absolute'; img.style.left = '300px'; img.style.top='378px'; img.setAttribute("src", "./images/hit1.gif") all.appendChild(img); hits(); } var num=2; function hits(){ //发射 if(hitm<950){ hitm+=5; var per=document.getElementById('all'); per.lastChild.style.left=hitm+'px'; mit1=setTimeout(hits,1);} else{ clearTimeout(mit1); document.getElementsByTagName('img')[2].style.display = 'none'; } num++; } walk(); </script> <p></html><br> 但存在错误,第一发子弹会在第二发子弹发射后停止调用函数</p>