Question about adding attributes in actionscript 3

advertisements

i'll put a "#" sign on the part where i ask for clarity! this is the code from this link http://www.emanueleferonato.com/2008/05/02/creation-of-a-matching-game-with-flash-and-as3/

i know this link explains it by detail but i still don't some parts of the code haha

package {

    import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;

    public class color_match extends Sprite {

    private var first_tile:colors;
    private var second_tile:colors;
    private var pause_timer:Timer;
    var colordeck:Array = new Array(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8);
    public function color_match() {
        for (x=1; x<=4; x++) {
            for (y=1; y<=4; y++) {
        var random_card = Math.floor(Math.random()*colordeck.length);
                var tile:colors = new colors();
                                   //why is .col attribute not declared?
                                    //#
                     tile.col = colordeck[random_card];
                colordeck.splice(random_card,1);
                tile.gotoAndStop(9);
                tile.x = (x-1)*82;
                tile.y = (y-1)*82;
                tile.addEventListener(MouseEvent.CLICK,tile_clicked);
                addChild(tile);
            }
        }
    }
    public function tile_clicked(event:MouseEvent) {
                     //what does "as colors" suppose to mean here, can i just omit it?,
                    //does the type of any display obj with event.currentTarget/target
                    //generates a type OBJECT
                    //#
        var clicked:colors = (event.currentTarget as colors);
        if (first_tile == null) {
            first_tile = clicked;
            first_tile.gotoAndStop(clicked.col);
        }
        else if (second_tile == null && first_tile != clicked) {
            second_tile = clicked;
            second_tile.gotoAndStop(clicked.col);
            if (first_tile.col == second_tile.col) {
                pause_timer = new Timer(1000,1);
                pause_timer.addEventListener(TimerEvent.TIMER_COMPLETE,remove_tiles);
                pause_timer.start();
            }
            else {
                pause_timer = new Timer(1000,1);
                pause_timer.addEventListener(TimerEvent.TIMER_COMPLETE,reset_tiles);
                pause_timer.start();
            }
        }
    }
    public function reset_tiles(event:TimerEvent) {
        first_tile.gotoAndStop(9);
        second_tile.gotoAndStop(9);
        first_tile = null;
        second_tile = null;
        pause_timer.removeEventListener(TimerEvent.TIMER_COMPLETE,reset_tiles);
    }
    public function remove_tiles(event:TimerEvent) {
        removeChild(first_tile);
        removeChild(second_tile);
        first_tile = null;
        second_tile = null;
        pause_timer.removeEventListener(TimerEvent.TIMER_COMPLETE,remove_tiles);
    }
}

}


The first question, "why is .col attribute not declared?" is because the object is a dynamic class. What this means is that you can set properties of any name on the object so you can retrieve them later. In this case, it's being used to store a value that is later used in the tile_clicked() function. This could be called anything, such as tile.myvalue = colordeck[random_card]; It's worth noting that using dynamic properties like this is flexible but you will lose any code hinting your IDE may give.

The second question, "what does 'as colors' mean?" is a type cast. event.current target is just a generic object as far as flash knows and trying to assign a generic object to a variable that was declared as a specific type can result in compiler errors. By casting the object (telling the compiler that you know what that object is supposed to be), you eliminate the errors.