4

I'm trying to change a static variable in the class's constructor. At the start I have:

public static var mainReference:Main;
public static var timerReference:Timer;
public var timer:Timer = new Timer(1000);

This is so my static functions can access main and timer. At Main's constructor I have:

mainReference = this;
timerReference = timer;

The problem is, the first gives no error when I compile it, but the second tells me Access of undefined property (timerReference).

5
  • 2
    What if you do timerReference = new Timer(1000) in the constructor instead? Commented Jul 12, 2011 at 8:36
  • Why? Initializing a static variable in every instance of a class doesn't sound like a very good idea to me. Commented Jul 12, 2011 at 8:43
  • 1
    @RIAstar Surely OP is not doing this with every class. I bet he just want a global access to the document class, just like a singleton. However timerReference is pretty useless since timer is public, making it possible to access it by doing Main.mainReference.timer. Commented Jul 12, 2011 at 8:47
  • @ancide: Oh! I have never thought of that! Thanks Commented Jul 12, 2011 at 8:58
  • @Ancide @Marty If the OP is doing this only with the first instance of the class, then the program becomes dependent on order of instantiation. Next developer comes in; he unknowingly changes the order in which objects are created; the program breaks. Commented Jul 12, 2011 at 9:11

1 Answer 1

5

It might have something to do that the flash player is trying to access timerReference as a class var instead of a static var.

Try this:

this.mainReference = this;
Main.timerReference = this.timer;

Now you are telling flash player to explicitly access mainReference as a class var and timerReference as a static class var.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.