Scope of the Function

by 9re
♥0 | Line 36 | Modified 2009-06-30 14:14:24 | 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/4PL0
 */

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
        */
        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;
                
                //Be careful to the scope of the listeners
                //This code does not work as you may think
                //************************************************
                sp.addEventListener(MouseEvent.MOUSE_OVER, function (e:MouseEvent):void {
                    tf.text = i.toString();
                });
                sp.addEventListener(MouseEvent.MOUSE_OUT, function (e:MouseEvent):void {
                    tf.text = "";
                });
                //************************************************
                
                addChild(sp);
            }
        }
    }
}

Forked