View Full Version : Variables between AS Files
KillinSprE
September 5th, 2009, 05:26 PM
I have variables declared (I assume) in Main.as and I wish to use them in other AS files such as pg1.as
Here's my code:
//Main.as
package {
import fl.controls.*;
import flash.text.*;
import flash.events.*;
import flash.net.*;
import flash.display.Sprite;
public class Main extends Sprite {
public static var tf_CSM_17_l:TextFormat = new TextFormat();
tf_CSM_17_l.font = "Comic Sans MS";
tf_CSM_17_l.bold = false;
tf_CSM_17_l.color = 0x000000;
tf_CSM_17_l.size = 17;
tf_CSM_17_l.align = TextFormatAlign.LEFT;
//several more almost identical declarations
function Main(){
var Page1:pg1 = new pg1();
Page1.pg_1();
}
//lots more coding not relevant to the problem
}
}
//pg1.as
package {
import fl.controls.*;
import flash.text.*;
import flash.events.*;
import flash.net.*;
import flash.display.Sprite;
public class pg1 extends Sprite {
public function pg_1(){
var pt1:Label = new Label()
pt1.setStyle("textFormat", tf_CSM_17_l);
pt1.move(102,93);
pt1.width = 789;
pt1.height = 284;
pt1.text="some text";
addChild(pt1);
}
}
}
this produces the following error:
1120: Access of undefined property tf_CSM_17_l.
I assume its in the wording of the "public static var". What should be used so that this variable is available to all AS files?
GeorgeCrabtree
September 5th, 2009, 09:28 PM
//Main.as
You need to pass the varible to your pg_1 class in its constructor. like this.
also ditch the static varible declaration.
:)
package {
import fl.controls.*;
import flash.text.*;
import flash.events.*;
import flash.net.*;
import flash.display.Sprite;
public class Main extends Sprite {
public var tf_CSM_17_l:TextFormat = new TextFormat();
tf_CSM_17_l.font = "Comic Sans MS";
tf_CSM_17_l.bold = false;
tf_CSM_17_l.color = 0x000000;
tf_CSM_17_l.size = 17;
tf_CSM_17_l.align = TextFormatAlign.LEFT;
//several more almost identical declarations
function Main(){
var Page1:pg1 = new pg1();
Page1.pg_1(tf_CSM_17_1);
}
//lots more coding not relevant to the problem
}
}
//pg1.as
package {
import fl.controls.*;
import flash.text.*;
import flash.events.*;
import flash.net.*;
import flash.display.Sprite;
public class pg1 extends Sprite {
public function pg_1(incomingFormat:TextFormat){
var pt1:Label = new Label()
pt1.setStyle("textFormat", incomingFormat);
pt1.move(102,93);
pt1.width = 789;
pt1.height = 284;
pt1.text="some text";
addChild(pt1);
}
}
}
GeorgeCrabtree
September 5th, 2009, 09:35 PM
AS3 doesn't support global variables, the declaration - public static var - is for something else. Just declare all you variables as - public var - for now. It can be quite confusing as to what all the declarations are for, you'll use them when you have to
:)
KillinSprE
September 5th, 2009, 09:58 PM
started by changing the public static var to public var but that gives me an 1120: Access of undefined property tf_CSM_17_l. for each of the next five lines in the Main.as. also when i get your solution implemented, how do I handle multiple variable? currently there are 4 other simliar text formats that could be used in several AS files as well as a variable that will be used on pretty much every AS file
Felixz
September 6th, 2009, 06:00 AM
Main.tf_CSM_17_l
GeorgeCrabtree
September 6th, 2009, 06:07 AM
your actionscript was looking a bit funky so i've tidyed it up for you.
//Main.as
package {
import fl.controls.*;
import flash.text.*;
import flash.events.*;
import flash.net.*;
import flash.display.Sprite;
public class Main extends Sprite {
public var txtFmt1:TextFormat;
// you dont set properties for your varibles here, you just define them
public function Main():void {
// use this function to set all the properties for your textFormats
initTxtFmts();
var Page1:pg1 = new pg1(txtFmt1);
// you dont need to call the constructor this is done in the line above when you type, new pg1(txtFmt1);
//Page1.pg_1();
}
private function initTxtFmts():void {
txtFmt1 = new TextFormat();
txtFmt1.font="Comic Sans MS";
txtFmt1.bold=false;
txtFmt1.color=0x000000;
txtFmt1.size=17;
txtFmt1.align=TextFormatAlign.LEFT;
}
}
}
//pg1.as
package {
import fl.controls.*;
import flash.text.*;
import flash.events.*;
import flash.net.*;
import flash.display.Sprite;
public class pg1_1 extends Sprite {
public function pg_1(txtFmt1:TextFormat):void {
var pt1:Label = new Label();
pt1.setStyle("textFormat", txtFmt1);
pt1.move(102,93);
pt1.width=789;
pt1.height=284;
pt1.text="some text";
addChild(pt1);
}
}
}
This should work fine. I also changed the name of your text format to txtFmt1 as it was hurting my brain looking at all those random characters. As for multiple variables you could put them all into an array and then pass the array instead. There is no native support for global variables, so your gonna have to get used to passing in all the variables to your classes.
Hope I helped and didn't confuse you :)
KillinSprE
September 6th, 2009, 03:08 PM
George, tried your method tho I cannot see how to use it for several textformats. So I went with the easier suggestion of putting Main. in front of each variable and it worked. Thanks Felixz
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.