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
[FONT=Wingdings]–>[/FONT]Swf Folder:
**[FONT=Wingdings]–[/FONT][FONT=Wingdings]-[/FONT][FONT=Wingdings]–>[/FONT]Site_elements Folder:
**[FONT=Wingdings]–[/FONT][FONT=Wingdings]-[/FONT][FONT=Wingdings]–>[/FONT]Files Stored: background.swf
[FONT=Wingdings]–>[/FONT]AS Folder:
**[FONT=Wingdings]–[/FONT][FONT=Wingdings]-[/FONT][FONT=Wingdings]–>[/FONT]Widgets Folder:
**[FONT=Wingdings]–[/FONT][FONT=Wingdings]-[/FONT]**[FONT=Wingdings]–>[/FONT]**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!