簡體   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