数组重复不起作用

时间:2013-04-25 00:45:56

标签: actionscript-3 flash

我正在尝试在数组中创建一个随机化实例和一个正确排序的对象实例,但由于某种原因它无法正常工作。 这是它的代码:

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时,似乎会发生这种情况..我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

看起来你有几个问题。 1你永远不会重置你的indexcount数字,这导致它尝试访问你选择数组长度的长度值(即你允许indexcount大于choices.length)。 如果你试图两次添加艺术,你也正确地推测你,你在舞台上只有1个实例。但是,当您去除精灵时,您不会检查它是否存在或应该存在。

eating_breakfast = new Sprite ();
eating_breakfast.name = "eating_breakfast";
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.name = "walking";
walking.graphics.beginFill(0xC3266C);
walking.graphics.drawRect(0,0,50,50);
walking.graphics.endFill();
walking.x = 100;
walking.y = 100;


swimming = new Sprite ();
swimming.name = "swimming";
swimming.graphics.beginFill(0x48AFD1);
swimming.graphics.drawRect(0,0,50,50);
swimming.graphics.endFill();
swimming.x = 150;
swimming.y = 150;


art = new Sprite ();
art.name = "art";
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 = -1;
var randomize:Number;
civilizedorder();
randomizedorder();

function switchpic(d:MouseEvent) {
    removeChild(choices[indexcount]);
    if(choices[indexcount].name != choices[randomize].name){
        removeChild(choices[randomize]);
    }
    civilizedorder();
    randomizedorder();
}    

function civilizedorder() {
    indexcount++;
    if(indexcount == choices.length-1){
        indexcount = 0;
    }
    trace("adding " + choices[indexcount].name);
    addChild(choices[indexcount]);
    choices[indexcount].x = 300;
}
trace("The number of choices in the choice array is " + choices.length);

function randomizedorder() {
    randomize = Math.floor(Math.random() * choices.length);
    trace("adding " + choices[randomize].name);
    addChild(choices[randomize]);

}

抱歉,我还在精灵上添加了.name,以便更容易看到正在发生的事情。

答案 1 :(得分:0)

您需要做的是将实例的创建包装在类或函数中。

public var choices:Array;

function Main()
{
    choices = [eatingBreakfast, walking];

    var randomIndex:int = Math.random() * choices.length;
    var choice:Function = choices[randomIndex] as Function;
    var yourSprite:Sprite = choice();

}



function eatingBreakfast():Sprite
{
    var instance:Sprite =  new Sprite;
    instance.graphics.beginFill(0xE39D43);
    instance.graphics.drawRect(0,0,50,50);
    instance.graphics.endFill();
    instance.x = 50;
    instance.y = 50;
    return instance;
}


function walking():Sprite
{
    var instance:Sprite =  new Sprite;
    instance.graphics.beginFill(0xE39D43);
    instance.graphics.drawRect(0,0,50,50);
    instance.graphics.endFill();
    instance.x = 50;
    instance.y = 50;
    return instance;
}

此示例使用的函数返回Sprite实例,但我建议使用每种类型的类或基于某种标识符返回实例的函数。

相关问题