按顺序显示数组中的影片剪辑
Displaying movieclips from an array in sequential order
这是我正在尝试做的事情:
- 在库中建立影片剪辑数组
- 随机排列数组内的顺序
- 进入帧时,显示随机数组中的第一个影片剪辑
- 单击 "next" 按钮时,将加载已卸载的现有剪辑和数组顺序中的下一个移动剪辑
- 显示数组中的最后一个影片剪辑并单击 "next" 按钮后,将加载数组中的第一个影片剪辑。
- 如果用户一直点击 "next" 按钮,这将一遍又一遍地重复。
这是我目前建立随机数组的代码:
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);
}
我只在点击按钮时出错。有什么建议吗?
这是您可以执行此操作的一种方法:
//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,则使用它们的实际 class 名称而不是数组中的字符串,然后实例化它们:
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);
}
编辑
知道您使用的是链接 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);
}
这是我正在尝试做的事情:
- 在库中建立影片剪辑数组
- 随机排列数组内的顺序
- 进入帧时,显示随机数组中的第一个影片剪辑
- 单击 "next" 按钮时,将加载已卸载的现有剪辑和数组顺序中的下一个移动剪辑
- 显示数组中的最后一个影片剪辑并单击 "next" 按钮后,将加载数组中的第一个影片剪辑。
- 如果用户一直点击 "next" 按钮,这将一遍又一遍地重复。
这是我目前建立随机数组的代码:
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);
}
我只在点击按钮时出错。有什么建议吗?
这是您可以执行此操作的一种方法:
//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,则使用它们的实际 class 名称而不是数组中的字符串,然后实例化它们:
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);
}
编辑
知道您使用的是链接 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);
}