2014-06-20 53 views
0

我正在製作一個有多個關卡的html5遊戲。他們(不正確)被定義,因爲我得到錯誤Uncaught TypeError: Cannot read property 'length' of undefined。我認爲我的var格式不正確。應該怎麼做?遊戲等級var是undefined

var levels = ([ 
     { factor: 1.2, duration: 100, multiplier: 0.5 }, 
     { factor: 1.5, duration: 200, multiplier: 0.6 }, 
     { factor: 1.9, duration: 300, multiplier: 0.7 }, 
     { factor: 2.3, duration: 450, multiplier: 0.8 }, 
     { factor: 2.8, duration: 600, multiplier: 1.0 }, 
     { factor: 3.3, duration: 800, multiplier: 1.1 }, 
     { factor: 3.9, duration: 1000, multiplier: 1.3 }, 
     { factor: 4.4, duration: 1500, multiplier: 1.7 }, 
     { factor: 5.6, duration: 2000, multiplier: 2.0 } 
    ] [ 
     { factor: 1.2, duration: 100, multiplier: 0.5 }, 
     { factor: 1.8, duration: 200, multiplier: 0.6 }, 
     { factor: 2.3, duration: 300, multiplier: 0.7 }, 
     { factor: 2.5, duration: 450, multiplier: 0.8 }, 
     { factor: 3.2, duration: 600, multiplier: 1.0 }, 
     { factor: 3.6, duration: 800, multiplier: 1.1 }, 
     { factor: 4.0, duration: 1100, multiplier: 1.3 }, 
     { factor: 4.8, duration: 1700, multiplier: 1.7 }, 
     { factor: 5.6, duration: 2400, multiplier: 2.0 } 
    ] 
    ); 

function generateLevelSelector() { 
     var html = ''; 

     for(var i = 1, len = levels.length; i <= len; i++) { 
      html += '<li data-level="' + i + '">' + i + '</li>' 
     } 

     // Find the list element that will hold all levels 
     var list = levelSelector.getElementsByTagName('ul')[0]; 

     list.innerHTML = html; 

     var listItems = levelSelector.getElementsByTagName('li'); 

     for(var i = 0, len = listItems.length; i < len; i++) { 
      listItems[i].addEventListener('click', levelSelectedHandler, false); 
     } 

     updateLevelSelector(); 

    } 
+0

哪條線發生錯誤?在'for ....(。levels.length; ....)'或'for ...(... listItems.length; ....)'處? 如果後者'levelSelector.getElementsByTagName('li');可能會返回undefined/null! – MABVT

+0

for(var i = 1,len = levels.length; i <= len; i ++){ –

回答

1

敷在[]中的代替()

var levels = [[ 
     { factor: 1.2, duration: 100, multiplier: 0.5 }, 
     { factor: 1.5, duration: 200, multiplier: 0.6 }, 
     { factor: 1.9, duration: 300, multiplier: 0.7 }, 
     { factor: 2.3, duration: 450, multiplier: 0.8 }, 
     { factor: 2.8, duration: 600, multiplier: 1.0 }, 
     { factor: 3.3, duration: 800, multiplier: 1.1 }, 
     { factor: 3.9, duration: 1000, multiplier: 1.3 }, 
     { factor: 4.4, duration: 1500, multiplier: 1.7 }, 
     { factor: 5.6, duration: 2000, multiplier: 2.0 } 
    ], [ 
     { factor: 1.2, duration: 100, multiplier: 0.5 }, 
     { factor: 1.8, duration: 200, multiplier: 0.6 }, 
     { factor: 2.3, duration: 300, multiplier: 0.7 }, 
     { factor: 2.5, duration: 450, multiplier: 0.8 }, 
     { factor: 3.2, duration: 600, multiplier: 1.0 }, 
     { factor: 3.6, duration: 800, multiplier: 1.1 }, 
     { factor: 4.0, duration: 1100, multiplier: 1.3 }, 
     { factor: 4.8, duration: 1700, multiplier: 1.7 }, 
     { factor: 5.6, duration: 2400, multiplier: 2.0 } 
    ] 
    ]; 
0

你的陣列創建似乎已損壞。

var levels = [ // <--- round brackes only with new Array, in this case square brackets 
[ 
    { factor: 1.2, duration: 100, multiplier: 0.5 }, 
    { factor: 1.5, duration: 200, multiplier: 0.6 }, 
    { factor: 1.9, duration: 300, multiplier: 0.7 }, 
    { factor: 2.3, duration: 450, multiplier: 0.8 }, 
    { factor: 2.8, duration: 600, multiplier: 1.0 }, 
    { factor: 3.3, duration: 800, multiplier: 1.1 }, 
    { factor: 3.9, duration: 1000, multiplier: 1.3 }, 
    { factor: 4.4, duration: 1500, multiplier: 1.7 }, 
    { factor: 5.6, duration: 2000, multiplier: 2.0 } 
], // <--- MISSED COMMA? 
[ 
    { factor: 1.2, duration: 100, multiplier: 0.5 }, 
    { factor: 1.8, duration: 200, multiplier: 0.6 }, 
    { factor: 2.3, duration: 300, multiplier: 0.7 }, 
    { factor: 2.5, duration: 450, multiplier: 0.8 }, 
    { factor: 3.2, duration: 600, multiplier: 1.0 }, 
    { factor: 3.6, duration: 800, multiplier: 1.1 }, 
    { factor: 4.0, duration: 1100, multiplier: 1.3 }, 
    { factor: 4.8, duration: 1700, multiplier: 1.7 }, 
    { factor: 5.6, duration: 2400, multiplier: 2.0 } 
] 
];