Tag Archive for URLRequest

AS3 Load External Text File

package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class Main extends MovieClip
	{
		private var _txtFileToLoad:String = "myTextFile.txt";
		private var _txtLoader:URLLoader;
		private var _loadedTxt:String;
		

		public function Main():void
		{
			loadExternalTxtFile();
		}
		
		private function loadExternalTxtFile():void
		{
			_txtLoader = new URLLoader();
			_txtLoader.addEventListener(Event.COMPLETE, onTxtLoader_COMPLETE);
			_txtLoader.addEventListener(IOErrorEvent.IO_ERROR, onTxtLoader_IO_ERROR);
			_txtLoader.load(new URLRequest(_txtFileToLoad));
		}
		
		private function onTxtLoader_COMPLETE(event:Event):void {
			_txtLoader.removeEventListener(Event.COMPLETE, onTxtLoader_COMPLETE);
			_txtLoader.removeEventListener(IOErrorEvent.IO_ERROR, onTxtLoader_IO_ERROR);
			_loadedTxt = String(event.target.data);
			trace(_loadedTxt);
		}
		
		private function onTxtLoader_IO_ERROR(event:Event):void {
			_txtLoader.removeEventListener(Event.COMPLETE, onTxtLoader_COMPLETE);
			_txtLoader.removeEventListener(IOErrorEvent.IO_ERROR, onTxtLoader_IO_ERROR);
			trace(event.target.text);
		}

	}
}

source

AS3 Loading and Playing an External MP3 File

import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;

var snd:Sound = new Sound();
var channel:SoundChannel = new SoundChannel();
snd.load(new URLRequest("song.mp3"));
snd.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
function onComplete(evt:Event):void {
	channel = snd.play();
}

source

Link URL Manager

hitBox.addEventListener (MouseEvent.MOUSE_DOWN, linkDown);
hitBox.xLink = "http://www.google.com"

private function linkDown (evt:MouseEvent):void
{
var url:String = evt.target.xLink;
var req:URLRequest = new URLRequest (url);

try
{
navigateToURL (req, "_blank");
} catch (evt:Error)
{
trace("Error occurred!");
}
}

source

Image Loader

loadImage("myImage.jpg",holder1);


function loadImage(clip:String, holder:MovieClip):void
{
removeChildren(holder);

var imageLoader:Loader = new Loader();
var imageURL:String = clip
var imageURLReq:URLRequest = new URLRequest(imageURL);

holder.addChild(imageLoader);
trace("loading "+imageURL);

imageLoader.load(imageURLReq);
imageLoader.contentLoaderInfo.addEventListener(Event.INIT, imageLoaded);
imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imageProgress);
}

function imageProgress (evt:ProgressEvent):void
{
//trace(Math.round((evt.bytesLoaded / evt.bytesTotal)*100));
}

function imageLoaded(evt:Event):void
{
//var targetLoader:Loader = Loader(evt.target.loader);
//trace("complete");
}

function removeChildren (container:MovieClip):void
{
while (container.numChildren)
{
container.removeChildAt (0);
}
}

source

Load Movie (URLRequest)

import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.ProgressEvent;

private function loadFile (path:String):void
{
var myLoader:Loader = new Loader ();
var myRequest:URLRequest = new URLRequest (path)
myLoader.load (myRequest);

myLoader.contentLoaderInfo.addEventListener (ProgressEvent.PROGRESS, loadProgress);
myLoader.contentLoaderInfo.addEventListener (Event.COMPLETE, loadComplete);
}

private function loadProgress (evt:ProgressEvent):void
{
var amountLoaded:Number = (evt.bytesLoaded / evt.bytesTotal)*100;
trace ("Loading: % " + amountLoaded);
}

private function loadComplete (evt:Event):void
{
trace ("Complete - " + evt.target.content);
}

//loadFile ("main.swf");

source

comunicazione con un Web Server

var variables:URLVariables = new URLVariables();
variables.miavariabile = "Ciao";
var request:URLRequest = new URLRequest();
request.url = "http://www.miodominio.com/miapagina.php";
request.method = URLRequestMethod.POST;
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, completeHandler);
try {
loader.load(request);
} catch (error:Error) {
trace("Errore nel caricamento dell' URL");
}
function completeHandler(event:Event):void {
var x_xml:XML = XML(event.target.data);
trace(x_xml);
}

source