25.06.2012 20:52
Sending data from preloader to loaded SWF [basic]
Continuing the topic on interfaces from the previous post, I’ve mentioned that their usefulness is sometimes irreplaceable and one good example of this is when trying to establish a communication channel with loaded SWF file, most frequently seen in preloaders.
Lets start with a basic preloader:
In such way that external no external user will be able to start it neither automatically, nor manually:
By writing super.play() we bypass our overwrites of play() from the MovieClip class. One last thing to do is to test it out:
(Animation file is located here: MyAnimation.swf - anyone who wants a challenge can try loading it themselves)
Source: PreloaderInterface.zip
Lets start with a basic preloader:
var loadLoader:Loader = new Loader(); loadLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaded); loadLoader.load(new URLRequest("MyAnimation.swf")); function onLoaded(e:Event):void { addChild(loadLoader.content); }In this case the MyAnimation.swf file and its content will be put on the stage when preloader finishes loading it. This is without a doubt the simplest solution, giving us pretty much no control over who and how can open this animation, which is why lets, for example, make it only viewable when preloader passes a correct keyword. To do this, we need to go back the the animation file and make it a document class:
In such way that external no external user will be able to start it neither automatically, nor manually:
public class MyAnimation extends MovieClip { public function MyAnimation() { super.gotoAndStop(1); } public override function gotoAndStop(frame:Object,scene:String=null):void { return; } public override function gotoAndPlay(frame:Object,scene:String=null):void { return; } public override function play():void { return; } }Now even we won’t be able to start it, which is why we will make a backdoor entrance available only to us, by using an Interface of course. Lets make it look like this:
public interface IMyInterface { function playUsingPassword(pass:String):void; }And then implemented in our MyAnimation class:
public class MyAnimation extends MovieClip implements IMyInterface { public function MyAnimation() { super.gotoAndStop(1); } public function playUsingPassword(pass:String):void { if(pass == "MyPassword") super.play(); } public override function gotoAndStop(frame:Object,scene:String=null):void { return; } public override function gotoAndPlay(frame:Object,scene:String=null):void { return; } public override function play():void { return; } }(Don’t forget to add the implements!)
By writing super.play() we bypass our overwrites of play() from the MovieClip class. One last thing to do is to test it out:
var loadLoader:Loader = new Loader(); loadLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaded); loadLoader.load(new URLRequest("MyAnimation.swf")); function onLoaded(e:Event):void { addChild(loadLoader.content); var main:IMyInterface = loadLoader.content as IMyInterface; if(main != null) main.playUsingPassword("MyPassword"); }Resulting in something like this:
(Animation file is located here: MyAnimation.swf - anyone who wants a challenge can try loading it themselves)
Source: PreloaderInterface.zip