PDA

View Full Version : Dumb Noob Question - loading an external swf



MoonMonkey
September 14th, 2008, 02:04 PM
Hi, I’m a complete noob at AS3 and I’m having a problem with what should be the simplest of things to do. I’m building a full screen browser site with centered content but I can’t manage to get the background swf to display in the main swf due to an error.

Having trawled the web to find answers that haven’t worked, and there have been many, this is the code I’m trying at the moment...

AS3 file...

package widgets
{
import flash.display.Sprite;
import flash.display.*;
import flash.net.URLRequest;

public class ImageLoader extends Sprite
{
public var bck:Sprite = new Sprite();
private var loader:Loader;

public function ImageLoader(bgContainer:Sprite)
{
bgContainer.addChild (bck);
var req:URLRequest = new URLRequest( "swf/site_elements/background.swf" );
loader = new Loader();
loader.load ( req );
init();
}

public function init():void
{
bck.addChild (loader.content);
}

}

}


In the main movie I have an instance of background_mc movieclip onstage with an identifier (bgContainer) which I want to replace with the background.swf, and just a couple of lines of code...

import widgets.ImageLoader;
var setStage:ImageLoader = new ImageLoader(this.bgContainer);


The background swf has just an uninteresting movie onstage that does nothing as yet, this will be replaced.

When I test the main movie I get the following warning...

TypeError: Error #2007: Parameter child must be non-null.at flash.display:: DisplayObjectContainer/addChild()
at widgets::ImageLoader/init()
at widgets::ImageLoader$iinit()
at start_fla::MainTimeline/start_fla::frame1()*/

Please tell me where I’m going wrong!!! Every method I’ve tried of doing this has failed with either warnings or errors. I’ve seen from other posts and websites that some people don’t give enough information when asking questions, so here’s all the info I can muster....

My file structure goes as follows...

Main folder: Where everything’s kept
Files Stored: start.fla/swf
-->Swf Folder:
----->Site_elements Folder:
----->Files Stored: background.swf
-->AS Folder:
----->Widgets Folder:
----->Files Stored: ImageLoader.as

Under Preferences I have the following path set in Actionscript 3 preferences for an added classpath (in case you’re wondering how the start movie is finding the actionscript!).

C:\Documents and Settings\MyName\Desktop\Sample\as

And finally, everything is set up in the attachment except for the AS3 preferences.

My plans for this project are to pass the url to the ImageLoader class from a setBackground class where I can randomly set a background url or colour (yes, by using a random number class). Also the same principles will be used to set the content (again with its own class). And so on etc etc.

I know that there are easier ways of doing this such as not using namespaces etc and this might seem a bit overly object-orientated but that’s exactly what I want, code that I can lift and use at will for other projects. And if I’ve got to learn Actionscript 3 then I may as well do it properly!!!

Please help, I’m losing sleep over this!

jwopitz
September 14th, 2008, 04:46 PM
good lord there is alot to read here.

I didn't make it all the way down the post but it sounds like maybe the loaded SWF is not completely loaded. Listen for the Event.COMPLETE event on a SWFLoader, then once it is loaded, try adding it to the stage from there. It sounds more like a timing issue.

MoonMonkey
September 15th, 2008, 06:45 AM
Thanks for the quick reply, I had a go at it and now I get no errors and no warnings but the background.swf isn't loading either!

here's the code...


package widgets
{
import flash.display.Sprite;
import flash.display.*;
import flash.net.URLRequest;
import flash.events.Event;

public class ImageLoader extends Sprite
{
public var bck:Sprite = new Sprite();
private var loader:Loader;

public function ImageLoader (bgContainer:Sprite)
{
bgContainer.addChild (bck);
var req:URLRequest = new URLRequest("swf/site_elements/background.swf");
loader = new Loader();
loader.contentLoaderInfo.addEventListener (Event.COMPLETE, init);
loader.load (req);
//init();
}
public function init (event:Event = null):void
{
addChild (loader.content);
}
}
}


This spawns some more questions...

1. Is there a problem with the way I'm passing the onstage mc as a parameter?
2. Should I be using stage properties (or Stage!)?
3. Will I ever get the hang of this?

Cheers

jwopitz
September 15th, 2008, 11:27 AM
I have to add one correction to my previous post. I mentioned SWFLoader however that is a Flex-only class. Sorry about that, I was in Flex mode when I answered this.

Firstly, this look right at first glance, however I think it may be a bit over-complicated. Try adding this logic somewhere in your timeline and see if you can simplify the display logic here. Remember that a Loader is a display object, so having that somewhere on the stage timeline should ensure that your added SWF isn't getting lost somewhere in the display chain. Check out the example at the bottom of this page - http://livedocs.adobe.com/flex/3/langref/flash/display/Loader.html#includeExamplesSummary

Also check out the comments at the bottom of that page. Seems like there may be some confusion on the event types.

Hope that helps.

Alex Lexcuk
September 15th, 2008, 03:37 PM
look my small example
Home swf
http://www.dnadillo.dn.ua/fla/House_mc.swf
Load it to
http://www.dnadillo.dn.ua/fla/Receiver_mc.swf

http://www.dnadillo.dn.ua/fla/house-receiver-mc.zip

MoonMonkey
September 16th, 2008, 06:48 AM
I got directed to a tutorial here http://www.calypso88.com/?p=34 which I managed to get working for bitmaps. When I get time I'll start messing with it using Alex's excellent example for reference (for the movieclip requirement)

Jwopitz: Don't worry about the swfLoader thing, I didn't use it anyhoo! And many thanks for the link, I've bookmarked it cos it's bound to come in handy! Thanks for your help up to now, too!

Alex: Many thanks for your example, I haven't had much chance to look at it yet as work keeps getting in the way of what I actually want to do! But as mentioned above I'll be delving into the code when I get a chance. I'll check back and let you all know how I get on!

Thank you both very muchly!:hugegrin:

neznein9
September 16th, 2008, 06:05 PM
I got directed to a tutorial here http://www.calypso88.com/?p=34 which I managed to get working for bitmaps. When I get time I'll start messing with it using Alex's excellent example for reference (for the movieclip requirement)

Hey MoonMonkey - I'm not sure exactly what you're aiming for, but just last night I posted a second class similar to Pic that's meant for loading swf files. Hope it helps:

http://www.calypso88.com/?p=41

MoonMonkey
September 17th, 2008, 03:23 AM
Fantastic, thanks a lot neznein9, I owe ya a pint!!! I'll be having another go at the weekend as work gets in the way until then!

I'll check back with the results!!!

Cheers everybody, you've given me hope at the point of giving up the oo way of doing things and slapping everything into the same swf!!