在状态之间移动我在Flex中出错

时间:2010-03-24 16:06:25

标签: flex actionscript-3

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.

所以我有一个在state1中运行并具有

的函数
while(myCanvas.rawChildren.numChildren > 0){
 myCanvas.rawChildren.removeChildAt(0); 
}
//this code is definitely the problem....

我可以移动到state2,但是当我回到state1时,我得到了错误。

为什么呢?具有while循环的函数仅在state1中搜索某些内容时运行,那么为什么从state2返回时它会运行?

编辑...

所以我测试了在app初始化时运行的函数:

private function init():void{

    trace(g);
            if(g == true){
                while(myCanvas.rawChildren.numChildren > 0){
                    myCanvas.rawChildren.removeChildAt(0);
                }
                g = false;
            }
            trace(g);
}

它不应该运行一个它回到state1,但我仍然得到错误。所以基本上只要while循环在state1中运行一次,我就永远无法返回到state1

<mx:states>
    <mx:State name="contactform">

        <mx:AddChild position="lastChild">

            <mx:Canvas id="msgCanvas" backgroundColor="0xEEEEEE" height="121" y="158"  width="800" fontWeight="normal" fontSize="12" backgroundAlpha="0.02" x="0">
            </mx:Canvas>

        </mx:AddChild>
        <mx:AddChild position="lastChild">
            <custom:email_cmp x="150" y="287" height="213"/>
        </mx:AddChild>


    </mx:State>
    <mx:State name="main">

        <mx:AddChild position="lastChild">
            <mx:Canvas id="myCanvas" backgroundColor="0xEEEEEE" height="342" y="158"  width="800" fontWeight="normal" fontSize="12" backgroundAlpha="0.02" x="0">
            </mx:Canvas>

        </mx:AddChild>



    </mx:State>
</mx:states>
    public function mainFunction():void{
            origArray = [];
            var cache:int = 0;


        if(myCanvas.rawChildren.numChildren > 0){
            myCanvas.rawChildren.removeChildAt(0);
        }


            //text format for links
            var hFormat:TextFormat = new TextFormat();
                hFormat.font = "Verdana";
                hFormat.color = 0x000000;
                hFormat.size = 15;

                //text format end
            var sprite1:Sprite = new Sprite();
            var textQ:TextField = new TextField();
            textQ.mouseEnabled = false;

            if(amt_cnt.count == 1){
                amt_cnt.end = amt_cnt.endCache;
                amt_cnt.start = amt_cnt.startCache;



                //set the text

                textQ.defaultTextFormat = hFormat;
                textQ.text = "Still can't find your answer? Need more help?";
                textQ.x = 270;
                textQ.y = 300;
                textQ.width = textQ.textWidth +20;
                textQ.selectable = false;





                sprite1.addChild(textQ);
                sprite1.buttonMode = true;

                myCanvas.rawChildren.addChild(sprite1);              

                sprite1.addEventListener(MouseEvent.CLICK, moreHelp);

            }else{

                amt_cnt.end = amt_cnt.endCache;
                amt_cnt.start = amt_cnt.startCache;


                textQ.defaultTextFormat = hFormat;
                textQ.text = "More Questions...";
                textQ.x = 275;
                textQ.y = 300;
                textQ.width = textQ.textWidth +20;
                textQ.selectable = false;



                sprite1.addChild(textQ);
                sprite1.buttonMode = true;
                myCanvas.rawChildren.addChild(sprite1); 

                sprite1.addEventListener(MouseEvent.CLICK, moreQuestions);


            }

            var fontSize:int = 12;
            //text formatting for the displayed question list Begin
            var qFormat:TextFormat = new TextFormat();
                qFormat.font = "Verdana";
                qFormat.color = 0x000000;
                qFormat.size = fontSize;




            //ending text format

            for(var t:uint = amt_cnt.start; t<amt_cnt.end; t++){

                /*if(t == 0){
                    var topQ:TextField = new TextField();

                    topQ.text = full_array[t][1];

                    mainQsprite.addChild(topQ);
                }*/


                var qSprite:Sprite = new Sprite();
                var txt:TextField = new TextField();
                txt.defaultTextFormat = qFormat;

                txt.text = full_array[t][0];
                txt.selectable = false;
                txt.mouseEnabled = false;
                txt.border = false;
                txt.width = 500; // how wide you want the text to go.

                var numLines:Number = Math.floor(txt.textWidth/txt.width)+1;  //calculates number of lines of the textfield.


                txt.height = ((fontSize+8)*numLines);

                txt.wordWrap = true;
                qSprite.x = 30;
                qSprite.y = 350;
                qSprite.alpha = 0;
                var temp_a:Number = cache;  //20 is the padding between questions displayed
                if(t != amt_cnt.end-1){
                    Tweener.addTween(qSprite, {y:temp_a, alpha:1, time:1, delay:t*0.1, transition:"easeoutexpo"}); //tweener INNNNN!
                }else{
                    Tweener.addTween(qSprite, {y:temp_a, alpha:1, time:1, delay:t*0.1, transition:"easeoutexpo", onComplete:runTop}); //tweener INNNNN!
                }

                cache = txt.height + temp_a; 

                qSprite.buttonMode = true;


                origArray[t] = new Array(qSprite.x,temp_a, qSprite);

                mainDict[qSprite] = new Object();

                mainDict[qSprite].question = full_array[t][0];
                mainDict[qSprite].answer = full_array[t][1];
                mainDict[qSprite].count = full_array[t][2];
                mainDict[qSprite].top = full_array[t][3];
                mainDict[qSprite].origX = qSprite.x;
                mainDict[qSprite].origY = temp_a;
                mainDict[qSprite].id = t;
                mainDict[qSprite].height = txt.height;

                amt_cnt[t] = new Object();
                amt_cnt[t].hit = false;

                qSprite.addChild(txt);

                qSprite.addEventListener(MouseEvent.CLICK, clicked);
                qSprite.addEventListener(MouseEvent.MOUSE_OVER, over);
                qSprite.addEventListener(MouseEvent.MOUSE_OUT, out);

                myCanvas.rawChildren.addChild(qSprite);

                if(full_array[t][3] == true){
                    var thereIsTop:Boolean = true;
                }



            }

            amt_cnt.array = origArray;

            /*if(thereIsTop == true){
                topAnswer(); //makes the top answer open first
                thereIsTop = false;
            }*/


        }

所以这是主要功能。顶部有州。首先加载主状态并具有myCanvas画布。 main函数中的所有内容都添加到myCanvas。

这是我第一次使用flex,所以告诉我是否有更好的方法来做这件事。谢谢?

我真的需要解决这个问题。几周以来,我一直在强调这一点。

2 个答案:

答案 0 :(得分:1)

我明白为什么你可能不会在你的状态下使用mx:RemoveChild(如果你在更容易爱的mxml状态属性中管理你的状态),但看起来你的代码试图做什么removeAllChildren()已经做到了。你可以使用removeAllChildren吗?但是,拥有更多代码来查看您想要实现的目标会更好。

答案 1 :(得分:0)

也许您正在使用此代码处理enterState事件?

相关问题