1
votes

I apologize in advance for the newbiness of this question; I think I'm not grasping the basics of Actionscript but haven't been able to find an answer elsewhere.

What I want is some global constants like one would have in C++. In C++, I would simply have a file where I would #define MAP_HEIGHT 20, or something like that, and they would be globally accessible when I included the file at the top. In Actionscript, I've tried making a static class instead, like so:

package  
{
public class Settings {

    public const mapx:int = 20;

    public function Settings() {}

}
}

But when I try to reference it in my code with colon syntax

var a:int = Settings::mapx;

I get the error "[Fault] exception, information=TypeError: Error #1034: Type Coercion failed: cannot convert Settings$ to Namespace."

I tried dot syntax:

var a:int = Settings.mapx;

and got a different error, "Access of possibly undefined property mapx through a reference with static type Class."

But what I really want is a file of global static variables, like in C++, and can't find a way to do it in Actionscript.

1
where do you define the a var? Basically the dot syntax is the right one. - intuitivepixel

1 Answers

2
votes

Mark variables, constants, and functions with the static keyword, as in:

package
{
    public class MathUtil
    {

        public static const PI:Number = 3.141592653589;

        public static function deg2rad(angle:Number):Number
        {
            angle = !isNaN(angle) ? (angle) : (0);

            return angle * PI / 180;
        }

    }
}

Then you may use dot notation.

var pi:Number = MathUtil.PI;
var angle:Number = MathUtil.deg2rad(45);

From ActionScript 3.0 Reference for the Adobe Flash Platform: Statements, Keywords & Directives

Usage

class someClassName { 
  static var varName; 
  static const kName;
  static function methodName() { }
}

You can use static in class definitions only, not in interface definitions.

Static class members are not inherited. You cannot refer to a static class member using the name of a subclass, as you can in Java or C++. You can, however, refer to a static variable or method within a class or subclass, without using any qualifier. See the example below.

You cannot use the super statement or the this keyword inside a static method.