Move through a sequence of variables in Haxe

134 views Asked by At

I honestly don't know how this function is called(and I'm sure it is a simple thing), neither how I can effectively search for it(I didn't find it when I googled it).

I have a set of string variables in Haxe called

public static var variable01:String;
public static var variable02:String;
public static var variable03:String;

public static function TextContent() 
{
    variable01 = new String("abc");
    variable02 = new String("def");
    variable03 = new String("ghi");
}   

And a function that shows the text in the variable

_message = variable01; 
_Dialogue = new FlxText(60, 400, 100, _message);

When the user clicks the screen, I want the variable in display change to the next one. Is there a way to simply change the number of the variable? I need to be able to change the text of the variable for another dialogue, because then there will be another set of variables with the same kind of pattern(text01, text02, text03, etc...)

1

There are 1 answers

1
Andy Li On BEST ANSWER

When there is "a set of variables", it is nearly always a great use case of Array. To remember what is the current displaying variable, we need an additional integer variable (current below) to store the array index.

public static var variables:Array<String> = ["abc", "def", "ghi"];
public static var current:Int = 0;

//initialize
_Dialogue = new FlxText(60, 400, 100, variables[current]);


//when user clicks
++current;
if (current >= variables.length) { //we don't have that many variables
    current = 0; //reset it to the first one
}
_Dialogue.text = variables[current];