Flash AS3 scripts and Tips
 
Migrating in AS3 from AS2 is not that easy, there are lots of changes and codes you need to update when you want to convert your AS2 to AS3.

Referencing to parent movie from child move in AS3

AS2:
				this.parent.gotoAndPlay(1);

AS3:
				MovieClip(this.parent).gotoAndPlay(1);

Calling function in root from child movieclip

AS2:
				_root.functionName( variableName ); 

AS3:
				MovieClip(root).functionName;

Executing external Javascript from Flash

AS3 code:
				import flash.net.URLRequest;

var url:URLRequest = new URLRequest(\"javascript:DisplayMessage()\");
navigateToURL(url, \"_self\");		
External Javascript code:
				function DisplayMessage(){
   alert(\"This is a javascript message\");	
}
Note: the swf file and javascript must be in the same html file

Script inside Movie Object making it as button
				this.buttonMode=true;
this.addEventListener(MouseEvent.CLICK, entersite);

function entersite(event:MouseEvent):void
{
	var url:URLRequest = new URLRequest(\"index.php?page=main\");
	navigateToURL(url, \"_self\");
}


Generating Random number in AS3
				var rnd:Number = Math.floor(Math.random() * (999 -  100)) + 100;



Flash AS3 Preloader Script
Place this code on the frame 1 and put your flash content on the frame 2, you need to put a stop() command on the frame 2 so that the animation will not go back again to frame 1, put a dynamic textfield on the stage within frame 1 and name it with loader_text
				stop();
this.addEventListener(Event.ENTER_FRAME, loading);
function loading(e:Event):void {

	var total:Number = this.stage.loaderInfo.bytesTotal;
	var loaded:Number = this.stage.loaderInfo.bytesLoaded;
	
	loader_txt.text = Math.floor((loaded/total)*100)+ \"% Loading..\";


	if (total == loaded) {
		play();
		this.removeEventListener(Event.ENTER_FRAME, loading);
	}
}


Flash AS3 Dynamic Loading of external Image
On the first frame create an empty movie clip and put instance name imageArea, press F9 and enter the code below
				var imageLoader:Loader;
 
function loadImage(url:String):void {
   imageLoader = new Loader();
   imageLoader.load(new URLRequest(url));
   imageLoader.contentLoaderInfo.addEventListener(
                       ProgressEvent.PROGRESS, imageLoading);
   imageLoader.contentLoaderInfo.addEventListener(
                       Event.COMPLETE, imageLoaded);
}
loadImage(\"Winter.jpg\");
 
function imageLoaded(e:Event):void {
    imageArea.addChild(imageLoader);
}
 
function imageLoading(e:ProgressEvent):void {
      // Use it to get current download progress
}