I have two swf, A.swf and B.swf: B.swf is a child of A.swf. Each swf has its DocumentCla开发者_运维百科ss. Now: I must pass 4 arrays from A.swf to the DocumentClass of B.swf. Which could be the best way? Is a listener in B.swf a good idea? I noticed there's the possibility to send data over the querystring but I would like to avoid this solution, if possible.
EventListeners solution
You have some GlobalEventListener ( http://en.wikipedia.org/wiki/Singleton_pattern ) which is used in both A.swf and B.swf.
You have a customEvent which extends Event and has possibility to send arrays.
package com
{
import flash.events.Event;
public class TransferrArray extends Event
{
public static const TRANSFERRING:String = 'transferring';
private var _array:Array;
public function TransferrArray(type:String, array:Array, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
_array = array;
}
public function get array ():Array
{
return _array;
}
}
}
in B.swf somewhere you putting:
GlobalEventListener.addEventListener ( TransferrArray.TRANSFERRING, handleTransfer )
private function handleTransfer ( e : TransferrArray) : void
{
e.array // <- do what you need with it
}
in A.swf at the point when your Arrays are ready to be transfered:
GlobalEventListener.dispatchEvent ( new TransferrArray ( TransferrArray.TRANSFERRING, [your,arrays,needed,for,b] );
Direct parsing
B.swf need to have ( the main class of B.swf ) some:
public function transferrArrays ( array : Array )
in A.swf after B.swf is loaded and your arrays are ready to be transfered:
BSWFLoader.content["transferrArrays"] ( [your,arrays,needed,for,b] );
I would not advise starting to walk down the path of Singletons. Sooner or later, you will regret it. (http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/)
Instead, make the base class of your child swf implement an interface that has a variable for the value you want to set on it. Once the loader dispatches the complete event, cast your swf to that interface and simply set the variable as normal.
Check out the example here http://flexdiary.blogspot.com/2009/01/example-of-casting-contets-of-swfloader.html. It's a Flex example, but if you click on the thumbnail, then right click on the movie and select "View Source", you will see the source code. The source of the Flash movie that implements the Blusher interface is in the flashSrc folder.
HTH;
Amy
精彩评论