1

I have problem with this array thing I am doing. U can just plug in the code and run. I need to have 2 things display out of the same array and which ever is picked gets kickedout from the array and stashed into another one.

One of the 2 things that are displaying out is picked at random and the other one goes in order it was put in.

So the logic I applied, or tried applying and is not working very well is..

Once the 2 things display out and if u pick the index count, no numbers change since the index count becomes subtracted by one, so the object after it gets pushed up. but if the random choice is picked the index count moves up by one since it needs to keep moving...

The error i get is this:

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]

This is the Code:

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

It is giving me a new error now. It's called StackOverflow. I am not entirely sure what is going wrong now.

4

2 回答 2

2

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

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

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

我认为您已经在switchpic()方法中从“选择”数组中分割了两次,并且再也没有真正添加到数组中。所以你最终会得到一个空的选择数组。因此错误。

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

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

这是流程,我想:

看起来您正在单击精灵的实例。

然后它调用switchpic(),它执行:

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

然后goNext() 调用Civilizedorder( )

执行:

...
addChild(choices [indexcount]);
...
于 2013-05-02T03:52:31.927 回答
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();
    }
}
于 2013-05-03T16:23:39.537 回答