0
votes

i want to create kind of slot machine. i googled and found a sample code for that.

import com.greensock.*;
import com.greensock.easing.*;


var blitMask1:BlitMask = new BlitMask( strip1, strip1.x, strip1.y,
                         strip1.width, 100, true, true, 0xffff00, true);    

spin_btn.addEventListener(MouseEvent.CLICK, spin);

function spin(event:MouseEvent):void {    
        spin_btn.visible = false;           
        var newNumber:Number = (randomNumber(0, 9) * 100) + 1200;
        TweenMax.to(strip1, 1, {y:String(newNumber), onComplete:showBtn});
}

function showBtn() {
    spin_btn.visible = true;
}

function randomNumber(min:Number, max:Number):Number {
    return Math.floor(Math.random() * (1 + max - min) + min);
}

There is an object on stage called strip1 that have 10 children.Is there any way to find out which number(child) is on the stage after the virtual slot machine has stopped?

In other words, there is a tween method for an object and I want to find out whats its location after tween.

2

2 Answers

2
votes

I recently developed a slot machine for the sake of it. I'm sure there are quite a few ways to do this but this is the way I did it. This has to be done for each reel.
There are usually three symbols visible inside your blitmask. If your blitmask is one symbol high then this method will give you the visible symbol
First declare a variable to keep track of the centre symbol.
I positioned my strip so that the 3rd symbol was center in the blitmask window, remember this is not the index.
Basically you know where it is going to land so you can find out what the symbol is before it lands.

var REEL1:Number = 3;


function spin(event:MouseEvent):void  
{
    var newNumber:Number = (randomNumber(1, 10)) ;
    //call function to find the symbol to land
    REEL1 = GetReelPosition(REEL1, newNumber);
    //work out the amount to tween. Random number * symbol height + strip length
    //if you want a longer spin add more strip lengths
    newNumber = (newNumber * 100) + 1000;
    var myTimeline:TimelineMax = new TimelineMax();
    myTimeline.append(new TweenLite(Strip1, 2, {y:String(newNumber)}));
//get target symbol as movieclip
var currentTile:MovieClip = Strip1.getChildAt(REEL1-1) as MovieClip;
//if you have export for actionscript, this will tell what the object is
    trace(String(currentTile));
}

When creating your strip, make symbols from your images, export for actionscript then add to your strip from top to bottom. If you change a symbol then you will have to delete and paste from top to bottom again. The index reflects the order the symbols are added.

function GetReelPosition(reel:Number,num:Number):Number
{   
    reel = reel - num;
    switch(reel)
    {
//if symbol is the first number on the strip and random
//number = 1 (1-1) then the centre symbol is the last on the strip
        case 0:
          reel = 10;
          break;
//if symbol is the first number on the strip and random
//number = 10 (1-10) then the centre symbol is the first on the strip
        case 1:
        case -9:
          reel = 1;
          break;
        case 2:
        case -8:
          reel = 2;
          break;
        case 3:
        case -7:
          reel = 3;
          break;
        case 4:
        case -6:
          reel = 4;
          break;
        case 5:
        case -5:
          reel = 5;
          break;
        case 6:
        case -4:
          reel = 6;
          break;
        case 7:
        case -3:
          reel = 7;
          break;
        case 8:
        case -2:
          reel = 8;
          break;
        //case 9, -1:
        case 9:
        case -1:
          reel =  9;
          break;
    }
    return reel;

}

I updated the switch code. case 9, -1: was not reliable. I'm not sure what language I picked that one from.

This was my first flash project and I did this slot machine fairly quickly. I'm sure there are better ways to do this. Have a look at my slot?
http://rcras.com/pokies/funnyfarm

1
votes

Use a class variable to keep track of the currently visible element.

You can use the onCompleteParams argument of the TweenMax to pass the desired number. This would be available in the onComplete Handler. Set the variable when the tween complete (slot machine stops).

///... 

var selectedChildIndex:Number = -1;

function spin(event:MouseEvent):void {    
        spin_btn.visible = false;           
        var newNumber:Number = (randomNumber(0, 9) * 100) + 1200;           
        TweenMax.to(strip1, 1, {y:String(newNumber),
                    onComplete:showBtn,  onCompleteParams:[1]});            
}    

function showBtn(index) {
    spin_btn.visible = true;
    selectedChildIndex = index;
}