Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 11-01-2009, 03:30 PM   #1
slalonde44
Member
 
Join Date: Feb 2007
Posts: 98
Default loading external swf - works from document clas, but not from a different class

Hello.

This is simple, however I am having trouble. I am adding a swf to the stage in my Document class (Test.as) - it works. I am trying to add the same swf to the stage in a different class called Control.as, it doesn't work. Can someone please help and explain what I am doing wrong?

Thanks,
Steve

// Document class Test.as
// Adding bg.swf to the stage - this works....

package ca.blah (

// Import Flash Stuff
import flash.display.*;
import flash.events.*;
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.*;
import flash.net.URLRequest;

public class Test extends MovieClip {

private var bgRequest:URLRequest;
private var bgLoader:Loader;
private var bgMc:MovieClip;

public function Test():void {
var loader:Loader = new Loader();
var url:URLRequest = new URLRequest("bg.swf");
loader.load(url);
this.addChild(loader);
}
}
}



// Control class Control.as
// Adding bg.swf to the stage - this does not work.... What am I doing wrong here compared to with the Document class Test.as

package ca.blah.display (

// Import Flash Stuff
import flash.display.*;
import flash.events.*;
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.*;
import flash.net.URLRequest;

public class Control extends MovieClip {

private var bgRequest:URLRequest;
private var bgLoader:Loader;
private var bgMc:MovieClip;

public function Control():void {
bgLoad();
}

public function bgLoad():void {
var loader:Loader = new Loader();
var url:URLRequest = new URLRequest("bg.swf");
loader.load(url);
this.addChild(loader);
}
}
slalonde44 is offline   Reply With Quote
Old 11-01-2009, 03:50 PM   #2
Mazoonist
Site Contributor
 
Mazoonist's Avatar
 
Join Date: Jun 2006
Posts: 3,160
Send a message via AIM to Mazoonist
Default

When you use a document class, it is already added to the display list for you. So the problem is that while your Control class adds the instance of loader to its display list, nothing else ever adds your instance of the Control class to the main display list (that is, the stage).

Every display object container (of which MovieClip is one) can contain other display objects on its display list. But if that display object container is not itself on a display list that ultimately connects to the stage's display list, it is not displayed.
__________________
My Tutorials * My Website
Mazoonist is offline   Reply With Quote
Old 11-01-2009, 04:48 PM   #3
slalonde44
Member
 
Join Date: Feb 2007
Posts: 98
Default

Thanks Mazoonist for your explaination.

Can you show some code on how to add an instance of my Control class to the main display list?

Would I add this code below to the document class, something like:

var test:Control = new Control();
slalonde44 is offline   Reply With Quote
Old 11-02-2009, 08:30 AM   #4
henke37
Senior Member
 
henke37's Avatar
 
Join Date: Mar 2009
Location: Sweden
Posts: 10,886
Send a message via Skype™ to henke37
Default

That's a good start, but not enough. While you do indeed need an instance of the class, you also need to actually put the instance on a display list.
henke37 is offline   Reply With Quote
Old 11-02-2009, 12:37 PM   #5
Mazoonist
Site Contributor
 
Mazoonist's Avatar
 
Join Date: Jun 2006
Posts: 3,160
Send a message via AIM to Mazoonist
Default

Your document class will make an instance of your Control class:
ActionScript Code:
var control:Control = new Control();
(Note: use a variable name that makes sense, like control, not "test." One good reason for always using initial capital letters for Class names is that the lowercase version is then available for a variable name. Example Loader, loader).
Then it will add it to the display list:
ActionScript Code:
addChild(control);
In short, a document class represents the main timeline of your fla file. In a brand new fla file, your main timeline (aka the root) has already been added to the stage for you, behind the scenes. So anything that is added to the root is also at the same time being added to the stage. Everything must ultimately connect to the stage (speaking in a display hierarchy sense) or it is not displayed.
__________________
My Tutorials * My Website
Mazoonist is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:07 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.