1

我对 JS 和 JQuery 有点陌生。我正在尝试在 JQuery 中创建一种 GuitarHero 游戏,但音符并没有顺利下降我所做的基本上是setTimeout()在一个循环中调用 - 每个都有不同的超时(以数组形式给出 - “歌曲 scipt”如果你愿意)。代码如下所示:

while(counter < curSong.numNotes){
                // set the parameters sent to runCreateNote
        runCreateNote(counter, margin, entity, interval, name);
        counter++;
}

runCreateNote调用时setTimeout()(用于范围目的)

这是动画代码:

    noteBall.animate({
            top: (noteBall.parent().height() - noteBall.height()*2) + 'px'
        }, {
            duration: fallingtime,
            queue: false,
            easing: "linear",
            step: function() {
                checkStep(id);  
            },
            complete: function() {
                reachBottom(id);    
            }
    });

    function checkStep(id){

        var noteBall = $("#" + id);
        var name = id.substring(0,1);
        if (name == "R"){
            if (isInRange(noteBall)){
                isRedIn = true;
            } else {
                isRedIn = false;
            }
        }
        if (name == "B"){
            if (isInRange(noteBall)){
                isBlueIn = true;
            } else {
                isBlueIn = false;
            }
        }
        if (name == "G"){
            if (isInRange(noteBall)){
                isGreenIn = true;
            } else {
                isGreenIn = false;
            }
        }
        if (name = "O"){
            if (isInRange(noteBall)){
                isOrangeIn = true;
            } else {
                isOrangeIn = false;
            }
        }

}

我不知道是否有任何代码是相关的,但我只是想表明动画看起来不太重(我认为它会运行顺利)

我错过了一些关键原则吗?

4

1 回答 1

0

Well, without seeing the rest of your code or a demo of the application, my guess would be that this is the line giving you problems:

top: (noteBall.parent().height() - noteBall.height()*2) + 'px'

If you could calculate those values ahead of time and cache them you'd probably be better off.

You also should consider taking all those if statements in checkStep and moving them into a switch statement (or at least if/else) -- assuming the cases are mutually exclusive.

于 2011-12-03T20:06:30.713 回答