Javascript Slot Machine Bug?

Javascript Slot Machine Bug?

本文关键字:Bug Machine Slot Javascript      更新时间:2023-09-26

我正在编写一种用JS和PHP编写的老虎机。

我在网上找到了一个脚本,其中有3个"项目"。

所以现在我添加了一些,但它只计数的数字,而不是所有的数字定义在卷轴数组?…

我的问题是为什么?

下面是我的代码:

<script type="text/javascript">
/*
    requestAnimationFrame polyfill
*/
(function(w){
    var lastTime = 0,
        vendors = ['webkit', /*'moz',*/ 'o', 'ms'];
    for (var i = 0; i < vendors.length && !w.requestAnimationFrame; ++i){
        w.requestAnimationFrame = w[vendors[i] + 'RequestAnimationFrame'];
        w.cancelAnimationFrame = w[vendors[i] + 'CancelAnimationFrame']
            || w[vendors[i] + 'CancelRequestAnimationFrame'];
    }
    if (!w.requestAnimationFrame)
        w.requestAnimationFrame = function(callback, element){
            var currTime = +new Date(),
                timeToCall = Math.max(0, 16 - (currTime - lastTime)),
                id = w.setTimeout(function(){ callback(currTime + timeToCall) }, timeToCall);
            lastTime = currTime + timeToCall;
            return id;
        };
    if (!w.cancelAnimationFrame)
        w.cancelAnimationFrame = function(id){
        clearTimeout(id);
    };
})(this);
/*
    Slot Machine
*/
var sm = (function(undefined){
    var tMax = 3000, // animation time, ms
        height = 210,
        speeds = [],
        r = [],
        reels = [
            ['1',  '2', '3', '4', '5', '6', '7', '8', '9', '0'],
            ['1',  '2', '3', '4', '5', '6', '7', '8', '9', '0'],
            ['1',  '2',  '3', '4', '5', '6', '7', '8', '9', '0']
        ],
        $reels, $msg,
        start;
    function init(){
        $reels = $('.reel').each(function(i, el){
            el.innerHTML = '<div><p>' + reels[i].join('</p><p>') + '</p></div><div><p>' + reels[i].join('</p><p>') + '</p></div>'
        });
        $msg = $('.msg');
        $('button').click(action);
    }
    function action(){
        if (start !== undefined) return;
        for (var i = 0; i < 3; ++i) {
            speeds[i] = Math.random() + .5; 
            r[i] = (Math.random() * 3 | 0) * height / 3;
        }
        $msg.html('Spinning...');
        animate();
    }
    function animate(now){
        if (!start) start = now;
        var t = now - start || 0;
        for (var i = 0; i < 3; ++i)
            $reels[i].scrollTop = (speeds[i] / tMax / 2 * (tMax - t) * (tMax - t) + r[i]) % height | 0;
        if (t < tMax)
            requestAnimationFrame(animate);
        else {
            start = undefined;
            check();
        }
    }
    function check(){
        $msg.html(
            r[0] === r[1] && r[1] === r[2] ?
                'You won! Enjoy your ' + reels[1][ (r[0] / 70 + 1) % 3 | 0 ].split(' ')[0]
            :
                'Try again'
        );
    }
    return {init: init}
})();
$(sm.init);
</script>

我不知道错误在哪里或者它可能在哪里?

单轮只显示数字1-4,不显示0-9这是必须的。

我疏忽了哪一部分?

如果您想让for循环计数到数组中的最大值,您必须告诉它这样做。目前你仍然让它在3之前停止,所以它就是这样做的。