按顺序显示数组中的影片剪辑 [英] Displaying movieclips from an array in sequential order
问题描述
- 在库中建立一个影片剪辑数组
- 随机化数组中的订单
- 在输入框架中,显示随机数组中的第一个影片剪辑
按钮被点击,现有剪辑与卸载和下一个移动剪辑的数组顺序将加载 - 一旦数组中的最后一个影片剪辑已经显示,并且下一个按钮已经点击后,数组中的第一个影片剪辑将被加载。
- 如果用户不断点击下一步按钮,这将反复重复。
- Establish an array of movie clips in the library
- Randomize the order within the array
- On enter frame, display the first movie clip in the randomized array
- When a "next" button is clicked, the existing clip with unload and the next move clip in the array order will load
- 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.
- this will repeat over and over if the user keeps clicking the "next" button.
ol>
以下是我建立随机数组的代码:
var animalArray:Array = [animal1,animal2,animal3,animal4,animal5,animal6,animal7,]
var animalIndex:int = -1;
$ b $ function getNextanimal():String {
if(animalIndex == -1)animalIndex = Math.round(Math.random()*(animalArray.length - 1));
animalIndex ++; //增加它
if(animalIndex> = animalArray.length)animalIndex = 0;
return animalArray [animalIndex];
这是我的按钮的代码:
button.addEventListener(MouseEvent.CLICK,clickCard);
函数clickCard(event:MouseEvent):void {
var animalCard:String = getNextanimal();
addChild(animalCard);
$ / code>
我只在点击按钮时发生错误。有什么建议?
解决方案以下是您可以执行此操作的一种方法:
//创建你的数组(这里没有任何改变,尽管你可以在循环中做到这一点,如果所有的项目都遵循数字模式,就像它出现的那样)
var animalArray:Array = [animal1,animal2,animal3,animal4,animal5,animal6,animal7,]
//创建一个随机化函数:
function randomSort(a:*,b:*):int {
return(Math.random()> 5)? 1:-1;
//使用
上面的函数对数组进行排序animalArray.sort(randomSort);
//复制数组,以便在循环结束后重置它
var origArray:Array = animalArray.concat();
//获取下一个动物
函数getNextAnimal():String {
//如果数组现在为空,则复制原始数组(您也可以在这里重新随机化如果需要的话)
if(!animalArray.length)animalArray = origArray.concat();
$ b $ //弹出数组的最后一项(并将其从数组中移除)
return(animalArray.pop());
code
$ b $ p现在, '遇到很可能是试图使用 addChild
与一个字符串作为参数(它期待一个显示对象)的结果。
$ b 如果这些动物实际上是实例名称,只需使用 getChildByName
:
addChild(getChildByName(getNextAnimal));
如果它们是从库中导出的链接ID,那么使用它们是实际的类名而不是字符串在你的数组中,然后实例化它们:
$ b $ $ pre $ var animalArray:Array = [Animal1,Animal2,Animal3,Animal4];
//然后:
函数getNextAnimal():Class ...
//然后:
var cls:Class = getNextAnimal( );
addChild(new cls());
所以,下次点击应该是这个样子:
button.addEventListener(MouseEvent.CLICK,clickCard);
var curItem:DisplayObject; //你需要一个var来跟踪最后添加的项目
函数clickCard(event:MouseEvent):void {
//如果有一个当前项目,并且它有一个父项,删除它
if(curItem&& curItem.parent)removeChild(curItem);
var cls:Class = getNextAnimal();
curItem = new cls();
addChild(curItem);
编辑
知道你正在使用Linkage ID,这就是我要写的:
//一个向量就像一个数组,除了所有的成员必须是指定的类型
var animalArray:Vector。< Class> = new< Class> [animal1,animal2,animal3];
$ b $函数randomSort(a:*,b:*):int {
return(Math.random()> .5)? 1:-1;
}
animalArray.sort(randomSort);
var origArray:Vector。< Class> = animalArray.concat();
函数getNextAnimal():Class {
if(!animalArray.length)animalArray = origArray.concat();
return(animalArray.pop());
}
button.addEventListener(MouseEvent.CLICK,clickCard);
var curItem:DisplayObject; //一个var跟踪最后添加的项目
函数clickCard(event:MouseEvent):void {
//如果有一个当前项目并且有一个父项目,则将其删除
if(curItem&& curItem.parent)removeChild(curItem);
var cls:Class = getNextAnimal();
curItem = new cls();
addChild(curItem);
}
Here is what I am trying to do:
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?
解决方案 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);
}
这篇关于按顺序显示数组中的影片剪辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!