Displaying movieclips from an array in sequential order

不打扰是莪最后的温柔 提交于 2019-12-24 07:31:16

问题


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?


回答1:


Here is one way you can do this:

//create your array (nothing changed here, though you could do this in a loop if all items follow a numerical pattern like it appears they do)
var animalArray:Array = ["animal1", "animal2", "animal3", "animal4", "animal5", "animal6", "animal7", ];

//create a function that randomizes the array:
function randomSort(a:*, b:*):int {
    return ( Math.random() > .5 ) ? 1 : -1;
}

//sort the array using the function above   
animalArray.sort(randomSort);

//copy the array so you can reset it after getting to the end of a cycle
var origArray:Array = animalArray.concat();

//get the next animal
function getNextAnimal():String {
    //if the array is now empty, copy the original array (you could also re-randomize here if desired)
    if (!animalArray.length) animalArray = origArray.concat();

    //pop takes the last item of the array (and removes it from the array)
    return(animalArray.pop());
}

Now, the error you're encountering is likely the result of trying to use addChild with a string as the parameter (it's expecting a display object).

If these animals are actually instance names, just use getChildByName:

addChild(getChildByName(getNextAnimal));

If they are linkage ID's exported from your library, then use they're actual class names instead of strings in your array, then instantiate them:

var animalArray:Array = [Animal1, Animal2, Animal3, Animal4];

//then later:
function getNextAnimal():Class  ...

//then later:
var cls:Class = getNextAnimal();
addChild(new cls());

So, your next click should look something like this:

button.addEventListener(MouseEvent.CLICK, clickCard);    
var curItem:DisplayObject; //you need a var to keep track of the last item added

function clickCard(event: MouseEvent): void {
    //if there is a current item and it has a parent, remove it
    if(curItem && curItem.parent) removeChild(curItem);
    var cls:Class = getNextAnimal();
    curItem = new cls();
    addChild(curItem);
}

EDIT

Knowing that your are using Linkage ID's, this is how I'd write this:

//a vector is just like an array, except all members have to be of the specified type
var animalArray:Vector.<Class> = new <Class>[animal1, animal2, animal3];

function randomSort(a:*, b:*):int {
    return ( Math.random() > .5 ) ? 1 : -1;
}

animalArray.sort(randomSort);
var origArray:Vector.<Class> = animalArray.concat();

function getNextAnimal():Class {
    if (!animalArray.length) animalArray = origArray.concat();
    return(animalArray.pop());
}

button.addEventListener(MouseEvent.CLICK, clickCard);    
var curItem:DisplayObject; //a var to keep track of the last item added

function clickCard(event: MouseEvent): void {
    //if there is a current item and it has a parent, remove it
    if(curItem && curItem.parent) removeChild(curItem);
    var cls:Class = getNextAnimal();
    curItem = new cls();
    addChild(curItem);
}


来源:https://stackoverflow.com/questions/35706217/displaying-movieclips-from-an-array-in-sequential-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!