PDA

View Full Version : Help with document class!


drummer_scum
09-01-2008, 12:52 AM
Hi guys, i have 2 swf files, Loader.swf & Shell.swf. They both have a document class each...

Loader.swf's document class "PreLoader"

package com.KB
{
import flash.display.*;
import flash.events.*;
import flash.filters.*;
import flash.media.*;
import flash.net.*;
import flash.text.*;
import flash.utils.*;

public class PreLoader extends MovieClip
{
public var TEST_TEXT:String;
private var SWF_LOADER:Loader;
private var SWF_PATH:URLRequest;

public function PreLoader()
{
TEST_TEXT = "hello there";
SWF_PATH = new URLRequest("Shell.swf");
SWF_LOADER = new Loader();
SWF_LOADER.contentLoaderInfo.addEventListener(Even t.COMPLETE,LOAD_COMPLETE);
SWF_LOADER.load(SWF_PATH);
}

private function LOAD_COMPLETE(e:Event):void
{
trace("load complete");
addChild(SWF_LOADER);
}
}
}

and here's Shell.swf's document class "Shell"

package com.KB
{
import flash.display.*;
import flash.events.*;
import flash.filters.*;
import flash.media.*;
import flash.net.*;
import flash.text.*;
import flash.utils.*;

public class Shell extends MovieClip
{
public function Shell()
{
test();
}

public function test():void
{
try
{
trace(PreLoader(this.parent.parent).TEST_TEXT);
}
catch(err:Error)
{
trace("there was an error");
}
}
}
}

the whole idea is PreLoader loads shell.swf and adds it to the stage and i want to be able to refer a variable or call a function from shell.swf document class to the PreLoader document class. Its easy to do this in a keyframe on the main timeline but using a document class is a little different, help please!

Mazoonist
09-01-2008, 02:08 AM
The trouble is that you have to listen for the ADDED_TO_STAGE event, in order to detect when the instance of Shell has been added with addChild. Until that moment, the reference to "parent.parent" is invalid, as there is no parent/child relationship outside of the display list.

Change Shell.as to this:
package com.KB
{
import flash.display.*;
import flash.events.*;
import flash.filters.*;
import flash.media.*;
import flash.net.*;
import flash.text.*;
import flash.utils.*;

public class Shell extends MovieClip
{
public function Shell()
{
//test();
addEventListener(Event.ADDED_TO_STAGE, addedHandler);
}
public function addedHandler(event:Event):void {
test();
}

public function test():void
{
try
{
trace(PreLoader(this.parent.parent).TEST_TEXT);
}
catch(err:Error)
{
trace("there was an error");
}
}
}
}
Save it, and be sure to run Shell.fla again (on this run, you will get the error, but that's to be expected), so that it generates a new Shell.swf, to be loaded by the other file(s).

No change is needed to PreLoader.as. Now run PreLoader's fla file and you will get the desired output.

drummer_scum
09-01-2008, 02:40 AM
thanks so much! this has been drivin me crazy for days! your a champion!