Wednesday, March 10, 2010

ActionScript3: EventFlow

Guys welcome back....
The below code will illustrate, how EVENT-FLOW mechanism works in AS3.


Just make two files naming -
 EventFlow.as and
 Button.as


Copy-paste the below written code, execute the EventFlow.swf, and watch the trace() results in debugger. You will surely get an-practical idea, how the event-flow works in ActionScript3.

EventFlow.as
package
{
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.events.MouseEvent;
    import flash.display.DisplayObject;
   
    public class EventFlow extends Sprite
    {
            private var uiContainer:Sprite;
            private var buttonContainer:Sprite;
            private var uiLabel:TextField;
            private var stopButton:Button;
            private var playButton:Button;
            private var pauseButton:Button;
           
            public function EventFlow () {
                uiContainer = new Sprite();
                uiContainer.name = "uiContainer";
                addChild(uiContainer);
               
                buttonContainer = new Sprite();
                 buttonContainer.graphics.beginFill(0x666666);
                 buttonContainer.graphics.drawRect(0, 0, 420, 50);
                 buttonContainer.name = "buttonContainer";
                 buttonContainer.y = 20;
                 uiContainer.addChild(buttonContainer);
               
                uiLabel = new TextField();
                uiLabel.name = "uiLabel";
                uiLabel.text = "Audio Controls";
                uiLabel.width = 80;
                uiLabel.height = 15;
                uiContainer.addChild(uiLabel);
               
                stopButton = new Button("Stop");
                stopButton.x = 10;
                stopButton.y = 10;
                buttonContainer.addChild(stopButton);
               
                playButton = new Button("Play");
                playButton.x = 160;
                playButton.y = 10;
                buttonContainer.addChild(playButton);
               
                pauseButton = new Button("Pause");
                pauseButton.x = 310;
                pauseButton.y = 10;
                buttonContainer.addChild(pauseButton);
               
                uiContainer.addEventListener(MouseEvent.CLICK, onClick);
            }
       
            private function onClick(event:MouseEvent):void {
                trace ("Click Received");
                trace ("Event Target: ", DisplayObject(event.target).name);
                trace ("Current Target: ", DisplayObject(event.currentTarget).name);
            }
           
            public override function toString():String {
                return "";
            }
    }
}



Button.as
package
{
    import flash.display.Sprite;
    import flash.text.TextField;
   
    public class Button extends Sprite
    {
        private var labelField:TextField;
       
        public function Button (label:String = "button") {
            graphics.beginFill(0x3366CC);
            graphics.drawRect(0, 0, 100, 30);
           
            labelField = new TextField();
            labelField.mouseEnabled = false;
            labelField.selectable = false;
            labelField.text = label;
            labelField.x = 10;
            labelField.y = 10;
            labelField.width = 80;
            labelField.height = 20;
            addChild(labelField);
        }
    }
}

No comments: