Copy link to clipboard
Copied
Hi all,
Is it possible to access a movieclip which has been dragged onto the main stage in my FLA from an external class that i've written? Can i create a variable that refers to the object on the stage? Or does it have to be added dynamically to be accessible?
Many Thanks
Matt
Copy link to clipboard
Copied
If you pass a stage reference to the class you should be able to target aything that is on it.
Copy link to clipboard
Copied
Hi Ned, thanks for the reply. Could you show me an example of how i'd go about that?
Many thanks
Matt
Copy link to clipboard
Copied
How do you implement the other class in the main file... how do you instantiate it?
Copy link to clipboard
Copied
Hi Ned, i'm not instantiating it as such i'm just using it to store quite a large method which is being called from the document class.
I'm trying to break all my code down into chuncks stored in .as files so that i dont have 2,000 lines of code all in frame one of the main timeline : S
So this is my document class:
package {
import flash.display.MovieClip;
import loadXML;
public class mainClass extends MovieClip {
public function mainClass () {
loadXML.importXML();
}
}
}
then this is the class being called from the mainClass:
package {
import flash.display.Stage;
import flash.net.*;
import flash.events.ProgressEvent
import flash.events.Event;
import flash.text.*;
public class loadXML {
private static var myXML:XML;
private static var myLoader:URLLoader = new URLLoader();
public function loadXML() {
}
public static function importXML():void{
myLoader.load(new URLRequest("pipeLineData.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
}
private static function processXML(e:Event):void {
myXML = new XML(e.target.data);
objectOnStage.text = myXML.TEXT[0];
//etc...
//etc...
}
}
}
Many thanks
Matt
Copy link to clipboard
Copied
One way around it is to move all the stuff that targets stuff in the stage into the document class where it should be and use the loadXML class strictly for loading the data into the document class. The loadXML class should not be concerned at all with what is in the xml data. It should be an independent module that serves the purpose of its name. That way it can be re-used as a generic entity.
Beyond that, you might just try passing "this" as an argument to the class constructor. As in...
loadXML.importXML(this);
And in the loadXML class you have an object defined for that role in the class that gets assigned it in the constructor function.
Copy link to clipboard
Copied
All objects that are on the stage in Frame 1 are available in the constructor to the Class. If you want to use code completion to refer to the object turn off "declare stage instances automatically" and declare the instances in the file. You can declare them as a getter/setter pair, which will allow you to know when the instance has been populated by Flash even if it is not on Frame 1.
However, given your particular situation, you only need to break out your data and service Classes into Classes that do not extend MovieClip or Sprite. I'd suggest extending EventDispatcher, so that you can dispatch events when changes occur, such as when the load completes. When the main document Class or some other suitable Class hears that event, it can populate the variables accordingly.
I'd suggest that you not hard code the path to the XML in importXML. Note that static functions like you're using are really bad practice, especially in combination with the hard-coded path.