-1

I am developing flash game.

first class:

public class Game{
    public var age;
}

second class:

public class Display{
    Game.age  //<-- cannot retrieve
}

so how to get the variable from Game() to Display()?

Jørgen R
  • 10,568
  • 7
  • 42
  • 59
Alan Lai
  • 1,094
  • 7
  • 18
  • 41
  • 3
    This is not a very good question to ask if you are developing a game. You should learn flash by starting with the basics. A few things to start with: hello world, fizzbuzz, implement a few sorting algorithms, vector graphics manipulation (API), xml/AMF loading + parsing, design patterns. This will scratch the surface of what you need to make a game in Flash. – mfa Feb 16 '12 at 20:44

1 Answers1

1

See this other post about the difference between static variables and non-static variables:

Actionscript 3: Can someone explain to me the concept of static variables and methods?

You're attempting to access age as a static variable, when it is, in fact, not one.

To access it in your code, you would have to instance the Game class and then reference "age" on the instance of the class.

After looking at this further, I feel I should point out that if you intend the "age" property of the Game class to be read-only, you should not make it public and instead create a static method on the Game class which can return the information to you.

public class Game{
    private var age = 10;
    public static function getAge() {
        return self.age;
    }
}

public class Display{
    public function whatever() {
        trace( Game.getAge() );
    }
}
Community
  • 1
  • 1
Brian
  • 3,013
  • 19
  • 27
  • public static Instance: Game = new Game(); i put this in Game class but after some second the swf run, it cause an error ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild() at Class::Game/Update() – Alan Lai Feb 16 '12 at 19:34
  • return self.age; has give error that 1120: accessof undefined property – Alan Lai Feb 16 '12 at 21:54
  • Oh right Actionscript... um try Game.age? I think that's how flash handles the static methods... been a while since I coded in AS3 :P – Brian Feb 16 '12 at 22:24