簡體   English   中英

每次我添加 <!DOCTYPE html> 它破壞了我的代碼

[英]Everytime i add <!DOCTYPE html> it breaks my code

添加它使得貓一直向左生成並且無法移動它也會破壞moveguydown功能。 它幾乎打破了一切

看看其他人做了什么來解決這個問題,他們將px添加到寬度和高度屬性但是我非常確定我對所有需要它的人都這樣做了

<script language = "JavaScript">
var numberOfMonster= 0, setupScore, setupMonster, names=["rat.gif"], 
catleft = 325, catright= 250, ratleft, ratright, countElement, howfast= 
10, score = 0;

//This function is the initial setup for the game aka score, sound, 
monster spawn
function myFunction()
{
spawntheMonster( 0 );
document.all.coolscore.innerHTML= 0;
setupScore=setInterval(function(){score++; 
document.all.coolscore.innerHTML=score;}, 1000 );
setupMonster=setInterval(function(){spawntheMonster( 0 );}, 3000 );
document.getElementById('sound').play();
}

 //Next four function are deticated for moving the cat and setting 
boundaries
function leftArrowPressed()
{
  var element = document.getElementById("cat"); 

  if(parseInt(element.style.right.substring(element.style.right.length 
- 2 , 0 )) > 0 ) {
        element.style.right = parseInt(element.style.right) - 
20 + 'px';          
  }

}    

function rightArrowPressed() 
{
   var element = document.getElementById("cat");


if(parseInt(element.style.right.substring(element.style.right.length - 2 , 
0 )) < 480 ) {
          element.style.right = parseInt(element.style.right) 
+ 20 + 'px';
     }
}

function upArrowPressed() 
{
    var element = document.getElementById("cat");

    if(parseInt(element.style.top.substring(element.style.top.length - 2 , 
0 )) > 0 ) {
           element.style.top = parseInt(element.style.top) - 
20 + 'px';
    }
}

function downArrowPressed() 
{
    var element = document.getElementById("cat");


if(parseInt(element.style.top.substring(element.style.top.length - 2 , 0 
)) < 740 ) {
           element.style.top = parseInt(element.style.top) + 
20 + 'px';
    }
}

//connects the id's of arrow keys and w,a,s,d to the previous functions to 
be able to move     
function movetheguy(event){
switch (event.keyCode) {
       case 39:
         leftArrowPressed();
         break;

       case 37:
         rightArrowPressed();
         break;

       case 38:
          upArrowPressed();
          break;

       case 40:
          downArrowPressed();
          break;

       case 68:
          leftArrowPressed();
          break;

       case 65:
          rightArrowPressed();
          break;

        case 87:
           upArrowPressed();
           break;

        case 83:
           downArrowPressed();
           break;
}
}

//sets spawn, attributes, and clickablity of the rats 
function spawntheMonster(monster){

var widthrandom = Math.floor(Math.random() * 112 )* 5 - 20;
widthrandom     = "position:absolute; right: "+widthrandom+"; top: 

000;“; var z = document.createElement(”IMG“);

z.setAttribute("src", names[monster]);
z.setAttribute("style", widthrandom);
z.setAttribute("width", "40");
z.setAttribute("height", "54");
z.setAttribute("id", numberOfMonster+"mon");
z.setAttribute("onLoad", "setInterval(moveguydown, 100, this);");
z.setAttribute("onClick", "this.style.top=parseInt(this.style.top)-75;");

document.getElementById("back1").appendChild(z);
numberOfMonster++;
}

//moves the rats 
function moveguydown(moveMonster){

if(parseInt(moveMonster.style.top)>= 900 ){
    moveMonster.style.top= -500;
    moveMonster.style.right=Math.floor(Math.random() * 112 )* 5 - 
20;  //check this
}
else
    moveMonster.style.top=parseInt(moveMonster.style.top)+howfast;
    overlap(moveMonster);
}

//randomly spawns the rats 
function randomspawn(){

spawntheMonster(Math.floor(Math.random() * names.length));
}


function die(){
var highscore=document.all.coolscore.innerHTML;
var count;
for(count= 0 ; count<numberOfMonster; count++){
    countElement=document.getElementById(count+"mon");
    document.getElementById("back1").removeChild(countElement);
}

numberOfMonster = 0;
document.all.coolscore.innerHTML=
"GAME OVER<br><span onClick='location.reload();'>Click to restart! 
</span><br>SCORE: "+score+
"<font size='5'><br>Thanks to<br>Cat By: PRguitarman<br>Sound By: Jay 
Man<br>Rats By: Yacht Club Games";
clearInterval(setupScore);
clearInterval(setupMonster);
}

//Compares hit boxes and checks to see if you die 
function overlap(obj){
catleft =parseInt(cat.style.right)+ 75;
catright=parseInt(cat.style.right);
ratleft =parseInt(obj.style.right)+parseInt(obj.width);
ratright=parseInt(obj.style.right);

cattop =parseInt(cat.style.top);
catbot=parseInt(cat.style.top)+ 150;
rattop =parseInt(obj.style.top)+parseInt(obj.height);
ratbottom=parseInt(obj.style.top);  

if(rattop<catbot && ratbottom>cattop && ratright<catleft && 
ratleft>catright)

    die();


}

//Switches difficulty and sound
function twospeeds(){

if(howfast== 30 ){//fast
        back1.style.backgroundImage="url('large0.gif')";
        howfast= 10;}
if(howfast== 10){//WAY too fast
        back1.style.backgroundImage="url('large2.gif')";
        howfast= 30;
        document.getElementById('sound').src="sun.mp3";
        document.getElementById('sound').play();
        }
}

這里有幾個問題:

1 - 您沒有關閉腳本標記。 你應該在你的javascript代碼后添加</script>

2 - //運算符用於單行注釋 ,而您用於:

//This function is the initial setup for the game aka score, sound, 
monster spawn

這是一個多行評論。 對於多行注釋,請使用/* some comment */

3 - 定義字符串時,您應該在一行中完成 (至少在HTML文件中使用腳本標記時,就像在您的情況下一樣)。 或者,您可以用反引號(`)替換引號 例如,替換:

document.all.coolscore.innerHTML=
    "GAME OVER<br><span onClick='location.reload();'>Click to restart! 
    </span><br>SCORE: "+score+
    "

通過

document.all.coolscore.innerHTML=
    `GAME OVER<br><span onClick='location.reload();'>Click to restart! 
    </span><br>SCORE: "+score+
    `

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM