SlotMachine

//            SlotMachine
            var goButton = $("#go_button");
            var completed = 0,
                imgHeight = 776,
                posArr = [
                    0, //4.4
                    194, //12.5
                    194*2, //38.1
                    194*3
                ];

            window.setTimeout(function(){
                    go();
                },600);

            $.addPressListener(goButton,
                 {
                     "onPressEnd": function (event)
                     {
                         go();
                     }
                 });

            function go(){
                document.body.style.pointerEvents = "none";
                a.start();
                b.start();
                c.start();
                $(‘#text‘).style.webkitTransition = "opacity 1s";
                $(‘#text‘).style.opacity = "0";
                disableControl()

                x = window.setInterval(function() {
                    if(a.speed >= a.maxSpeed && b.speed >= b.maxSpeed && c.speed >= c.maxSpeed) {
                        enableControl();
                        window.clearInterval(x);
                    }
                }, 100);

                 window.setTimeout(function(){
                    a.stop();
                    b.stop();
                    c.stop();
                     window.setTimeout(function(){
                        $(‘#text‘).style.webkitTransition = "opacity 1s";
                        $(‘#text‘).style.opacity = "1";
                        document.body.style.pointerEvents = "auto";
                     },2500);
                 },3500);
            }

            function enableControl() {
                J(‘#go_button‘).attr("disabled", false);
            }

            function disableControl() {
                J(‘#go_button‘).attr("disabled", true);
            }

            //create slot objects
            var a = new Slot(‘#machine1‘, 70, 3.5),
                b = new Slot(‘#machine2‘, 65, 3),
                c = new Slot(‘#machine3‘, 70, 4);

            function Slot(el, max, step) {
                this.speed = 0; //speed of the slot at any point of time
                this.step = step; //speed will increase at this rate
                this.si = null; //holds setInterval object for the given slot
                this.el = el; //dom element of the slot
                this.maxSpeed = max; //max speed this slot can have
                this.pos = null; //final position of the slot	

                J(el).pan({
                    fps:30,
                    dir:‘down‘
                });
                J(el).spStop();
            }

            //start
            Slot.prototype.start = function() {
                var _this = this;
//                J(_this.el)[0].style.backgroundImage = "url(../images/reel_blur.png)";
                J(_this.el).spStart();
                _this.si = window.setInterval(function() {
                    if(_this.speed < _this.maxSpeed) {
                        _this.speed += _this.step;
                        J(_this.el).spSpeed(_this.speed);
                    }
                }, 100);
            };

            //stop
            Slot.prototype.stop = function() {
                var _this = this,
                    limit = 15;
                clearInterval(_this.si);
                _this.si = window.setInterval(function() {
                    if(_this.speed > limit) {
                        _this.speed -= _this.step;
                        J(_this.el).spSpeed(_this.speed);
                    }
                    if(_this.speed <= limit) {
                        _this.finalPos(_this.el);
                        J(_this.el).spSpeed(0);
                        J(_this.el).spStop();
                        clearInterval(_this.si);
                        J(_this.el).removeClass(‘motion‘);
                        _this.speed = 0;
                    }
                }, 100);
            };

            //final Position
            Slot.prototype.finalPos = function() {
                var el = this.el,
                    el_id,
                    pos,
                    posMin = 2000000000,
                    best,
                    bgPos,
                    i,
                    j,
                    k;

                el_id = J(el).attr(‘id‘);
                //pos = J(el).css(‘background-position‘); //for some unknown reason, this does not work in IE
                pos = document.getElementById(el_id).style.backgroundPosition;
                pos = pos.split(‘ ‘)[1];
                pos = parseInt(pos, 10);

                for(i = 0; i < posArr.length; i++) {
                    for(j = 0;;j++) {
                        k = posArr[i] + (imgHeight * j);
                        if(k > pos) {
                            if((k - pos) < posMin) {
                                posMin = k - pos;
                                best = k;
                                this.pos = posArr[i]; //update the final position of the slot
                            }
                            break;
                        }
                    }
                }

            var number =  Math.floor(best / imgHeight);
                best = number * imgHeight + imgHeight / 2;//fixed position

                bgPos = "0 " + best + "px";

                J(el).animate({
                    backgroundPosition:"(" + bgPos + ")"
                }, {
                    duration: 1000,
                    complete: function() {
                        completed ++;
                    }
                });
            };

  

#SlotMachine-background
{
    position:absolute;
    left: 98px;
    top: 223px;
    width: 734px;
    height: 442px;
    background-image: url(../images/SlotMachine-background.98-223.png);
    background-size: 734px 442px;
    background-repeat: no-repeat;
}
#go_button{
    width: 44px;
    height: 220px;
    left: 678px;
    top: 120px;
    position: absolute;
}
#machine1, #machine2, #machine3{
    position: absolute;
    width: 148px;
    height: 194px;
}
#machine1{
    background-image: url(../images/reel_normal-1.png);
    left: 137px;
    top: 127px;
}
#machine2{
    background-image: url(../images/reel_normal-2.png);
    left: 314px;
    top: 127px;
}
#machine3{
    background-image: url(../images/reel_normal-3.png);
    left: 490px;
    top: 127px;

}
#text
{
    position:absolute;
    left: 278px;
    top: 488px;
    width: 419px;
    height: 37px;
    background-image: url(../images/text.278-488.png);
    background-size: 419px 37px;
    background-repeat: no-repeat;
    opacity: 0;
}

#title-text
{
    position:absolute;
    left: 230px;
    top: 269px;
    width: 497px;
    height: 60px;
    background-image: url(../images/title-text.230-269.png);
    background-size: 497px 60px;
    background-repeat: no-repeat;
}

#paper-pop-up
{
    background-image: url(../../_/images/paper-list.png);
    background-position: center;
}

#paper-pop-up >.close-button
{
    position:absolute;
    left: 945px;
    top: 85px;
    width: 42px;
    height: 42px;
    opacity: 0;
}

  用到的框架:

jquery-1.6.1.min.js
jquery.backgroundPosition.js
jquery.spritely.js

  

SlotMachine,布布扣,bubuko.com

时间: 2024-10-15 03:29:22

SlotMachine的相关文章

JS组件系列——还记得那些年玩过的游戏机吗?(SlotMachine组件简易实现)

前言:前两天在网上找组件,无意中发现了我们儿时游戏机效果的“SlotMachine组件”,浏览一遍下来,勾起了博主小时候满满的回忆.于是下定决定要研究下这么一个东西,不得不再次叹息开源社区的强大,原来这些组件已经被封装得这么好了,使用起来如此简单.下面就让博主带着大家来看看这么一个神奇的组件——SlotMachine吧. 一.组件预览 先来一发简单的效果压压惊 觉得太简单?别急,好戏在后头,试试手气先. 什么?还没达到想要的效果,好!下面,真实效果来一发. 博主点击了好长时间,都没有中奖,难怪小

jquery实现移动端slotmachine抽奖游戏

项目中需要写一个抽奖的移动端的小游戏,于是就在网上找了一下插件,然后自己改动了一些样式,以下代码段仅供个人学习参考,非100%原创,有问题的地方可以提出来哦 页面效果: 以下分别为html+css+js代码: 先引用这两个文件:<script type="text/javascript" src="js/jquery.min.js"></script><script type="text/javascript" sr

cocos2d实现SlotMachine(老*虎*机)

实现一个四个转子,每个转子有五个花色的老*虎机.转子的转动实现原理很简单,和背景图无限滚动的原理是一样的:排成列的精灵在屏幕上向上滚动,再通过裁剪结点的裁剪就造成了转子滚动的效果. 1 void LayerSlotMachine::on_btn_roll(CCObject* pSender, CCControlEvent event) 2 { 3 if (!flag_act_) 4 { 5 schedule(schedule_selector(XLayerSlotMachine::roll_up

水果机抽奖

不过w关于拉霸,水果机  他们的抽奖活动 我做了两次,网上也借鉴了很多案例,但是发现都不是特别全面 因为我们做一个抽奖的活动,肯定是有弹窗的,有中奖和未中奖的判断,以及中奖几率,中哪种奖项的判断 这些都是困扰这我的,因为我找的案例都没有这么全面的例子, 我们先来看第一个案例 是个盛世公主号做的一个抽奖页面: 这个页面 我是借用了这个案例 链接   http://www.17sucai.com/pins/demoshow/26298 引入了这两个js 这个案例是一张背景图上多个奖品, 那关于中奖机

HTML5小游戏-简单抽奖小游戏

换了新工作以后,专注前端开发,平常空闲时间也比较多,可以多钻研一下技术,写一下博客.最近在学习canvas,参考网上的slotmachine插件,用canvas实现了一个简单抽奖小游戏.        知识点 canvas绘制背景 canvas绘制图片 canvas绘制边框 canvas事件处理 canvas简单动画制作      步骤 1.准备好图片,首先是机器的外观.以及滚动的奖项图片,我一共准备了6种,奖项图片按照一定的规律命名,这样方便处理   2.准备好canvas画布,设置好基本的C