Skip to main content
Known Participant
September 12, 2008
Question

Accessing resources in loaded/external SWFs

  • September 12, 2008
  • 6 replies
  • 2685 views
I've been looking around for an effective way of solving this issue, but without much like. What I'm trying to accomplish is creating a container for my newest Flash site in main.swf and loading other swf files that provide resources that modify mcs, functions etc. of the main.swf. I plan to have main.swf hold my background, logo etc. then each subrelated page as contact.swf, index.swf to be placed on top of that swf.

I did find this thread: http://www.kirupa.com/forum/archive/index.php/t-241538.html but all I managed to get from it is how to reference elements in the child from the parent using Loader.content. What I'm trying to figure out is how to reference from the child to the parent. ie. if I want to modify the background of the main.swf which is contained in a function called changeBackground().

To add some more complexity I am trying to keep as much of my code in .as files that are placed in the Document class. So for each main.fla I have a main.as and index.fla is index.as. In my index.as script I need to determine how to access objects/functions contained in main.as.

I HOPE that I've explained that all properly. If I'm being confusing or using inappropriate terms - apologies.
This topic has been closed for replies.

6 replies

Participating Frequently
September 16, 2008
"So you're suggesting re-working how I'm thinking about the entire site?"

Hard to say. You have a few options. Which option you choose depends on many factors. The second solution with type casting the parent.parent as a dynamically typed Object may be the simplest and quickest solution for you, however, it does have several disadvantages discussed earlier in this post. You definitely should become familiar with events as they are essential in AS, but maybe this is not a situation where you want to use them. Again, it's really hard to say without knowing more about the architecture and context.
Participating Frequently
September 13, 2008
Import does compile the source code into your swf, but it doesn't just copy it like include does. If you have the files in the same directory (and refer to each other's class) you are going to compile the source code anyway. Just like you are compiling the Tween classes into your indexPage class already. This should not be seen as a problem as the file size will be negligible and you will need to import classes.

What is the compiler error when you try the other way?
Known Participant
September 13, 2008
It still asks me about parent.parent not being inside the DisplayContainer or something like that despite my checking that it's null. Another question I have is it may not be worth it because I wont be able to trigger or access public variables in the parent swf file right? For example will I be able to do something like parent.parent.changeBackground(). I don't understand scoping or the OO elements yet as I've never had to do something like this. It's mind-boggling that it's not easier than this.

My thoughts on doing this were that it is relatively easy to contain "modularised" pieces of functions/codes in sub-SWF files that can be imported to effect functions contained in a parent SWF. What seems to be clear though is that each SWF contains declarations and logics from the docs (thus indexPage contains logics that icMain already contains. This is basically what I'm doing with my Tweener classes - yeah).

Obviously I'm super confused. heh. :-/
Participating Frequently
September 13, 2008
In either approach, you will "be able to trigger or access public variables in the parent swf file"

This is also true: "each SWF contains declarations and logics from the docs (thus indexPage contains logics that icMain already contains."

I would be curious to compare the file sizes side by side. In any case, there is good way to do this and limit file size redundancies but you would need to understand some OOP concepts like interfaces.

In the other approach (from the link), you would avoid duplicate logic in both swfs. This approach is the easiest and may be the most practical for your application, but it is also more error prone. There may also possibly be a performance hit.

It may be good to get that to work first though. What is the exact error message (cut and paste) that the compiler gives you. And could you post the code?
Participating Frequently
September 13, 2008
There is no file size difference between using import and having the files in the same directory.

If you are moving your source files to a proper directory structure, I would say try the OOP way since you will already be importing other source files.

Another thing I wanted to mention is that I noticed that your class names didn't conform to the standard in these ways:
1. The name of the class did not match the name of the .as file
2. The name of the class did not start with a Capital letter

Maybe that was just the example you showed me, but if it isn't I would fix those things sooner than later.

Let me know if you need help importing the files.

Known Participant
September 13, 2008
Hi there,

Wow thank you very much for all this. I'll definitely give it a go later on tonight. I have 4 other things I need to finish right now :-/ ack! Though hold on, your response was curious not sure if I explained it correctly. Whenever I import an as file into my indexPage it WILL NOT compile code from that source file into the new page thus making it a larger footprint. We're simply importing it to satisfy the compilers need to declare the type.

Also regarding the parent.parent reference issue. Mind helping poor me explain why it's reading compile errors as well. The person in the other thread I presented seemed to have got it working...strange. I might want to use that method in the future, so it would be useful if I understand what's going on there as well.

Thanks!!!

David
Participating Frequently
September 12, 2008
Note that icMain.as and indexPage.as need to be in the same directory or you will need to import them.
Known Participant
September 13, 2008
Hey there,

Yeah I noticed that I needed to add it loadComplete AFTER I posted the code :-/ I added it all correctly I believe but I'm getting compile errors:

On icMain.as - 1046: Type was not found or was not a compile-time constant: indexPage.
On index.as - 1046: Type was not found or was not a compile-time constant: icMain.

I thought I could just ignore them but the swf doesn't run properly :-/

Note that I was getting this error too: 1114: The public attribute can only be used inside a package. so I moved the function outside the construct function right after the package declaration. Here's what I got for index.as

package {

import flash.display.*;
import caurina.transitions.Tweener;
import caurina.transitions.properties.CurveModifiers;

public var _icMain:icMain;

public class indexPage extends Sprite
{

public function set mainSWF(mainSWF:icMain):void {
_mainSWF = mainSWF;
}

public function indexPage():void
{



}
}
}

As for having both .as in the same folder, erm...what do you mean I have to import them? My index.fla and index.as are in an index/ folder...right now.
Participating Frequently
September 13, 2008
Both your main.fla and index.fla and all the .as files that go with them need to be in the same directory OR you need to use the import statement in your .as files to reference them (basically, tell the compiler where they are). Are main.fla and index.fla in the same directory?
Known Participant
September 12, 2008
Heh, my puny brain can't handle this. Let me know if I've done this correctly in my example cause I'm having trouble doing it in my own code:

Here is the structure of icMain.swf (my preloader/base template):

package {
public class icMain extends Sprite
{
public function icMain():void
{
var indexRequest:URLRequest = new URLRequest("index/index.swf");
var indexLoader:Loader = new Loader();
indexLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
indexLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);

var indexPage:indexPage = indexLoader.content as indexPage;
indexPage.icMain = this;
indexLoader.load(indexRequest);
}
}
}

Here is my loaded content page, otherwise in this example index.swf (indexPage class name):

package {
public class indexPage extends Sprite
{

private var _icMain:MainSWFDocumentClass;

public function indexPage():void
{

public function set mainSWF(icMain:icMain):void {
_icMain = icMain;
}
}
}
}
Participating Frequently
September 12, 2008
First, you need a loadComplete function in your icMain class to get called after the swf is loaded. You set up that relationship already in this line:
indexLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);

so you need this:
private function loadComplete(event:Event):void
{
}

then, move these functions into the loadComplete function as you have to wait until the swf is loaded before setting the reference:
var indexPage:indexPage = indexLoader.content as indexPage;
indexPage.icMain = this;


In your indexPage class, you need to move your set mainSWF function outside of the indexPage function. Also, the type of object you are passing, according to your code, should be icMain.

So, instead of this:

public function indexPage():void
{
public function set mainSWF(icMain:MainSWFDocumentClass):void {
_icMain = icMain;
}
}

Do this (notice I changed the name of your variable to avoid name conflict. using the convention of starting class names with a Capital letter would help too):

private var _mainSWF:icMain;

public function indexPage():void
{
}
public function set mainSWF(mainSWF:icMain):void {
_mainSWF = mainSWF;
}


Here is the rewritten icMain class:

package
{
public class icMain extends Sprite
{
public function icMain():void
{
var indexRequest:URLRequest = new URLRequest("index/index.swf");
var indexLoader:Loader = new Loader();
indexLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
indexLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
indexLoader.load(indexRequest);
}

private function loadComplete(event:Event):void
{
var indexPage:indexPage = indexLoader.content as indexPage;
indexPage.icMain = this;
}
}
}

And here is your rewritten indexPage class:

package {
public class indexPage extends Sprite
{
private var _mainSWF:icMain;

public function indexPage():void
{
}
public function set mainSWF(mainSWF:icMain):void {
_mainSWF = mainSWF;
}
}
}
Participating Frequently
September 12, 2008
On the loaded swf, set a reference to your main.swf. This can be done by adding a setter in the Document class for your loaded swf.

Class LoadedSWF extends MovieClip
{
private var _mainswf:MainSWFDocumentClass;
public function set mainSWF(mainswf:MainSWFDocumentClass):void
{
_mainswf = mainswf;
// elsewhere in your code you can now do
//_mainswf.changeBackground();
}
}

In your main swf:

var loadedswf:LoadedSWF = Loader.content as LoadedSWF;
loadedswf.mainSWF = this;