actionscript 3 - Organizing Long Scripts In Separate Files? -
in attempt organize code, i'm trying split (lengthy) main controller class separate files, but new files must still have access variables , functions of main controller class.
i'm trying cut , paste code controller class new class/file, allowing controller class call new class, , allowing new class have access controller class's properties , function.
assuming i'm not totally bludgeoning appropriate design patterns, below unsuccessful attempt @ accomplishing task:
package { import flash.display.sprite; import flash.events.event; public class test extends sprite { public var mystring:string; public function test() { if (stage) init(null); else addeventlistener(event.added_to_stage, init); } private function init(evt:event):void { if (haseventlistener(event.added_to_stage)) removeeventlistener(event.added_to_stage, init); /////////////// move commented code new file /////////////////////// // // //assign string value // mystring = "hello world"; // // //draw blue square // var sq:sprite = new sprite(); // sq.graphics.beginfill(0x0000ff); // sq.graphics.drawrect(10, 10, 100, 100); // sq.graphics.endfill(); // super.addchild(sq); // // //call tracer function // tracer(); // ////////////////////////////////////////////////////////////////////// //call pasted method in newfile.as newfile.mynewfunction(); // <- doesn't work } public function tracer():void { trace(mystring); } } }
new file doesn't have access controller class - doesn't work. how can write new file have access properties, functions, stage, etc. of controller class, if code never removed , still in original place.
package { public class newfile { public static function mynewfuntion():void { //assign string value mystring = "hello world"; //draw blue square var sq:sprite = new sprite(); sq.graphics.beginfill(0x0000ff); sq.graphics.drawrect(10, 10, 100, 100); sq.graphics.endfill(); super.addchild(sq); //call tracer function tracer(); } } }
public class mainclass extends sprite { private var subclass:subclass; public function mainclass { var controller:controller = new controller(); subclass = new subclass(controller); addchild( subclass ); } private function init():void { subclass.dowhatever(); } } public class controller { public function dothis():void { } public function dothat():void { trace("controller that..."); } public function dosomethingelse():void { } } public class subclass extends sprite { private var controller:controller; public function subclass(controller:controller) { this.controller = controller; trace( "new subclass instance!" ); } public function dowhatever():void { controller.dothat(); } }
Comments
Post a Comment