Access stage elements from sub classes
The question is simple: Access stage objects (like Movie Clip or text fields) from sub classes. The problem came when I define a subclass and try to access stage objects. How can I do that? Imagine I have a movie clip named redBox_mc on the stage.I want to change it`s alpha after I get my xml loaded.(or any other action in subclass)
Main class:
package src{
// ..
public class code01 extends MovieClip {
public var rt:xmlReader = new xmlReader("art.xml"); // my subclass
public function code01():void {
// .. my code
}
}
}
xmlReader subclass:
package src{
// ..
public class xmlReader extends MovieClip {
// ..
public function xmlReader(xmlFilename:String)
// .. my code
stage.redBox_mc.alpha = .2 ; // doesn’t work
MovieClip(parent).redBox_mc.alpha = .2 ; // doesn’t work
}
}
}
P开发者_StackOverflow中文版lease help me ..
There are many ways to make the stage accessible to a child object's class:
OPTION 1
The simplest way is to parse a reference of the stage to a child object's class:
Main.as(document class):
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
public function Main()
{
if(stage) init()
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
public function init(e:Event = null):void
{
var foo:Foo = new Foo(stage);
}// end function
}// end class
}// end package
Foo.as:
package
{
import flash.display.Stage;
public class Foo
{
public function Foo(stage:Stage)
{
if(stage) trace("success"); // output: success
}// end function
}// end class
}// end package
OPTION 2
Another option is that you add the display object to the display list. By doing this, your display object's class will have a reference to the stage. You then have to add an event listener within your display object's class that listens for the display object to be added to the stage. Only when the event listener recieves this event will you have a reference to the stage:
Main.as(document class):
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
public function Main()
{
if(stage) init()
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
public function init(e:Event = null):void
{
var foo:Foo = new Foo();
addChild(foo);
}// end function
}// end class
}// end package
Foo.as:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Foo extends Sprite
{
public function Foo()
{
if(stage) init()
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
public function init(e:Event = null):void
{
if(stage) trace("success"); // output: success
}// end function
}// end class
}// end package
OPTION 3
Yet another option is to store the reference to the stage in a class that can be accessed globally. To do this you use static public properties or methods to set and get the reference of the stage directly to and from the class:
Main.as(document class):
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
public function Main()
{
if(stage) init()
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
public function init(e:Event = null):void
{
GlobalVars.stage = stage;
var foo:Foo = new Foo();
}// end function
}// end class
}// end package
GlobalVar.as:
package
{
import flash.display.Stage;
public class GlobalVars
{
public static var stage:Stage;
}// end class
}// end package
Foo.as
package
{
import flash.display.Stage;
public class Foo
{
public function Foo()
{
var stage:Stage = GlobalVars.stage;
if(stage) trace("success"); // output: success
}// end function
}// end class
}// end package
[UPDATE]
Looking at your code I just couldn't figure out why your xmlReader
class extends DisplayObject
in the first place. xmlReader
seemingly handles all xml related logic like loading and intepreting xml, so why not leave it at that? Let another class handle the object on the stage. I think the real solution to your problem is a better code structure, the following is an example of this:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
public class Main extends Sprite
{
public function Main()
{
if(stage) init()
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
public function init(e:Event = null):void
{
var textField:TextField = new TextField();
textField.name = "textField";
stage.addChild(textField);
var stageManager:StageManager = new StageManager(stage, "dummy.xml");
}// end function
}// end class
}// end package
import flash.display.Stage;
import flash.events.Event;
import flash.text.TextField;
class StageManager
{
private var _stage:Stage;
private var _xmlReader:XMLReader;
public function StageManager(stage:Stage, url:String)
{
_stage = stage;
_xmlReader = new XMLReader(url);
_xmlReader.addEventListener(Event.COMPLETE, onXMLReaderComplete);
}// end function
private function onXMLReaderComplete(e:Event):void
{
var textField:TextField = _stage.getChildByName("textField") as TextField;
textField.text = "XML LOADED!";
}// end function
}// end class
import flash.events.EventDispatcher;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.IOErrorEvent;
import flash.errors.IOError;
class XMLReader extends EventDispatcher
{
public var xml:XML;
public function XMLReader(url:String)
{
read(url);
}// end function
public function read(url:String)
{
var urlLoader:URLLoader = new URLLoader(new URLRequest(url));
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onURLLoaderIOError);
urlLoader.addEventListener(Event.COMPLETE, onURLLoaderComplete);
}// end function
private function onURLLoaderIOError(e:IOErrorEvent):void
{
throw new IOError(e.text);
}// end function
private function onURLLoaderComplete(e:Event):void
{
xml = URLLoader(e.target).data as XML;
dispatchEvent(new Event(Event.COMPLETE));
}// end function
}// end class
精彩评论