I am following a tutorial for HaxeFlixel which uses the Haxe language. Now I do not have any experience in Haxe, but I decided to brave the tutorial since I do have experience in Java and Haxe as a language seems weirdly similar to Java.
So far, it was going smoothly. However, I came across this piece of code and I have several questions:
class FSM
{
public var activeState:Void->Void;
public function new(?InitState:Void->Void):Void
{
activeState = InitState;
}
public function update():Void
{
if (activeState != null)
activeState();
}
}
Now I understand that this is a class called FSM
and has a variable called activeState
.
Here are my questions:
What is the type of activeState
? I would understand if was something like activeState:Void
but what does the ->
accomplish? Is it used as a pointer? Is it a void pointer pointing to another void variable?
What does the ?
before the InitState:Void->Void
signify?
After the if statement, the activeState
is being called like a function using the parentheses. However, activeState
is a variable and not a function. So what does the if statement do exactly?
Also another question:
public var playerPos(default, null):FlxPoint;
I understand playerPos
is an instance of the FlxPoint
class, but what does the default
and null
do?
The type is Void->Void
- it's a function type, in this case a function that takes no arguments and returns Void
.
?
indicates an optional argument. In this case it's equivalent to writing new(InitState:Void->Void = null)
.
activeState
is a variable, but it stores a function - like you guessed, activeState()
calls it.
(default, null)
indicates that playerPos
is a property. With default
as the read access identifier and null
as the write access identifier, it is read-only outside of the class it's defined in.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With