In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

External XML loading example

External XML loading example
Get Adobe Flash player
by flash.technologist 14 Nov 2014
/**
 * Copyright flash.technologist ( http://wonderfl.net/user/flash.technologist )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/5ugl
 */

package
{
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;    
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.net.URLLoader;    
    import flash.net.URLRequest;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFieldType;
    
    public class XMLLoadingTest extends Sprite
    {
        private var urlLabel:TextField;
        private var urlInput:TextField;
        private var resultLabel:TextField;
        private var resultField:TextField;
        private var loadButton:Sprite;
        
        private static const PADDING:Number = 10.0;
        private static const GAP:Number = 2.0;
        private static const BIG_GAP:Number = 8.0;     
        private static const INPUT_HEIGHT:Number = 19.0;
                
        public function XMLLoadingTest()
        {
            addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
        }
        
        private function addedToStageHandler(event:Event):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            
            urlLabel = new TextField();
            urlLabel.text = "URL:";
            urlLabel.autoSize = TextFieldAutoSize.LEFT;
            addChild(urlLabel);
            
            urlInput = new TextField();
            urlInput.border = true;
            urlInput.type = TextFieldType.INPUT;
            urlInput.height = INPUT_HEIGHT;
            addChild(urlInput);
            
            loadButton = new Sprite();
            with (loadButton.graphics)
            {
                beginFill(0x000000);
                moveTo(0, 0);
                lineTo(0, INPUT_HEIGHT);
                lineTo(INPUT_HEIGHT, 0.5 * INPUT_HEIGHT);
                lineTo(0, 0);
                endFill();
            }
            loadButton.buttonMode = true;
            loadButton.addEventListener(MouseEvent.CLICK, loadButton_clickHandler);
            addChild(loadButton);       
            
            resultLabel = new TextField();
            resultLabel.text = "Result:";
            resultLabel.autoSize = TextFieldAutoSize.LEFT;
            addChild(resultLabel);
            
            resultField = new TextField();
            resultField.border = true;
            addChild(resultField);            
            
            refreshStage();
            
            stage.addEventListener(Event.RESIZE, stage_resizeHandler);
        }
        
        private function loadButton_clickHandler(event:MouseEvent):void
        {
            resultField.text = "";
            
            if (urlInput.text != "")
            {
                var request:URLRequest = new URLRequest(urlInput.text);
                var loader:URLLoader = new URLLoader();
                loader.load(request);
            }
            else
            {
                debug("ERROR: empty URL is not allowed!");
            }

        }
        
        private function debug(str:String):void
        {
            resultField.appendText(str + "\n");
        }
        
        private function stage_resizeHandler(event:Event):void
        {
            refreshStage();
        }
        
        private function refreshStage():void
        {
            urlLabel.x = urlLabel.y = PADDING;
            
            urlInput.x = urlLabel.x;
            urlInput.y = urlLabel.y + urlLabel.height + GAP;
            urlInput.width = stage.stageWidth - 2 * PADDING - loadButton.width - BIG_GAP;
            
            loadButton.x = stage.stageWidth - PADDING - loadButton.width;
            loadButton.y = urlInput.y;
            
            resultLabel.x = urlLabel.x;
            resultLabel.y = urlInput.y + urlInput.height + BIG_GAP;
            
            resultField.x = urlLabel.x;
            resultField.y = resultLabel.y + resultLabel.height + GAP;
            resultField.width = stage.stageWidth - 2 * PADDING;
            resultField.height = stage.stageHeight - PADDING - resultField.y;
        }
    }
}