繁体   English   中英

改变动画速度

[英]Change animation speed

我正在创造一种新的“打鼹鼠”风格的游戏,孩子们必须根据问题打出正确的数字。 到目前为止它真的很顺利,我有一个计时器,计算正确和错误的答案,当游戏开始时,我有一些称为“字符”的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/

你的按钮不共享类'game_ettings',它们在div中有一个'game_settings'类,所以游戏也会在你点击按钮之间启动。 像这样修改它:

// 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中你使用AnitmationSpeed:

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

在这里找到一个工作演示。

我认为你想要做的是根据游戏难度设置timeInterval。 这就是我认为你可以让它发挥作用的方式。

要做的更改:

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>

Sript:

//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);
    }

根据需要设置时间间隔。

注意:这只是一个想法应该是什么样的。 您可以通过更好的方式提高效率,因为您比其他人更了解您的代码。

在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中是一样的?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM