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();
}
哪條線發生錯誤?在'for ....(。levels.length; ....)'或'for ...(... listItems.length; ....)'處? 如果後者'levelSelector.getElementsByTagName('li');可能會返回undefined/null! – MABVT
for(var i = 1,len = levels.length; i <= len; i ++){ –