lundi 29 février 2016

Displaying movieclips from an array in sequential order

Here is what I am trying to do:

  1. Establish an array of movie clips in the library
  2. Randomize the order within the array
  3. On enter frame, display the first movie clip in the randomized array
  4. When a "next" button is clicked, the existing clip with unload and the next move clip in the array order will load
  5. Once the last movie clip in the array has been displayed and the "next" button has been clicked, the first movie clip in the array will load.
  6. this will repeat over and over if the user keeps clicking the "next" button.

Here is the code that I have so far that establishes the random array:

var animalArray:Array = ["animal1","animal2","animal3","animal4","animal5","animal6","animal7",]; 
var animalIndex:int = -1; 

function getNextanimal():String {
    if(animalIndex == -1) animalIndex = Math.round(Math.random() * (animalArray.length - 1)); 

    animalIndex++; //increment it
    if(animalIndex >= animalArray.length) animalIndex = 0; 

    return animalArray[animalIndex];
}

this is the code for my button:

button.addEventListener(MouseEvent.CLICK, clickCard);
function clickCard(event: MouseEvent): void {

    var animalCard: String = getNextanimal();
    addChild(animalCard);

}

I only get errors when clicking the button. Any suggestions?

Aucun commentaire:

Enregistrer un commentaire