按顺序显示数组中的动画片段

时间:2016-02-29 17:55:43

标签: actionscript-3 flash actionscript

以下是我要做的事情:

  1. 在库中建立一系列影片剪辑
  2. 随机化数组中的顺序
  3. 在输入框架上,显示随机数组中的第一个影片剪辑
  4. 单击“下一步”按钮时,将加载现有的带卸载的剪辑和数组顺序中的下一个移动剪辑
  5. 显示阵列中的最后一个影片剪辑并单击“下一步”按钮后,将加载阵列中的第一个影片剪辑。
  6. 如果用户一直点击“下一步”按钮,这将反复重复。
  7. 这是我到目前为止建立随机数组的代码:

    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];
    }
    

    这是我按钮的代码:

    button.addEventListener(MouseEvent.CLICK, clickCard);
    function clickCard(event: MouseEvent): void {
    
        var animalCard: String = getNextanimal();
        addChild(animalCard);
    
    }
    

    单击按钮时我只会收到错误。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

以下是一种可以执行此操作的方法:

//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());
}

现在,您遇到的错误可能是尝试使用addChild并将字符串作为参数(它期望显示对象)的结果。

如果这些动物实际上是实例名称,只需使用getChildByName

addChild(getChildByName(getNextAnimal));

如果它们是从库中导出的链接ID,那么使用它们是实际的类名而不是数组中的字符串,然后实例化它们:

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

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

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

所以,你的下一次点击应该是这样的:

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);
}

修改

知道你使用的是Linkage ID,我就是这样写的:

//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);
}