views:

616

answers:

4

Hi all,

I have main movie A that loads (with a Loader) another movie B inside. In movie A I have a class API, with a bunch of static methods that perform actions in A.

A.as
public class Main extends MovieClip{
 public Main()
 {
   b= new Loader();
   b.load("b.swf");
   addChild(b);
 }
}
-------
API.as
public class API{
   public static function DO_STUFF() { ... }
}
-------
B.fla
API.DO_STUFF();

The fact is that I cannot do this last call, because API class is not available at compile time (but it is on run time).. Could I delay this check to run time? How could I expose API to loaded movies (done by clients)?

I found I can make a interface/bridge function in A, like this:

public function DO_STUFF() { API.DO_STUFF(); }

and then call this from B:

MovieClip(parent.parent).DO_STUFF();

but it seems to be very ugly and unadequated to me.. I'd rather prefer an API.do_stuff() type of call.

any solution? Thanks!

A: 

I've found a simple solution:

Clients must declare API var simply with:

var API;

so I can link this variable with my API class after swf loading is complete:

private function eventCompleteHandler(event:Event):void
{
   event.target.content.API= API;
}

and then clients can call to any API function with API.func();

Diego FG
A: 

You can do this class reference in a most proper way by typing it, like the following:

var API:Class;

And in the complete handler you can safely get it:

import flash.utils.getDefinitionByName;

private function eventCompleteHandler(event:Event):void{
    event.target.content.API = getDefinitionByName("API") as Class;
}
ruyadorno
Thanks, more elegant :)
Diego FG
A: 

The best way to do this is by throwing events. It's not a good idea to call functions directly. Instead, you can listen to an event in the loaded class and dispatch it in the loader.

A.as
public class Main extends MovieClip{
 public Main()
 {
   b= new Loader();
   b.load("b.swf");
   addChild(b);
 }
}
-------
API.as
public class API{
   public function API():void {
      this.addEventListener('DO_STUFF',DO_STUFF);
   }
   public static function DO_STUFF() { ... }
}
-------
B.fla
// instead of API.DO_STUFF();
MovieClip(parent.parent).dispatchEvent(new Event('DO_STUFF'));

Dispatching an event is always free of errors. Either there's someone to listen to it, or it dispatches into nothingness.

evilpenguin
Very interesting. I don't like the syntax in B.fla (ugly for the client to write down), but I like the event approaching. Let's see if I can beautify it.
Diego FG
I'm not very sure where you include the API class.. You could add the event listener in A.as: b.content.addEventListener('DO_STUFF',DO_STUFF); And then the code from the B.fla would be this.dispatchEvent(...) or simply dispatchEvent(...)
evilpenguin
A: 

A.as

package {

import flash.display.Sprite

import flash.display.Loader

import flash.events.Event

import flash.net.URLRequest

public class A extends Sprite
{
 private var api:API;

 public function A():void
 {
  api = new API();

  var loader:Loader = new Loader();
  loader.contentLoaderInfo.sharedEvents.addEventListener("DoStuff", DoStuff);
  loader.contentLoaderInfo.addEventListener(Event.COMPLETE, AddSWF);
  //normally put error listener in here
  loader.load(new URLRequest("B.SWF"));  
 }

 private function AddSWF(e:Event):void
 {
  // No Need to Load to Stage if no Visible Conent - just vars etc
  //this.addChild(e.target.content);
 }

 private function DoStuff(e:Event):void
 {
  trace("Calling DoStuff in API Class");
  api.DoAPIStuff();
 }
}

}

--

API.as (included in A.swf)

package {

public class API {

public function API():void { trace("API Ready"); }

public function DoAPIStuff():void { trace("API is DoingStuff()"); } } }

/////////////////////////////////////////////////////////////////////////////////

B.as (Doc Class of B.swf) {

import flash.display.Sprite

import flash.events.Event

public class B extends Sprite
{
 public function B():void
 {
  trace("B Loaded");
  loaderInfo.sharedEvents.dispatchEvent(new Event("DoStuff", true));
 }
}

}

Sorry about the weird formatting for the package an import lines...