删除子项和错误 2025

发布于 2024-10-07 12:53:30 字数 2656 浏览 1 评论 0原文


我想通过另一个类删除一个孩子(背景)。我似乎无法瞄准它!它总是返回 null 或错误 2025 之类的东西......呵呵。

我的类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();

下面,我添加它:

addChild(_creationBackground);

然后我想从另一个类actionObjets中删除它!我怎样才能了解我的背景? 我尝试了

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

removeChild(creationObjets._creationBackground);

我真的不知道如何访问它!

I'd like to remove a child (background) via another class. I can't seem to be able to target 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

在梵高的星空下 2024-10-14 12:53:30

我不确定我是否正确理解您的问题,但是:

请记住,为了删除孩子,您需要访问孩子的舞台。如果您的 actionObjects 类是 Movieclip 或 sprite,它将有一个引用舞台的只读变量(该变量可能与您添加 _creationBackground 的舞台相同,也可能不同)。

例如:

stage.removeChild(_creationBackground);

如果 actionObjets 与您添加 _creationBackground 的位置具有相同的阶段,则应该可以正常工作。

如果actionObjets没有相同的stage或根本没有(也许它不是sprite或movieclip?),您可以传入添加了_creationBackground的stage。

IE:

package {

import flash.display.Stage;

public class actionObjets {

    private var myStage:Stage;

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

}

然后尝试:

 myStage.removeChild(_creationBackground);

这当然是假设您有权访问 actionObjets 内的 _creationBackground 剪辑。

不确定这是否解决了您的问题,
祝你好运。

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.

仙女 2024-10-14 12:53:30

以下任何一项都应该足够:

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

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

creationObjets.removeChildAt(0);

当您使用removeChildAt() 或getChildAt() 时,您必须指定显示对象的(您想要获取或删除的)索引位置。索引位置是显示对象在显示对象容器的显示列表中的位置(我假设它是0)。

此外,当使用 getChildByName() 时,您必须指定要获取的显示对象的名称。请注意,您必须首先设置显示对象的 name 属性。

下面是一个基于 Flash 应用程序/电影的工作示例:

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

在文档类 Main 中,首先导入 CreationObjet 和 ActionObjet。接下来,声明、实例化 CreationObjet 的实例并将其添加到舞台中。最后声明并实例化 ActionObjet 的实例,并将 CreationObjet 的实例解析为其唯一参数。

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

在CreationObjet 类中,CreationBackground 的实例被添加到CreationObjet 显示对象中。

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

最后在 ActionObjet 类中,CreationBackground 显示对象从 CreationObjet 中删除。

我不得不对您的 Flash 应用程序/电影做出一系列假设,但这应该能让您大致了解如何实现我之前建议的内容。

我希望这有帮助:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文