2017-04-30 3 views
-1

Ich habe ein Skript in meine HTML-Datei für JavaScript eingefügt. Ich versuche eine Version des Spiels Pig zu machen. Trotz der Verwendung von onClick-Funktionen zum Aufrufen meiner Funktionen scheint keine von ihnen zu funktionieren, und nur der HTML-Code funktioniert. Dies ist der Code, den ich bisher habe:Wie führe ich meinen Code Javascript in HTML-Code?

<html> 
<head> 
    <style> 
     .dice{ 
      background-color: rgb(102, 255, 255); 
      height: 200px; 
      width: 200px; 
      border-style: solid; 
     } 

     h1{ 
      font-family: "Verdana"; 
      color: (102, 255, 255); 
     } 

    </style> 
    <script> 
     var turn1; //The collection of all points gathered in player1's turn, until player rolls a 1 
     var turn2; //The collection of all points gathered in player2's turn, until player rolls a 1 
     var p1 = 0; //Player1 (human) score 
     var p2 = 0; //Player2 (computer) score 
     var pts; //the value from the dice 

     function score(){ 
      text("Player 1 Score: "); //Will display player1 score 
      text("Player 2 Score: "); //Will display player2 score 
     } 

     function skip(){ //Function will run if player clicks to skip 
      //p1 = p1 + turn1; 
      turn2 = 0; 
      pts = random(1,6); 
      if (pts == 1){ //Sets turn value to 0 and switches to player1's turn 
       textFont("Verdana", 100); 
       console.log("1", 500, 200); 
       turn2 = 0; 
       p2 = p2; 
      } 
      else if (pts > 1){ //Sets turn2 value to points collected, and adds it to player2's points 
       textFont("Verdana", 100); 
       text("1", 500, 200); 
       turn2 = turn2 + pts; 
       p2 = p2 + turn2; 
       return p2; 
      } 
      else{} 
     } 

     function roll(){ //Function will run if player clicks to roll dice 
      turn1 = 0; 
      pts = random(1,6); //Set the dice value to a random number from 1 to 6 
      if (pts == 1){ //Sets turn value to 0 and switches to player2's turn 
       turn1 = 0; 
       p1 = p1; 
       textFont("Verdana", 100); 
       text("Your turn has ended!"); 
       skip(); 
      } 
      else if (pts > 1){ //Sets turn1 value to points collected, and adds it to player1's points 
       textFont("Verdana", 100); 
       text(pts, 350, 200); 
       turn1 = turn1 + pts; 
       p1 = p1 + turn1; 
       return p1; 
      } 
      else{} 
     } 
    </script> 
</head> 

<body onload="score();"> 
    <h1>Pyg!</h1> 
    <div> 
     <button onClick="roll();" style="cursor: pointer">Roll</button> 
     <button onClick="skip();" style="cursor: pointer">Skip</button> 
     <div class="dice"></div> 
    </div> 
</body> 

+1

Jeder Fehler in Ihrem Browser-Konsole? – Lal

+0

Eine Funktion mit dem Namen 'text' fehlt in Ihrem Code. – yuriy636

+0

Ändern Sie auch 'onClick' zu' onclick'. –

Antwort

0

Haben Sie Ihren vollständigen Code geschrieben ?. Ich habe versucht, Ihren Code auszuführen, er hat nicht die Funktion random() und löst einen Fehler aus. Bitte fügen Sie diese Funktion hinzu.

Wenn Sie versuchen, eine Zufallszahl zwischen 2 Ziffern im Schritt random (1,6) hinzufügen möchte, fügen Sie die folgende Funktion zu Ihrem Skript

function random(min,max){Math.floor(Math.random() * max) + min} 

* Anmerkung: die min und max ersetzen mit Ihrem Mindest- und Höchstwert, zwischen denen die Anzahl generiert werden muss.

0

Wenn Sie Zufallszahl verwenden Sie die Math-Bibliothek verwenden, wie diese Math.random(1,6);

Ich habe suposed dass text = document.getelementById("");

Verwandte Themen