PDA

View Full Version : How to pass XML from preloader to main.swf?


janisiv
11-22-2009, 06:06 PM
Hi everyone,

I have been trying to figure out the following:

main.swf - contains my photo gallery application
preloader.swf - loads the main.swf file and on COMPLETE event displays it

At the moment the gallery list is in the XML file and the XML file is loaded from the main.swf file.

However, as the XML file will grow, I want to preload the XML file along with main.swf file in the preloader.swf and then pass the XML data to the main.swf file so that main.swf file can use this XML data and can create the galleries from there.

What would be the best way to accomplish this in AS3? I am using AS3 Project in Flex Builder 3.

Thanks,
janis

janisiv
11-22-2009, 06:08 PM
Hi everyone,

I have been trying to figure out the following:

main.swf - contains my photo gallery application
preloader.swf - loads the main.swf file and on COMPLETE event displays it

At the moment the gallery list is in the XML file and the XML file is loaded from the main.swf file.

However, as the XML file will grow, I want to preload the XML file along with main.swf file in the preloader.swf and then pass the XML data to the main.swf file so that main.swf file can use this XML data and can create the galleries from there.

What would be the best way to accomplish this in AS3? I am using AS3 Project in Flex Builder 3.

Thanks,
janis

Barna Biro
11-22-2009, 07:10 PM
The project type doesn't really matter, choose which would suit your needs the best way. Regarding the problem: there is more than one solution for what you want, I for one would do something like:

1) Preloader the XML file.
2) Start preloading the main.swf by passing in the loaded XML file as a parameter.
3) Once the main is loaded, it can access the passed parameter ( the XML object ) and do whatever it wants with it.

This would be the easiest solution that comes into my mind at the moment. All you need to check out is how to pass parameters to .swf files and that's kinda it ( you can build an URLVariables object and pass it in the request when preloading the main.swf file ).

Best wishes,
Barni

janisiv
11-22-2009, 07:51 PM
Hi Barni,

Thanks for the info. I tried to do what you suggest before, but it does not work for some reason.
I use the following code in the preloader to show or start my main application:

l2lClass = l2lSWFloader.contentLoaderInfo.applicationDomain.g etDefinition(l2lClassName) as Class;
l2lObject = new l2lClass();
l2lInstance = new l2lClass() as DisplayObject;
addChild(l2lInstance);

So basically as you can see, once i have completely loaded the main.swf file, i instantiate it and add it to the stage.

However, when i tried to pass a variable to the main.swf it would not work. it would say expexted 1 argument but supplied 0, even though i passed it.

cheers,
janis

Barna Biro
11-22-2009, 08:05 PM
Hi again,

Well, from my point of view, what you are doing is absolutely senseless. You don't have to instantiate anything that way and I honestly can't see any reason why you'd ever want to do something like that ( maybe you can explain this a bit and shed some ligth ). Assuming you are using a Loader or SWFLoader to load your main.swf file, then simply passing in parameters to the request that will load the main.swf file should be enough. Those parameters will automatically be passed down to the conent ( being the main.swf file ) and you can read them directly in the main.

Both the SWFLoader and Loader automatically add and play the loaded content ( the SWFLoader also has properties that can stop this auto-play behavior ) so there is absolutely no need to dig down into the loaded content, try to get the content's definition and who knows what, and instantiate it ( I seriously have no idea why are you doing that ).

However, when i tried to pass a variable to the main.swf it would not work. it would say expexted 1 argument but supplied 0, even though i passed it.
Maybe if you can post some code on this then we might find what the problem was and why didn't the passing work. By the way, are both your .swf files pure AS 3.0 or are they both usign the Flex framework or is it a mix?

I could try throwing together a fast example if you want.

Cheers,
Barni

janisiv
11-22-2009, 08:24 PM
Hi,

preloader and main swf are both pure AS3 built using Flex Builder 3 framework just by creating an AS3 Project.

A while ago i was searching and searching on how to create a simple AS3 preloader in Flex 3 but as AS3 project without Flex 3 builtin components and I came across one tutorial which explained doing this way and it worked for me, therefore, i stuck with this method.

But as you mentioned, i load my main.swf file using a Loader

mainLoader.load(new URLRequest(swfFile), myLoaderContext);

where would i pass the XML data to the loader?

thanks,
janis

Barna Biro
11-22-2009, 08:33 PM
While waiting for a reply, I took the time to throw together a fast example ( I have also commented it to make it as clear as possible ).

The preloader code ( since this was fast example, I didn't bother reading an xml structure from an external source... but that would be also a piece of cake. The only difference would be that you would first load the xml structure, store it, create the variables, request and only then start loading the main.swf file ):

package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.net.URLRequest;
import flash.net.URLVariables;

[SWF(backgroundColor="#FFFFFF", frameRate="31")]

/**
* Example preloader class that passed an xml structure
* to the target content.
*
* @author Barna Biro
*
*/
public class PreloaderSwf extends Sprite
{
/**
* Example xml structure.
*/
protected var _xmlData:XML = <root>
<name>Joe 'The Bull' Black</name>
<age>Confidential</age>
<sex>Yesterday</sex>
<location>Anywhere you want</location>
</root>;

// ----------------------------------------
// Constructor
// ----------------------------------------

/**
* Class constructor.
*/
public function PreloaderSwf()
{
// Movie properties.
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.showDefaultContextMenu = false;

// Create the variables.
var variables:URLVariables = new URLVariables();
variables.xmlData = _xmlData;

// Create the request.
var request:URLRequest = new URLRequest();
request.data = variables;
request.url = "main/MainSwf.swf";

// Create the loader and load the main swf file.
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.CO MPLETE, onLoadCompleteHandler);
loader.load(request);
};

// ----------------------------------------
// Event handling
// ----------------------------------------

/**
* Called once the external file has been successfully loaded.
*
* @param event The event object.
*/
protected function onLoadCompleteHandler(event:Event):void
{
// Add the loaded content to the display list.
this.addChild(event.currentTarget.content);
};
};
};
Here's the main.swf file's source:

package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;

[SWF(backgroundColor="#FFFFFF", frameRate="31")]

/**
* Example SWF file that will receive some parameters from
* the parent SWF file and it will display the values.
*
* @author Barna Biro
*
*/
public class MainSwf extends Sprite
{
// ----------------------------------------
// Constructor
// ----------------------------------------

/**
* Class constructor.
*/
public function MainSwf()
{
// Store the passed parameter.
var xmlData:XML = XML(this.loaderInfo.parameters.xmlData);

if (xmlData)
{
// Create a text field that will display the received parameter value.
var textField:TextField = new TextField();
textField.autoSize = TextFieldAutoSize.LEFT;
textField.selectable = false;
textField.text = xmlData.toXMLString();

// Add the text field to the display list.
this.addChild(textField);
};
};
};
};
I'll also attach the source files ( just in case you run into problems ).

Best wishes,
Barni

janisiv
11-22-2009, 08:45 PM
Hi Barni,

Thanks for the code and example. It works like a charm.

I just checked out the documentation for URLVariables() and that is exactly what i needed to be able to pass now any variables to the main.swf file.

Thanks a lot.

The problem is that i am just learning AS3, and i try to figure out how to do things as i go along and then implement them in my code.

I checked out the Loader documentation as well, and i can just directly add it to the Display List without creating extra classes and objects and then instantitate them.

Thanks for your help.

Cheers,
Janis

Barna Biro
11-22-2009, 08:58 PM
Don't worry, you'll get used to the language in no time. ;) Just make sure that you don't jump into coding without first understanding how the tools/classes/whatever work and how those things were intended to be used ( meaning: read the documentation always first, read articles, stuff you can find on the net and in existing threads - this strategy will be a real time saver and also a more efficient way of learning on the long run ).

Cheers,
Barni