forked from: Scope of the Function

by 9re forked from Scope of the Function (diff: 14)
♥0 | Line 40 | Modified 2009-06-30 14:34:20 | MIT License
play

ActionScript3 source code

/**
 * Copyright 9re ( http://wonderfl.net/user/9re )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/xhvZ
 */

// forked from 9re's Scope of the Function
package {
    import flash.events.MouseEvent;
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFormat;
    public class TestEvaluation extends Sprite {
        /*
        This code is a bad code for the purpose,
        but is a good example for learning scope of the functions
        compare to the original version: http://wonderfl.net/code/21e27da431c30d9908d605f3759bb2856d808125
        */
        public function TestEvaluation () {
            var sp:Sprite;
            var tf:TextField;
            for (var i:int = 0; i < 10; ++i) {
                sp = new Sprite();
                sp.graphics.beginFill(0, 0.7);
                sp.graphics.drawRect(0, 5, 40, 30);
                sp.x = stage.stageWidth / 2 - 20;
                sp.y = 32 + 40 * i;
                tf = new TextField();
                tf.defaultTextFormat = new TextFormat("_sans", 30, 0xffffff, true);
                tf.width = 40;
                tf.height = 36;
                tf.x = 2;
                tf.selectable = false;
                tf.mouseEnabled = false;
                sp.addChild(tf);
                
                sp.buttonMode = true;
                sp.tabEnabled = false;
                
                // save tf and i to the local variables of the closure
                //************************************************
                sp.addEventListener(MouseEvent.MOUSE_OVER, (function ($tf:TextField, $i:int):Function {
                    return function (e:MouseEvent):void {
	                $tf.text = $i.toString();
	            }
                })(tf, i));
                sp.addEventListener(MouseEvent.MOUSE_OUT, (function ($tf:TextField, $i:int):Function {
                    return function (e:MouseEvent):void {
	                $tf.text = "";
	            }
                })(tf, i));
                //************************************************
                
                addChild(sp);
            }
        }
    }
}