actionscript 3 - Using variable from another class as3 -
i separated 1 big file multiple file have cleaned , got problem now.
i have main.as, character.as, camera.as.
what i'm trying access variable class set later on class. ill show mean.
from main.as im loading each class , add them child displayed on screen.
public function buildgame() { var loadmap:sprite = new nf_mapbuilder(); var xchar:sprite = new nf_character(); var xcam:sprite = new nf_camera(); var usercontrol:nf_usercontrol = new nf_usercontrol(); addchild(loadmap); addchild(xchar); addchild(xcam); addchild(usercontrol); }
everything show on screen needed. goes character.as:
package as3 { import flash.display.sprite; import flash.events.event; public class nf_character extends sprite { public var character_pos:array = new array(); public var character_is_moving:boolean = false; public var character_x_dir:int = 0; public var character_y_dir:int = 0; public var character:hero = new hero(); public function nf_character() { addeventlistener(event.added_to_stage,xcharload); } public function xcharload(e:event) { character_pos = [2,2]; character.x=64*(character_pos[1]); character.y=64*(character_pos[0]); addchild(character); } } }
there problem. need use variable set there in character.as use in camera.as:
package as3 { import flash.display.sprite; import flash.events.event; import flash.geom.rectangle; import flash.display.stagescalemode; import as3.nf_character; public class nf_camera extends sprite { private var xchar:nf_character = new nf_character(); //camera variables var stagew2:number; var stageh2:number; var view:rectangle; public function nf_camera() { addeventlistener(event.added_to_stage,xcamgo); } public function xcamgo(e:event):void { trace("camera pos - " + xchar.x + " " + xchar.character.y); view = new rectangle(0,0,stage.stagewidth,stage.stageheight) stagew2 = stage.stagewidth / 2 - 32; stageh2 = stage.stageheight / 2 - 32; addeventlistener(event.enter_frame,camview); } public function camview(e:event):void { view.x = xchar.character.x - stagew2; view.y = xchar.character.y - stageh2; scrollrect = view; } } }
when in 1 big file ok had set variable in class , acessing trough every function im kinda confused. see how this?
you can pass character class instance camera class instance argument of constructor. have reference character inside camera class , can access it's variables
// inside buildgame() in main. var xchar:nf_character = new nf_character(); var xcam:nf_camera = new nf_camera(xchar); // inside nf_camera public function nf_camera(char:nf_character) { xchar = char; }
Comments
Post a Comment