#1023 StackOverflow错误

时间:2013-05-02 01:22:40

标签: arrays actionscript-3

我正在做这个阵列的问题。你可以插入代码然后运行。 我需要在同一个数组中显示两个东西,然后挑选的东西从数组中被踢出并存入另一个。

显示的两件事中的一件是随机挑选的,另一件是按顺序放入的。

所以我应用的逻辑,或者尝试应用并且工作效果不佳的是..

一旦显示出两件事,如果您选择索引计数,则由于索引计数减去一,因此没有数字更改,因此对象在被推高之后。  但如果选择随机选择,则索引计数会向上移动一,因为它需要继续移动...

我得到的错误是:

TypeError: Error #2007: Parameter child must be non-null.
    at flash.display::DisplayObjectContainer/addChild()
    at Level3Torture_fla::MainTimeline/civilizedorder()[Level3Torture_fla.MainTimeline::frame1:87]
    at Level3Torture_fla::MainTimeline/goNext()[Level3Torture_fla.MainTimeline::frame1:114]
    at Level3Torture_fla::MainTimeline/switchpic()[Level3Torture_fla.MainTimeline::frame1:79]

这是代码:

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 ();

//Sprite Creation
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;

//adding sprites into array
choices.push( eating_breakfast);
choices.push(walking);
choices.push(swimming);
choices.push(art);


var indexcount = 0;
var randomize:Number;
var storageArray: Array = new Array ();
civilizedorder();
randomizedorder();
this.addEventListener(MouseEvent.CLICK,switchpic);

//pick the target generated object
function switchpic(t:MouseEvent)
{
    //for index count
    if (t.target == choices[indexcount])
    {
        storageArray.push(choices[indexcount]);
        removeChild(choices [indexcount]);
        removeChild(choices [randomize]);
        choices.splice(indexcount,1);
        goNext();

    };
    // for randomize
if (t.target == choices[randomize])
{
    storageArray.push(choices[randomize]);
    removeChild(choices [indexcount]);
    removeChild(choices [randomize]);
    choices.splice(randomize,1);
    indexcount++;
    trace("The Index count is" + indexcount);
    goNext();
}
}

//generates the index count object
function civilizedorder()
{

addChild(choices [indexcount]);
choices[indexcount].x = 300;


}
trace("The number of choices in the choice array is " + choices.length);
//generates the randomized object
function randomizedorder()
{

randomize = Math.floor(Math.random() * choices.length);
trace("the random number is" + randomize);
if (randomize == indexcount )
{
    randomizedorder();
}
else
{
    addChild(choices [randomize]);
}

}
//EDIT

    function goNext()
{
    trace("The storagearray has " + (storageArray.length));
    if (choices.length < 0 || choices.length > 0)
    {
        if (indexcount > choices.length-1)
        {
            indexcount = choices.length - 1;
        }
        civilizedorder();
        randomizedorder();
    }
}

现在它给了我一个新的错误。它叫做StackOverflow。我现在还不完全确定会出现什么问题。

2 个答案:

答案 0 :(得分:2)

编辑:要添加条件并检查是否超出此阵列中添加子项的范围,请尝试以下操作:

if(indexcount <= choices.length){
  addChild(choices [indexcount]);
}

尝试在 switchpic()方法中注释掉拼接。那或者将这些值/精灵实例重新添加到数组中。

我认为您已经在 switchpic()方法中从“选择”数组切两次,并且从未实际再次添加到数组。所以你最终会得到一个空的选择数组。因此错误。

在第二个条件if (t.target == choices[randomize])中,您增加 indexcount ,然后调用 goNext(),它会将randomize值重新生成为不等于indexcount,但是还尝试重新添加子精灵。

这可能导致4个项目的数组变为2个项目,然后,可能是randomize = 0,indexcount = 1.在第二个传递中,您可能有一个0项的数组,randomize = 0,indexcount = 1和发生的错误。

以下是流程,我想:

看起来你正在点击精灵的一个实例。

然后调用 switchpic(),执行:

...
choices.splice(indexcount,1);
...

然后 goNext() 它叫 civilizedorder()

执行:

...
addChild(choices [indexcount]);
...

答案 1 :(得分:1)

你应该检查indexcount永远不会超出0-3范围。 在函数goNext()中,按如下方式更改代码:

function goNext()
{
    trace("The storagearray has " + (storageArray.length));
    if(choices.length <> 0)
    {
        if(indexcount > choices.length-1)    
            indexcount = choices.length-1;
        civilizedorder();
        randomizedorder();
    }
}