0

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

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

4

2 回答 2

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实例的函数,但我建议使用每个类型的类或基于某种标识符返回实例的函数。

于 2013-04-25T02:42:32.910 回答
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,以便更容易看到发生了什么。

于 2013-04-25T01:55:13.457 回答