How to load external swf and use the child class - actionscript-3

I would like to load external child swf into the parent swf (under same directory).
I did try, the child loaded but the class in it didn't work.
1) How can i load the class of child swf?
2) How can i unload the class of child swf?(becoz there could be many ext. swf with different class)
thx
main.fla
function startLoad(){
var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("game1.swf");
var mLoaderContext:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
mLoader.load(mRequest, mLoaderContext);
}
function onCompleteHandler(loadEvent:Event){
var keyManager:Class = ApplicationDomain.currentDomain.getDefinition("net.keithhair.KeyManager") as Class;
addChild(loadEvent.currentTarget.content);
}
game1.fla
import net.keithhair.KeyManager;
keyManager=new KeyManager(stage);
keyManager.addKey(["a"], doSomething);
function doSomething():void {
//do something
}
Result:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at net.keithhair::KeyManager/removeListeners()
at net.keithhair::KeyManager/addListeners()
at net.keithhair::KeyManager()
at game1_fla::MainTimeline/frame1()

function Constructor(){
if (stage){
onAddedToStage();
} else {
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
}
function onAddedToStage(evt:Event=null):void {
var keyManager:KeyManager;
trace("here!")
keyManager=new KeyManager(stage);
keyManager.addKey(["a"], doSomething
}
Constructor();
The above code solve all my problem.
thank you very much destinier
& senocular

Related

ActionScript 3.0 import swf

i've little bit trouble with imported swf. Problem is that imported swf has movie clip which exported as movie clip class. Code is here:
Imported swf:
class myBooks(){
var myBg:MovieClip = new backGround();
stage.addChild(myObjects);
and HOLDER flash file:
var txt:String = "Project/book2.swf";
var myLoader:Loader = new Loader();
var url:URLRequest = new URLRequest(txt);
myLoader.load(url);
addChild(myLoader);
when i try to run , it shows me error like this:
TypeError: Error #1009 cannot access a property or method of a null object reference at actions::myBooks()
Try the following
class myBooks()
{
public function myBooks():void
{
if(stage)
{
init(null);
}
else
{
addEventListener( Event.ADDED_TO_STAGE,init)
}
}
private function init( e:Event = null )
{
var myBg:MovieClip = new backGround();
stage.addChild(myObjects);
}
}
Hope that help

as3 access variable from parent swf

I'm trying to let the child read its parent's vars and vice versa.
The parent has no problems reading the child's vars, but for some reason the child gets only "undefined" as an answer...(instead of the "456")
Parent script
var mySwf
var masterVar=456
function startLoad() {
var myLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("test1.swf");
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
myLoader.load(mRequest);
}
function onCompleteHandler(loadEvent:Event) {
mySwf=loadEvent.currentTarget.content
addChild(mySwf);
trace(mySwf.slaveVar)//123
}
function onProgressHandler(mProgress:ProgressEvent) {
}
startLoad()
Child(test1.swf) script
var slaveVar=123
trace(Object(parent))//[object Loader]
trace(Object(parent.parent))//[object Object]
trace(Object(parent.parent).masterVar)//undefined
trace(Object(this.parent.parent).masterVar)//undefined
parent.parent.parent is null
and MovieClip(parent.parent) only spits out an error
I have no clue what's wrong...
am I missing something?
Try this:
In child swf:
var theParent:Object;
addEventListener(Event.ADDED_TO_STAGE, onAdded);
function onAdded(e:Event):void
{
theParent = this.parent as Object
trace(theParent.masterVar);
//will work after child swf has been added to the display list of the parent file.
}

AS3 Image Loader - It loads itself (the swf)

My LoaderInfo will return the swf itself rather than the image given via the FileReference and Loader. I had problems debugging it, as LoaderInfo.content returns [Object Main] (My document class). After investigation, I discovered LoaderInfo.content is a swf file, according to contentType.
The problem is, the file reference for the image is correct (It is an image, and it is not the swf).
My code:
private function onAction(e:MouseEvent){
if(e.currentTarget.name == 0){
myFileReference = new FileReference();
myFileReference.browse(getTypes());
myFileReference.addEventListener(Event.SELECT, loadedImage);
myFileReference.addEventListener(Event.COMPLETE, loadImage15);
}
}
private function loadedImage(e:Event){
var imgHolder:ImageHolder = Main.imageHolder;
while(imgHolder.numChildren > 0){
imgHolder.removeChild(imgHolder.getChildAt(0));
}
myFileReference.load();
}
private function loadImage15(e:Event){
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadImg2);
loader.loadBytes(myFileReference.data);
trace(myFileReference.type); // .JPG
}
private function loadImg2(e:Event){
var lInfo:LoaderInfo = (e.target as LoaderInfo); //e.target is indeed LoaderInfo
lInfo.removeEventListener(Event.COMPLETE, loadImg2);
trace(loaderInfo.contentType); //application/x-shockwave-flash
var newSprite:MovieClip = loaderInfo.content as MovieClip;
Main.imageHolde.addChild(newSprite); //Error as you can't addChild Main to Main
}
private function getTypes():Array {
return [new FileFilter("Images","*.jpg;*.jpeg;*.gif;*.png")];
}
EDIT
I originally had a very complicated answer - which was wrong...
You simply have an error in your program:
// here you reference the Loader's contentLoaderInfo
var lInfo:LoaderInfo = (e.target as LoaderInfo);
lInfo.removeEventListener(Event.COMPLETE, loadImg2);
// but from here on out, you reference your parent class' "loaderInfo" property!
trace(loaderInfo.contentType);
var newSprite:MovieClip = loaderInfo.content as MovieClip; // <- this is your Main class!
Main.imageHolde.addChild(newSprite); //Error as you can't addChild Main to Main
Change loaderInfo to lInfo, and you should be fine.
I ask about this issue before :
loading image by Loader.loadBytes(byteArray)
Noone knows :]
If You loadBytes of image with Loader , You will recive :
Loader.content is MovieClip
Loader.content.getChildAt(0) is Bitmap

preloading external .swf with class file as3

so i'm trying to make an external preloader to load my main .swf (loading.swf) file that has a class file named mainLoading.as using this code:
var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loop);
l.contentLoaderInfo.addEventListener(Event.COMPLETE, done);
l.load(new URLRequest("loading.swf"));
var loadingPage:loading = new loading;
function loop (e:ProgressEvent):void{
addChild(loadingPage);
loadingPage.x = stage.stageWidth/2;
loadingPage.y = stage.stageHeight/2;
}
function done (e:Event):void{
removeChild(loadingPage);
addChild(l);
}
so I'm getting an error message saying:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at mainLoading()
I think i'm getting the error message because i am accessing the stage in my mainLoading() class file. I tried adding this to the constructor in my class file but it didn't work:
public function mainLoading () {
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event): void {
initStartUpScene ();
}
my initStartUpScene function just throws the intro scene to the loading.swf
any suggestions?
thanks for your help.
(question) is your mainLoading extends either Sprite or Movieclip ?
EDIT
After reading your comment, I would suggest trying this :
Add a function call inside the swf content in your complete progress handler :
function done (e:Event):void{
removeChild(loadingPage);
addChild(l);
Object(l.content).initMethod();
}
content let you access the methods in your loaded .swf main class (e.g. mainLoading)
And replace the event handling in your mainLoading by :
public function mainLoading () {
//no event handling in constructor
}
public function initMethod():void {
//here you go
init();
}
public function init():void { ... //No more event here
Btw it's not the cleanest way to solve your problem.
If that's the exact message you are getting, then yes, adding the ADDED_TO_STAGE listener should have fixed it. Remember to recompile the "loading.swf" if you make any changes to it (a step I always seem to forget)
Does "loading.swf" run just fine without any errors when running it on it's own (not loading it into the "container" SWF)?
This may be unrelated to the question you asked, but you might get better results and avoid some errors by structuring your code like this:
var loadingPage:loading = new loading;
addChild(loadingPage);
loadingPage.x = stage.stageWidth/2;
loadingPage.y = stage.stageHeight/2;
var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
l.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
l.load(new URLRequest("loading.swf"));
//I renamed this function since I believe "loop" is a reserved keyword.
function onProgress (e:ProgressEvent):void{
//No code needed
}
function onComplete (e:Event):void{
removeChild(loadingPage);
addChild(l);
}
You can remove the "onProgress" function if you won't be needing it as well.
OOOOOOKKKKK, so after about a week of admitting defeat, trying it again, rewriting almost half of my code, trying to convince myself that preloaders are overrated, i finally figured it out (drum roll please):
I had a variable that was referencing the stage being called before my constructor method was called. like this:
private var xScrollPosRight:Number = stage.stageWidth - xScrollPosLeft;
I just changed stage.stageWidth to 750 and it worked.
live and learn.

Access a function inside a loaded .swf file?

Is there a way to call a function inside a loaded SWF file?
Basically, I have a .swf file (A) that loads another .swf file (B)...I would just like to treat B as if it was any other instance added to my class .swf "A"...
Have to recast "Loader" with the name of your .swf file class:
Loaded .swf class:
package src {
import flash.display.MovieClip;
public class LoadedSWF extends MovieClip {
public function LoadedSWF() {
}
public function helloWorld():void
{
trace("hello world from loaded swf!");
}
}
}
Main class:
package src {
import flash.display.Loader;
import flash.net.URLRequest;
import flash.display.MovieClip;
import flash.events.Event;
public class Main extends MovieClip {
private var loader:Loader;
public function Main() {
loadSWF("LoadedSWF.swf")
}
private function loadSWF(url:String):void {
var urlRequest:URLRequest = new URLRequest(url);
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded, false, 0, true);
loader.load(urlRequest);
addChild(loader);
}
private function onLoaded(e:Event):void {
var target:LoadedSWF = e.currentTarget.loader.content as LoadedSWF;
trace(target);
target.helloWorld();
addChild(target);
}
}
}
There are two cases i.e
Child swf(B) calls function of Parent swf(A) or
Parent swf(A) calls function of loaded swf(B)
Firstly, in both cases, You must make sure that loaded swf(B) has been loaded and added to Loader swf(A) using Event.COMPLETE. Then communication between two swf is possible. The Loaded swf is just like any other child.
Here is the sample code for case 2
var mLoader:Loader = new Loader();
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
mLoader.load(new URLRequest("B.swf"));
public function onCompleteHandler(evt:Event)
{
var embedSWF:MovieClip = MovieClip(evt.target.content);
addChild(embedSWF);
embedSWF.function_OF_B();
}
embedSWF.function_OF_B() statement will call the function of child swf B i.e function_OF_B()
In Adobe Flex, you can use the flash.display.Loader class to load another SWF, and add an event listener to it.
This example is taken from the Adobe Documentation:
var url:String = "http://www.helpexamples.com/flash/images/image2.jpg";
var urlRequest:URLRequest = new URLRequest(url);
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);
loader.load(urlRequest);
addChild(loader);
function loader_complete(evt:Event):void {
var target_mc:Loader = evt.currentTarget.loader as Loader;
target_mc.x = (stage.stageWidth - target_mc.width) / 2;
target_mc.y = (stage.stageHeight - target_mc.height) / 2;
}
Because contentLoaderInfo is a subclass of EventDispatcher, you can also dispatch events to the loaded SWF.
This is basically like calling a function from the SWF, just a little more complicated.