- Home /
Question by
Giaddon · Mar 31, 2014 at 06:55 PM ·
javascriptarrays
Advance through array elements with math (++)?
Hey everyone.
Stupid-simple question here:
I'm using Javascript. I have an array of strings (var textArray). I have the GUI set up to display one string at a time (var activeText), starting with the first element (textArray[0]). When the player hits spacebar, I want to replace the current activeText with the next string in the array, preferably using something simple like ++ applied to the textArray element, but I have no idea what to apply the ++ to.
What's the trick?
Thank you!
Comment
define an integer, say someint, everytime spacebar is hit increase it by 1 unless someint = array item count. if it's equal, set it to 0.
then your guitext would be textArray[someint].
Best Answer
Answer by tanoshimi · Mar 31, 2014 at 07:18 PM
var textArray = ["stringone", "stringtwo", "stringthree"];
var i : int = 0;
Update() {
if(GetKeyDown(KeyCode.Space)){
i++;
}
activeText = textArray[i];
}
?