The problem is that, while static initializer blocks look like their
counterparts in Java, they have a little but important difference. Even
though you use curly braces, you're not creating a new scope. In AS, scope
is either class / global or local (to a function or method).
So, you cannot declare var i:int and access it statically, since by default
variables and methods are not static. An option is to make i static as you
did, but it looks ugly, since i is clearly a temporary variable.
In C# you can declare a static constructor to handle such a situation (I'm
not 100% sure, but I think you can't use static initializers). This
constructor is automatically called when the class is loaded, or at least,
before the first instance is created.
In Actionscript, you don't have this feature, but you can do something
similar, except you'd have to call your "static constructor" (or, more
properly, initializer method) manually.
You could do something like this:
private static var BLACK:TextFormat = new TextFormat('Arial', 24,
0x000000, true);
private static var RED:TextFormat = new TextFormat('Arial', 24,
0xFF0000, true);
// run static initializer method
{
staticInit();
}
private static function staticInit():void {
BLACK.letterSpacing = -4;
RED.letterSpacing = -4;
// i is now a local var and will go out of scope when this function
returns
for (var i:int = 0; i < 10; i++)
trace(i);
}
In fact, there is a static constructor, but it's created ad hoc by the
compiler and called automatically by the runtime when the class is
initialized. If you decompile / disassemble your code, you'll see a method
called:
static function YourClassName$cinit()
This method contains all the inline static initializers. But since it's
generated by the compiler, you cannot declare it yourself.
Nevertheless, making your own static initializer method and calling it seems
a bit cleaner if you need some static initialization logic.
Cheers
Juan Pablo Califano
2009/4/12 Alexander Farber <[email protected]>
> Hello,
>
> I have a static initializer in my class and it works ok:
>
> private static var BLACK:TextFormat = new TextFormat('Arial', 24,
> 0x000000, true);
> private static var RED:TextFormat = new TextFormat('Arial', 24,
> 0xFF0000, true);
> // place "1" closer to "0" in the "10" string
> {
> BLACK.letterSpacing = -4;
> RED.letterSpacing = -4;
> }
>
> But when I try to add a for-loop there (I need to
> add some data to static array I have in the class),
> then I get errors "Access of undefined property i":
>
> {
> BLACK.letterSpacing = -4;
> RED.letterSpacing = -4;
>
> for (var i:uint = 0; i < 10; i++)
> trace(i);
> }
>
> I have to move the variable i outside the initializer:
>
> private static var i:uint;
> {
> BLACK.letterSpacing = -4;
> RED.letterSpacing = -4;
>
> for (i = 0; i < 10; i++)
> trace(i);
> }
>
> Isn't it strange? It looks ugly to me...
>
> Regards
> Alex
> _______________________________________________
> Flashcoders mailing list
> [email protected]
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders