【问题标题】:Displaying movieclips from an array in sequential order按顺序显示数组中的影片剪辑
【发布时间】:2016-02-29 17:55:43
【问题描述】:

这是我想要做的:

  1. 在库中建立影片剪辑数组
  2. 随机排列数组中的顺序
  3. 进入帧时,显示随机数组中的第一个影片剪辑
  4. 单击“下一步”按钮时,将加载现有的已卸载剪辑和阵列顺序中的下一个移动剪辑
  5. 显示数组中的最后一个影片剪辑并单击“下一步”按钮后,将加载数组中的第一个影片剪辑。
  6. 如果用户不断单击“下一步”按钮,这将一遍又一遍地重复。

这是我目前建立随机数组的代码:

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

}

我只在单击按钮时出现错误。有什么建议吗?

【问题讨论】:

  • 我还没有查看您的代码,但发布错误以及它们对应的行通常会有所帮助。
  • 虽然我可以马上告诉你,你不能使用带有字符串作为参数的addChild。您的库中是否有与该字符串数组匹配的 AS3 链接 ID 的项目?

标签: actionscript-3 flash actionscript


【解决方案1】:

这是您可以做到这一点的一种方法:

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

【讨论】:

  • 谢谢!我是影片剪辑的链接 ID。因此,我试图在上面应用您的建议。但是,我在测试时收到此错误:“方法不能用作构造函数”在包含 addChild(new getNextAnimal()); 的行上
  • 我在答案末尾附近更新了代码,您可以尝试一些东西(我目前没有方便的 flash pro 进行测试)。此外,请确保您的数组包含类而不是字符串,并且 getNextAnimal 函数返回一个类而不是字符串。
  • 去吧。根据您的建议进行修改后,单击按钮时出现以下错误:TypeError: Error #1034: Type Coercion failed: cannot convert animal2@16592a2bdba1 to Class.
  • 您确定在某处没有名为animal2 的对象吗?您确定您的链接 ID 与您的数组中的内容完全匹配吗?
  • 现在一切正常。必须修复链接 ID。谢谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-04
  • 2013-04-22
  • 1970-01-01
  • 2018-03-29
  • 1970-01-01
  • 2017-08-23
  • 1970-01-01
相关资源
最近更新 更多