更改动画速度

12
我正在制作一个新的“打地鼠”样式的游戏,孩子们需要按照问题的要求击打正确的数字。目前进展顺利,我已经设置了计时器、记录正确和错误答案,当游戏开始时,我会在容器中随机出现一些名为“characters”的div。
我去掉了“播放”按钮,用“简单”、“中等”和“困难”替代。当点击模式时,我希望速度发生变化。这三个按钮共享“game_settings”类。
以下是处理动画的代码。
function randomFromTo(from, to) {
    return Math.floor(Math.random() * (to - from + 1) + from);
}

function scramble() {
    var children = $('#container').children();
    var randomId = randomFromTo(1, children.length);
    moveRandom("char" + randomId);
}

var currentMoving = [];

function moveRandom(id) {
    // If this one's already animating, skip it
    if ($.inArray(id, currentMoving) !== -1) {
        return;
    }

    // Mark this one as animating
    currentMoving.push(id);

    var cPos = $('#container').offset();
    var cHeight = $('#container').height();
    var cWidth = $('#container').width();
    var bWidth = $('#' + id).width();

    var bHeight = $('#' + id).css('top', '395px').fadeIn(100).animate({
        'top': '-55px'
    }, 6000).fadeOut(100);

    maxWidth = cPos.left + cWidth - bWidth;
    minWidth = cPos.left;
    newWidth = randomFromTo(minWidth, maxWidth);

    $('#' + id).css({
        left: newWidth
    }).fadeIn(1000, function () {
        setTimeout(function () {
            $('#' + id).fadeOut(1000);

            // Mark this as no longer animating                
            var ix = $.inArray(id, currentMoving);
            if (ix !== -1) {
                currentMoving.splice(ix, 1);
            }
            window.cont++;
        }, 1000);
    });
}

我该如何使这些设置根据一开始选择的难度进行更改?

示例:http://jsfiddle.net/pUwKb/53/

3个回答

6
您的按钮没有共享“game_settings”类,它们位于具有“game_settings”类的div中,因此如果您在按钮之间单击,游戏也会开始。请像这样进行修改:
// remove this line
$(".game_settings").find('input').click(

// replace it with...
var AnimationSpeed = 6000;

$(".game_settings").find('input').click(function () {
        // here you could set a different timer value for each variant
        // or simply send the classname to startplay and handle the
        // settings there.
        switch($(this).attr('class')) {
          case 'easy':
            AnimationSpeed = 6000;
            break;
          case 'medium':
            AnimationSpeed = 3000;
            break;
          case 'hard':
            AnimationSpeed = 1000;
            break;
        }
        startplay();
 });

在您的定时器函数中删除这一行:
$("#btnstart").bind("click", startplay);

在你的函数moveRandom中,你使用了AnimationSpeed:
var bHeight = $('#' + id).css('top', '395px').
              fadeIn(100).animate({'top': '-55px'}, AnimationSpeed).
              fadeOut(100);

您可以在这里找到一个可用的演示。

我应该用“game_settings”点击函数来交换它吗?你有一个fiddle吗?@axel.michel - Milo-J
@Milo-J 是的,你可以替换它。我没有深入研究你的代码,所以我不知道你想要改变哪个间隔/超时时间的速度。这里有一个fiddle http://jsfiddle.net/axelmichel/rYmNz/,可以修改startplay函数的间隔时间。 - axel.michel
@Milo-J 我已经更新了这个fiddle:http://jsfiddle.net/xgDZ4/2/,现在每个变体中的块移动速度不同。 - axel.michel
有人能解释一下为什么会被踩吗?这个回答哪里有问题? - axel.michel

1
我认为您想要做的是根据游戏难度设置时间间隔。这是我认为您可能使其正常工作的方法。
需要进行的更改: HTML:
//Change
<div class="game_settings">
    <div><input class="easy" type="button" value="Easy"></div>
    <div><input class="medium" type="button" value="Medium"></div>
    <div><input class="hard" type="button" value="Hard"></div>
</div>

//To
<div class="game_settings">
    <div><input class="game-speed" id="easy" type="button" value="Easy"></div>
    <div><input class="game-speed" id="medium" type="button" value="Medium"></div>
    <div><input class="game-speed" id="hard" type="button" value="Hard"></div>
</div>

脚本:

//Change
$(".game_settings").click(function () {
    startplay();
});

//To
$(".game-speed").click(function () {
    startplay($(this).attr('id'));
});


//Change in startPlay()
   startPlay()

   play = setInterval(function () {
        if (window.cont) {
            window.cont--;
            scramble();
        }
    }, 500);



//To
    startplay(speed_check)  // As it is now expecting a variable

    if(speed_check == 'easy'){
        play = setInterval(function () {
            if (window.cont) {
                window.cont--;
                scramble();
            }
        }, 2000);
    }
    else if(speed_check == 'medium'){
        play = setInterval(function () {
            if (window.cont) {
                window.cont--;
                scramble();
            }
        }, 1000);
    }
    else if(speed_check == 'hard'){
        play = setInterval(function () {
            if (window.cont) {
                window.cont--;
                scramble();
            }
        }, 400);
    }
    else{
        play = setInterval(function () {
            if (window.cont) {
                window.cont--;
                scramble();
            }
        }, 1000);
    }

根据您的喜好设置时间间隔。

注意:这只是一个关于它应该如何的想法。当然,您可以根据自己更好地了解代码的情况使其更加高效。


-4
在DotNet中,您需要“停止”故事板并使用速度修改重新启动。
Dim sb as Storyboard = ctype(Me.FindRessources("Storyboard1"), Storyboard)
sb.Stop
Select Case Level
  Case "easy": sb.SpeedRatio = 0.75
  Case "medium": sb.SpeedRatio = 1
  Case "hard": sb.SpeedRatio = 2.0
End Select

sb.Begin

也许在JavaScript中是一样的吗?


它如何与问题相关? - zb'
今天我在我的项目中也有同样的问题:“如何设置与级别相关联的动画”,但是根据你的评价,情况似乎大不相同。 - Nasenbaer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接