Contact Me or check out the Producerism Blog

SlotMas3chine (Slot Machine AS3 Tutorial)

05/27/2010


I put together a quick example of a slot machine in AS3 in about 15 minutes, and under 100 lines.  It can still be optimized a great deal, and I will probably make some updates to this in the near future.  One of the requests of the example, was an ability to hold matching reels.  So in this example, once 2 of the reels in the slot machine match, those will not respond to the spin() function anymore.  This will be changed around a bit future updates, to act more like a typical slot machine that does not have this feature.

here it is in action on wonderfl

The explanation (or tutorial) part of this post will be complete soon.  I will also optimize the code in the next couple of posts.  Until then, take a look at the code and try to understand what's going on.

package
{
    import flash.display.Sprite;

    public class SlotMachine extends Sprite {

        private var _credits:uint = 4; // start with $20

        private var _slotReel1:SlotReel;
        private var _slotReel2:SlotReel;
        private var _slotReel3:SlotReel;

        public function SlotMachine()
        {   

            _slotReel1 = new SlotReel();
            _slotReel2 = new SlotReel();
            _slotReel3 = new SlotReel();

            spinReels();
            spinReels();
            spinReels();
            spinReels();
        }

        private function spinReels():void
        {
            if (_credits)
            {
                _credits--; // costs $1 per spin

                _slotReel1.spin();
                _slotReel2.spin();
                _slotReel3.spin();

                // check for matches
                if (_slotReel1.value == _slotReel2.value && _slotReel1.value == _slotReel3.value)
                {
                    trace("win!");
                    _credits += 1000;
                }
                else if (_slotReel1.value == _slotReel2.value)
                {
                    _slotReel1.hold = true;
                    _slotReel2.hold = true;
                }
                else if (_slotReel1.value == _slotReel3.value)
                {
                    _slotReel1.hold = true;
                    _slotReel3.hold = true;
                }
                else if (_slotReel2.value == _slotReel3.value)
                {
                    _slotReel2.hold = true;
                    _slotReel3.hold = true;
                }

                trace(_slotReel1.value + " " + _slotReel2.value + " " + _slotReel3.value+" credits left: "+_credits);
            } else {
                trace("out of credits!");
            }

        }

    }
}

import flash.display.MovieClip;

class SlotReel extends MovieClip {

    private var _numberOfSlotFrames:uint = 12;
    private var _currentSlotFrame:uint;
    public var hold:Boolean;

    public function SlotReel(){ super(); }

    public function spin():void
    {
        if (!hold)
        {
            _currentSlotFrame = Math.round(Math.random() * (_numberOfSlotFrames- 1) + 1);
            gotoAndStop(_currentSlotFrame);
        }
    }

    public function get value():uint { return _currentSlotFrame; }
}

Leave a Reply