我正在尝试在数组中创建一个随机实例和一个正确排序的对象实例,但由于某种原因它无法正常工作。这是它的代码:
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 == randomize 时似乎会发生这种情况..我该如何解决这个问题?