We will modify the movie so that each time it leaves the stage it reenters with the next message in the array.
We will modify the code as shown below:
var messages: Array=new Array("You can do it!","Keep up the good work!","Way to go!");
var messageNumber=0; //to keep track of which message is next
lblMessage.text=messages[messageNumber];
lblMessage.x=stage.stageWidth; //put the message off on the right
addEventListener(Event.ENTER_FRAME,frames);
function frames(e:Event):void {
lblMessage.x--;
if(lblMessage.x<0-lblMessage.width) {
lblMessage.x=stage.stageWidth; //put the message off on the right
messageNumber++; //next message
if(messageNumber>=messages.length) { //make sure it is not too much
messageNumber=0; //if it is too much, set it back to zero
}
lblMessage.text=messages[messageNumber];
}
}
It is very important that we do not refer to messages[3] if there are only 3 items in the list.
Remember that if there are 3 items in the list, they are [0], [1], and [2].
Referring to messages[3] will cause a run time error.
An array has a property length, that gives the number of items in the list. In the example above messages.length is 3.
If the index (messageNumber) is greater than OR equal to 3 there will be an error, so we must set it back to zero when it is 3 or more.
Experiment: