Javascript错误:未捕获的ReferenceError:[function]未定义

kuk*_*c67 1 javascript

我有两个js文件,都由一个HTML文件调用.

<script type="text/javascript" src="scripts/levelmovement.js"></script>
<script type="text/javascript" src="scripts/generation.js"></script>
Run Code Online (Sandbox Code Playgroud)

levelmovement.js的一部分:

function moveLevel(){
    if(firstreset == true){
        resetTime();
    }
    var time2 = new Date();
    var millis2 = time2.getTime();
    var millis3 = millis2 - millis1;
    poschange = Math.floor(millis3 / 5);
    for(i = 0; i < chunkpos.length; i++){
        temppos[i] = chunkpos[i] - poschange;
        if(temppos[i] <= -150){
            temppos[i] += 1200;
            generate(i);
        }
        pos = temppos[i];
        document.getElementById('chunk' + i).setAttribute('style','left: ' + pos + 'px;');
    }
}
Run Code Online (Sandbox Code Playgroud)

函数"moveLevel()"的调用方式如下:

window.onload = function(){
    gameLoop();
}

function gameLoop(){
    if(currentscreen == 'playing'){
        moveLevel();
    }
    setTimeout('gameLoop()',1);
}
Run Code Online (Sandbox Code Playgroud)

整个generation.js:

var generatedtop;
var howtogentop = 'gen';
var howtogenbottom = 'gen';
var chunktogenerate = 0;

function topGen(g){
    document.getElementById('t' + chunktogenerate).setAttribute('src','images/terrain/t' + g + '.png');
    if(g == 'gap0'){
        howtogentop = 'gap';
    }
    else{
        howtogentop = 'gen';
    }

    if(g == 'gap0' || g == 'gap2'){
        generatedtop = 'gap';
    }
    else{
        generatedtop = 'default';
    }
}

function bottomGen(g){
    document.getElementById('b' + chunktogenerate).setAttribute('src','images/terrain/b' + g + '.png');
    if(g == 'gap0'){
        howtogenbottom = 'gap';
    }
    else{
        howtogenbottom = 'gen';
    }
}

function generate(chunknum){
    chunktogenerate = chunknum;
    var rand1 = Math.floor(Math.random()*100)+1;
    var rand2 = Math.floor(Math.random()*100)+1;
    if(howtogentop == 'gen'){
        if(rand1 <= 25){
            topGen('space');
        }
        if(rand1 <= 50 && rand1 > 25){
            topGen('jump');
        }
        if(rand1 <= 75 && rand1 > 50){
            topGen('slide');
        }
        if(rand1 > 75){
            topGen('gap0');
        }
    }
    if(howtogentop == 'gap'){
        topGen('gap2');
    }

    if(howtogenbottom == 'gen'){
        if(generatedtop == 'gap'){
            if(rand2 <= 33){
                bottomGen('space');
            }
            if(rand2 <= 66 && rand2 > 66){
                bottomGen('jump');
            }
            if(rand2 > 66){
                bottomGen('gap0');
            }
        }
        if generatedtop != 'gap'){
            if(rand2 <= 25){
                bottomGen('space');
            }
            if(rand2 <= 50 && rand2 > 25){
                bottomGen('jump');
            }
            if(rand2 <= 75 && rand2 > 50){
                bottomGen('jump');
            }
            if(rand2 > 75){
                bottomGen('gap0');
            }
        }
    }
    if(howtogenbottom == 'gap'){
        bottomGen('gap2');
    }
}
Run Code Online (Sandbox Code Playgroud)

我已检查过所有内容,"moveLevel()"仅在我删除此行代码时才有效:

generate(i);
Run Code Online (Sandbox Code Playgroud)

看起来好像浏览器看不到"generate()"函数,我不知道为什么......

Dan*_*nte 5

这一行:

if generatedtop != 'gap'){
Run Code Online (Sandbox Code Playgroud)

缺少一个括号.正确的是:

if(generatedtop != 'gap'){
Run Code Online (Sandbox Code Playgroud)