简体   繁体   English

改变动画速度

[英]Change animation speed

I am creating a new "whack-a-mole" style game where the children have to hit the correct numbers in accordance to the question. 我正在创造一种新的“打鼹鼠”风格的游戏,孩子们必须根据问题打出正确的数字。 So far it is going really well, I have a timer, count the right and wrong answers and when the game is started I have a number of divs called "characters" that appear in the container randomly at set times. 到目前为止它真的很顺利,我有一个计时器,计算正确和错误的答案,当游戏开始时,我有一些称为“字符”的div,它们在设定的时间随机出现在容器中。

I have taken away the "play" button and have replaced it with "easy", "medium" and "hard". 我已经拿走了“播放”按钮并用“简单”,“中等”和“硬”取而代之。 When a mode is clicked I want the speed to change. 单击模式时,我希望速度发生变化。 The three button share the class "game_settings" 三个按钮共享“game_settings”类

Here is the code that makes deals with the animation 这是处理动画的代码

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

How would I make it so that these settings change in accordance the the difficulty pressed at the beginning? 如何使这些设置根据开始时按下的难度而改变?

Fiddle: http://jsfiddle.net/pUwKb/53/ 小提琴: http//jsfiddle.net/pUwKb/53/

Your buttons do not share the class 'game_ettings', they are inside the div with a class 'game_settings', so the game starts also in case you click between the buttons. 你的按钮不共享类'game_ettings',它们在div中有一个'game_settings'类,所以游戏也会在你点击按钮之间启动。 modify it like this: 像这样修改它:

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

In your timer function remove the line: 在您的计时器功能中删除该行:

$("#btnstart").bind("click", startplay);

And in your function moveRandom you use the AnitmationSpeed: 在你的函数moveRandom中你使用AnitmationSpeed:

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

You find a working demo here . 在这里找到一个工作演示。

What I think you want to do is set the timeInterval according to the game difficulty. 我认为你想要做的是根据游戏难度设置timeInterval。 This is how I think you might get it to work. 这就是我认为你可以让它发挥作用的方式。

Changes to be made: 要做的更改:

html: 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: 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);
    }

Set the time intervals as you like. 根据需要设置时间间隔。

Note: This is just an idea what it should be like. 注意:这只是一个想法应该是什么样的。 You can ofcourse make it more efficient as you know your code better that anyone else. 您可以通过更好的方式提高效率,因为您比其他人更了解您的代码。

In DotNet you need to "stop" the storyboard and restart with speed modification. 在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

Perhaps it is the same in JavaScript? 也许它在JavaScript中是一样的?

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

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