我想创建一个随机化的实例和一个数组中的对象的正确排序的实例,但由于某种原因它不能正常工作。 这是它的代码:Array重复不工作
import flash.sampler.NewObjectSample;
import flash.display.Sprite;
import flash.events.MouseEvent;
var eating_breakfast:Sprite;
var walking:Sprite;
var swimming:Sprite;
var art:Sprite;
var choices:Array = new Array();
var i: Number = 0;
eating_breakfast = new Sprite();
eating_breakfast.graphics.beginFill(0xE39D43);
eating_breakfast.graphics.drawRect(0,0,50,50);
eating_breakfast.graphics.endFill();
eating_breakfast.x = 50;
eating_breakfast.y = 50;
walking = new Sprite();
walking.graphics.beginFill(0xC3266C);
walking.graphics.drawRect(0,0,50,50);
walking.graphics.endFill();
walking.x = 100;
walking.y = 100;
swimming = new Sprite();
swimming.graphics.beginFill(0x48AFD1);
swimming.graphics.drawRect(0,0,50,50);
swimming.graphics.endFill();
swimming.x = 150;
swimming.y = 150;
art = new Sprite();
art.graphics.beginFill(0xafdb44);
art.graphics.drawRect(0,0,50,50);
art.graphics.endFill();
art.x = 200;
art.y = 200;
choices.push (eating_breakfast);
choices.push (walking);
choices.push (swimming);
choices.push (art);
stage.addEventListener (MouseEvent.CLICK, switchpic);
var indexcount = 0 ;
var randomize : Number ;
civilizedorder() ;
randomizedorder();
function switchpic(d:MouseEvent){
removeChild (choices [indexcount - 1]);
removeChild (choices [randomize]);
civilizedorder();
randomizedorder();
}
function civilizedorder() {
addChild (choices [indexcount]);
choices [indexcount].x = 300;
indexcount++;
trace (indexcount);
}
trace ("The number of choices in the choice array is " + choices.length);
function randomizedorder(){
randomize = Math.floor(Math.random() * choices.length);
trace ("the random number is" + randomize);
addChild (choices [randomize]);
}
请有人可以解释为什么发生这种情况。据我的分析,好像发生这种情况时indexcount ==随机..我该如何解决这个问题?
你将不得不解释将发生什么,因为它不清楚。 – prototypical 2013-04-25 01:10:08
哦,我的道歉..好吧,我有一个列表,我试图将该列表中的对象添加到阶段..但我需要2个实例随机生成一个,并且按照插入的顺序生成一个。他们都是同一个名单的一部分 – tailedmouse 2013-04-25 01:20:35
你是说你想要每个副本?我仍然不明白你想要什么。 – prototypical 2013-04-25 01:26:50