开发者

moving between states I get an error in Flex

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

so i have a function that runs in state1 and has a

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

I can move to state2, but when I move back to state1 i get the error.

why? the function that has the while loop only runs when something is searched in state1, so why is it running when coming back from state2?

edit...

so i did i test for a function that runs at app initialization:

private function init():void{

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

It shouldnt run one it comes back to state1, but i still get the error. So basically as long as that while loop runs once while in state1 I can never get back to 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;
               开发者_如何学C 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;
            }*/


        }

So this the mainfunction. The top part has the states. main state is loaded first and has the myCanvas canvas. Everything in mainfunction adds to myCanvas.

This is my first time working with flex, so tell me if there is a better way of doing this.Thanks?

I really need to get this solved. I've been stressing over this for weeks.


I understand why you might not use mx:RemoveChild in your state (if you're managing your states in the easier-to-love mxml states property), but it looks like your code is trying to do what removeAllChildren() already does. Can you use removeAllChildren instead? Though, it would be nice to have more code to see what you're trying to accomplish.


Maybe you're handling an enterState event with this code ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜