2016-11-16 2 views
0

Ich mache ein JS-Spiel, und ich muss die Highscores aktualisieren und sie mit Cookies anzeigen. Die Funktionen sind unten in eine DateiTypeError: Datensatz ist undefined

function getHighScoreTable() { 
    var table = new Array(); 
    for (var i = 0; i < 10; i++) { 
     // Contruct the cookie name 
     var cookie_name = "player" + i; 
     // Get the cookie value using the cookie name 
     var cookie_value = getCookie(cookie_name); 
     // If the cookie does not exist exit from the for loop 
     if (!cookie_value) { 
      break; 
     } 
     // Extract the name and score of the player from the cookie value 
     var value_array = cookie_value.split("~"); 
     var pname = value_array[0]; 
     var pscore = value_array[1]; 
     // Add a new score record at the end of the array 
     table.push(new ScoreRecord(pname, pscore)); 
    } 
    return table; 
} 
// 
// This function stores the high score table to the cookies 
// 
function setHighScoreTable(table) { 
    for (var i = 0; i < 10; i++) { 
     // If i is more than the length of the high score table exit 
     // from the for loop 
     if (i >= table.length) break; 
     // Contruct the cookie name 
     var cookie_name = "player" + i; 
     var record = table[i]; 
     var cookie_value = record.name + "~" + record.score; // **error here = TypeError: record is undefined** 
     // Store the ith record as a cookie using the cookie name 
     setCookie(cookie_name, cookie_value); 
    } 
} 

in meinem game.js highscore.js, ich habe eine Funktion, gameover(), die die Highscores usw. behandelt und die gametimers löschen.

function gameOver() { 
    clearInterval(gameInterval); 
    clearInterval(timeInterval); 
    alert("game over!"); 
    var scoreTable = getHighScoreTable(); 
    var record = ScoreRecord(playerName, score); 
    var insertIndex = 0; 
    for (var i = 0; i < scoreTable.length; i++) { 
     if (score >= scoreTable[i].score) { 
      insertIndex = i; 
      break; 
     } 
    } 
    if (scoreTable.length == 0) { 
     scoreTable.push(record); 
    } else { 
     scoreTable.splice(insertIndex, 0, record); 
    } 
    setHighScoreTable(scoreTable); 
    showHighScoreTable(scoreTable); 
} 

Wenn der Spiel in Gameover genannt wird, treten Fehler in setHighScoreTable (Tabelle), und der Fehler ist, dass Aufzeichnung (d.h.e Tabelle [i]) nicht definiert ist. Brauchen Sie Hilfe in diesem Fehler.

+1

Sollten Sie nicht schreiben 'getCookie (cookie_name); 'anstelle von' getCookie ("cookie_name"); '? – gus27

+0

Sie könnten einfach 'scoreTable.push (record); scoreTable.sort ((a, b) => a.score - b.score); ' – Tibrogargan

+0

Neben diesem' getCookie (cookie_name) 'Ding sehe ich kein Problem in Ihrem Code. Können Sie die genaue Fehlermeldung und die Zeile angeben, in der der Fehler auftritt? – gus27

Antwort

1

ScoreRecord Unter der Annahme, etwa wie folgt definiert:

function ScoreRecord(name, score) { 
    this.name = name; 
    this.score = score; 
} 

Das Problem ist, Sie tun:

record = ScoreRecord(playerName, score); 

Dies wird nur den Konstruktor aufrufen, als ob es sich um eine Funktion waren - aber es gibt nichts zurück . Fügen Sie einfach den new Schlüsselwort ein neues Objekt statt

record = new ScoreRecord(playerName, score); 

Man könnte auch tun, so etwas zu schaffen, den Konstruktor zu verhindern, als eine normale Funktion aufgerufen wird:

function ScoreRecord(name, score) { 
    "use strict" 

    if (!(this instanceof ScoreRecord)) { 
     throw new Error("ScoreRecord must be called with the new keyword"); 
    } 
    this.name = name; 
    this.score = score; 
} 
+0

ja ich habe den Break-Code, wenn es die table.length überschreitet –

Verwandte Themen