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); } } } } Scope of the Function