开发者

RemoveChild and Error 2025

I'd like to remove a child (background) via another class. I can't seem to be able to tar开发者_运维问答get it! It always returns me null or error 2025 and stuff... hehe.

I have the background in my class creationObjets:

package cem{
    import flash.display.Sprite;

    public class creationBackground extends Sprite{

        public function creationBackground() {
            switch(monterJeu._Difficulte){
                case 0:
                    backgroundFacile();
                    break;
                case 1:
                    backgroundMoyen();
                    break;
                case 2:
                    backgroundDifficile();
                    break;
            }
        }
        private function backgroundFacile():void{
            var backgroundStage:Sprite = new Sprite();
            backgroundStage.graphics.beginFill(0x8FCCA8);
            backgroundStage.graphics.moveTo(0,0);
            backgroundStage.graphics.lineTo(750,0);
            backgroundStage.graphics.lineTo(750,450);
            backgroundStage.graphics.lineTo(0,450);
            backgroundStage.graphics.lineTo(0,0);
            backgroundStage.graphics.endFill();

            this.addChild(backgroundStage);
        }
        private function backgroundMoyen():void{
            var backgroundStage:Sprite = new Sprite();
            backgroundStage.graphics.beginFill(0x8F3378);
            backgroundStage.graphics.moveTo(0,0);
            backgroundStage.graphics.lineTo(750,0);
            backgroundStage.graphics.lineTo(750,450);
            backgroundStage.graphics.lineTo(0,450);
            backgroundStage.graphics.lineTo(0,0);
            backgroundStage.graphics.endFill();

            this.addChild(backgroundStage);
        }
        private function backgroundDifficile():void{
            var backgroundStage:Sprite = new Sprite();
            backgroundStage.graphics.beginFill(0x233378);
            backgroundStage.graphics.moveTo(0,0);
            backgroundStage.graphics.lineTo(750,0);
            backgroundStage.graphics.lineTo(750,450);
            backgroundStage.graphics.lineTo(0,450);
            backgroundStage.graphics.lineTo(0,0);
            backgroundStage.graphics.endFill();

            this.addChild(backgroundStage);
        }
    }
}

public static var _creationBackground:creationBackground = new creationBackground();

below, I add it:

addChild(_creationBackground);

then I want to remove it from another class actionObjets! How can I get to my background? I tried

creationObjets._creationBackground.parent.removeChild(creationObjets._creationBackground);

removeChild(creationObjets._creationBackground);

I really have no idea how to access it!


I'm not sure if I understand your problem correctly but:

Remember that in order to remove a child you need access to the child's stage. If your actionObjects class is a Movieclip or sprite it will have a read-only variable that will reference the stage (which may or may not be the same as the stage you added _creationBackground too).

So for instance:

stage.removeChild(_creationBackground);

Should work fine if actionObjets has the same stage as wherever you added _creationBackground.

If actionObjets does not have the same stage or has none at all (maybe it isn't a sprite or movieclip?) You can pass in the stage where _creationBackground was added.

IE:

package {

import flash.display.Stage;

public class actionObjets {

    private var myStage:Stage;

    public function actionObjets(s:Stage) {
        myStage = s;
    }
}

}

and then try:

 myStage.removeChild(_creationBackground);

This is of course assuming that you have access to the _creationBackground clip inside actionObjets.

Not sure if this addressed your problem or not, good luck.


Any of the following should suffice:

creationObjets.removeChild(creationObjet.getChildAt(0));

or

creationObjets.removeChild(creationObjet.getChildByName("creationBackground"));

or

creationObjets.removeChildAt(0);

When you use removeChildAt() or getChildAt() you must specify the display object's(that you want to get or remove) index position. The index position is the position of the display object in the display list of the display object container(I've made the assumption that its 0).

Also when using getChildByName() you must specify the name of the display object that you want to get. Note that you must set the name property of the display object first.

Here's a working example based on you flash app/movie:

package
{
    import cem.CreationObjet;
    import cem.ActionObjet;
    import flash.display.MovieClip;

    public class Main extends MovieClip
    {
        public function Main():void
        {
            init();

        }// end function

        private function init():void
        {
            var creationObjet:CreationObjet = new CreationObjet();
            addChild(creationObjet);

            var actionObjet:ActionObjet = new ActionObjet(creationObjet);

        }// end function

    }// end class

}// end package

In the document class Main, first CreationObjet and ActionObjet is imported. Next an instance of the CreationObjet is declared, instantiated and added to the stage. Lastly an instance of the ActionObjet is declared, instantiated and the instance of CreationObjet is parsed as its only argument.

package cem
{
    import cem.CreationBackground;
    import flash.display.Sprite;

    public class CreationObjet extends Sprite
    {
        private var _creationBackground:CreationBackground;

        public function CreationObjet():void
        {
            _creationBackground = new CreationBackground();
            addChild(_creationBackground);

        }// end function

    }// end class

}// end package

In the CreationObjet class, an instance of CreationBackground is added to the CreationObjet display object.

package cem
{
    import cem.CreationObjet;

    public class ActionObjet
    {
        private var _creationObjet:CreationObjet;

        public function ActionObjet(p_creationObjet:CreationObjet):void
        {
            _creationObjet = p_creationObjet;

            _creationObjet.removeChild(_creationObjet.getChildAt(0));
            // or _creationObjet.removeChild(_creationObjet.getChildByName("creationBackground"));
            // or _creationObjet.removeChildAt(0);

        }// end function

    }// end class

}// end package

Finally in ActionObjet class, the CreationBackground display object is removed from the CreationObjet.

I've had to make a bunch of assumptions about your flash app/movie, but this should give you a general idea of how to implement what I suggested before.

I hoped this helped :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜