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

いくつか問題があるようです。1 決して indexcount 番号をリセットしていないため、choices 配列の長さの範囲外の値にアクセスしようとしています (つまり、indexcount をchoices.length よりも大きくすることを許可しています)。また、アートを 2 回追加しようとした場合、ステージ上にインスタンスが 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 に答える