as3删除子错误

发布于 2024-11-17 04:25:56 字数 5327 浏览 2 评论 0原文

这是我的类

Main - 创建预加载器 预加载器 - createas 网站 创建 MainMenu

网站 -当单击主菜单中的按钮时 ,它会创建一个对象。当单击另一个对象时,需要删除该对象。拆除部分有点乱。

package src 
{
import com.greensock.TweenLite;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.geom.Point;
/**
$(CBI)* ...
$(CBI)* @author servet
$(CBI)*/
public class MainMenu extends MovieClip
{
    public var filmlerButton:MovieClip;
    public var filmleralt:FilmAltButtonlar;

    private var ANASAYFA_ORIGINAL_Y:Number;
    private var HAKKIMIZDA_ORIGINAL_Y:Number;
    private var FILMLER_ORIGINAL_Y:Number;
    private var DAGITIM_ORIGINAL_Y:Number;

    private var state:String;

    public function MainMenu() 
    {
        init();
    }

    private function init():void 
    {
        repos();
        assignOriginalHeights();
        state = " ";

        filmlerButton.filmlerhitarea.alpha = 0;

        anasayfaButton.addEventListener(MouseEvent.CLICK, onAnasayfaClick);
        hakkimizdaButton.addEventListener(MouseEvent.CLICK, onhakkimizdaClick);
        filmlerButton.filmlerhitarea.addEventListener(MouseEvent.CLICK, onfilmlerClick);
        dagitimButton.addEventListener(MouseEvent.CLICK, ondagitimClick);
    }

    private function assignOriginalHeights():void 
    {
        ANASAYFA_ORIGINAL_Y = anasayfaButton.y;
        HAKKIMIZDA_ORIGINAL_Y = hakkimizdaButton.y;
        FILMLER_ORIGINAL_Y = filmlerButton.y;
        DAGITIM_ORIGINAL_Y = dagitimButton.y;

        //trace(ANASAYFA_ORIGINAL_Y, HAKKIMIZDA_ORIGINAL_Y, FILMLER_ORIGINAL_Y, DAGITIM_ORIGINAL_Y);
    }

    private function ondagitimClick(e:MouseEvent):void 
    {
        if (state != "anasayfa"&&state!="hakkimizda"&&state!="filmler"&&state!="dagitim") {
            state = "dagitim";
            traceState();

            dagitimButton.gotoAndStop(2);
        }else {
            state = " ";
            traceState();

            closeAllMenus();
        }
    }

    private function onfilmlerClick(e:MouseEvent):void 
    {
        if (state != "anasayfa"&&state!="hakkimizda"&&state!="filmler"&&state!="dagitim") {
            closeAllMenus();
            state = "filmler";
            traceState();

            filmlerButton.gotoAndStop(2);

            filmleralt = new FilmAltButtonlar();
            addChild(filmleralt);
            filmleralt.name = "trouble";
            //OBJECT ADDED HERE

            TweenLite.to(dagitimButton, 1, {y:DAGITIM_ORIGINAL_Y+400 } );
        }else {
            state = " ";
            traceState();

            closeAllMenus();
        }
    }

    private function onhakkimizdaClick(e:MouseEvent):void 
    {
        if (state != "anasayfa"&&state!="hakkimizda"&&state!="filmler"&&state!="dagitim") {
            closeAllMenus();
            state = "hakkimizda";
            traceState();

            hakkimizdaButton.gotoAndStop(2);

            TweenLite.to(filmlerButton, 1, {y:FILMLER_ORIGINAL_Y+200 } );
            TweenLite.to(dagitimButton, 1, {y:DAGITIM_ORIGINAL_Y+200 } );
        }else {
            state = " ";
            traceState();

            closeAllMenus();
        }
    }

    private function onAnasayfaClick(e:MouseEvent):void             //&& girecek
    {
        if (state != "anasayfa"&&state!="hakkimizda"&&state!="filmler"&&state!="dagitim") {
            closeAllMenus();
            state = "anasayfa";
            traceState();

            anasayfaButton.gotoAndStop(2);

            TweenLite.to(hakkimizdaButton, 1, {y:hakkimizdaButton.y+200 } );
            TweenLite.to(filmlerButton, 1, {y:filmlerButton.y+200 } );
            TweenLite.to(dagitimButton, 1, {y:dagitimButton.y+200 } );
        }else {
            state = " ";
            traceState();

            closeAllMenus();
        }
    }

    private function closeAllMenus() {
        TweenLite.to(hakkimizdaButton, 1, {y:HAKKIMIZDA_ORIGINAL_Y } );
        TweenLite.to(filmlerButton, 1, {y:FILMLER_ORIGINAL_Y } );
        TweenLite.to(dagitimButton, 1, { y:DAGITIM_ORIGINAL_Y } );

        anasayfaButton.gotoAndStop(1);
        hakkimizdaButton.gotoAndStop(1);
        filmlerButton.gotoAndStop(1);
        dagitimButton.gotoAndStop(1);

        //NEED TO REMOVE OBJECT HERE
        //BUT THIS FUNCTION GETS CALLED A LOT 
        //SO I NEED TO CHECK IF THE OBJECT EXISTS 
        //BEFORE TRYING TO REMOVE TO AVOID AN NULL
        //OBJECT REFERENCE SO I DO:
        if (getChildByName(filmleralt.name) != null) {
            removeFilmleralt();
        }

    }

    private function removeFilmleralt() {
        Main.instance.narfilm.mainmenu.removeChild(filmleralt);
    }

    private function traceState() {
        trace("current state is: "+state);
    }

    public function repos():void                //call on stage resize so it aligns to left babs
    {
        x = Main.instance.canvaswidth;
    }

}
}

重要部分:

    //NEED TO REMOVE OBJECT HERE
    //BUT THIS FUNCTION GETS CALLED A LOT 
    //SO I NEED TO CHECK IF THE OBJECT EXISTS 
    //BEFORE TRYING TO REMOVE TO AVOID AN NULL
    //OBJECT REFERENCE SO I DO:
    if (getChildByName(filmleralt.name) != null) {
        removeFilmleralt();
    }

这是我收到空对象引用错误的部分。

我缺少什么?

here are my classes

Main - creates preloader
Preloader - createas Website
Website - creates MainMenu

when a button in the main menu is clicked, it creates an object. And when another one is clicked, that object needs to be removed. The removing part got a little bit messy.

package src 
{
import com.greensock.TweenLite;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.geom.Point;
/**
$(CBI)* ...
$(CBI)* @author servet
$(CBI)*/
public class MainMenu extends MovieClip
{
    public var filmlerButton:MovieClip;
    public var filmleralt:FilmAltButtonlar;

    private var ANASAYFA_ORIGINAL_Y:Number;
    private var HAKKIMIZDA_ORIGINAL_Y:Number;
    private var FILMLER_ORIGINAL_Y:Number;
    private var DAGITIM_ORIGINAL_Y:Number;

    private var state:String;

    public function MainMenu() 
    {
        init();
    }

    private function init():void 
    {
        repos();
        assignOriginalHeights();
        state = " ";

        filmlerButton.filmlerhitarea.alpha = 0;

        anasayfaButton.addEventListener(MouseEvent.CLICK, onAnasayfaClick);
        hakkimizdaButton.addEventListener(MouseEvent.CLICK, onhakkimizdaClick);
        filmlerButton.filmlerhitarea.addEventListener(MouseEvent.CLICK, onfilmlerClick);
        dagitimButton.addEventListener(MouseEvent.CLICK, ondagitimClick);
    }

    private function assignOriginalHeights():void 
    {
        ANASAYFA_ORIGINAL_Y = anasayfaButton.y;
        HAKKIMIZDA_ORIGINAL_Y = hakkimizdaButton.y;
        FILMLER_ORIGINAL_Y = filmlerButton.y;
        DAGITIM_ORIGINAL_Y = dagitimButton.y;

        //trace(ANASAYFA_ORIGINAL_Y, HAKKIMIZDA_ORIGINAL_Y, FILMLER_ORIGINAL_Y, DAGITIM_ORIGINAL_Y);
    }

    private function ondagitimClick(e:MouseEvent):void 
    {
        if (state != "anasayfa"&&state!="hakkimizda"&&state!="filmler"&&state!="dagitim") {
            state = "dagitim";
            traceState();

            dagitimButton.gotoAndStop(2);
        }else {
            state = " ";
            traceState();

            closeAllMenus();
        }
    }

    private function onfilmlerClick(e:MouseEvent):void 
    {
        if (state != "anasayfa"&&state!="hakkimizda"&&state!="filmler"&&state!="dagitim") {
            closeAllMenus();
            state = "filmler";
            traceState();

            filmlerButton.gotoAndStop(2);

            filmleralt = new FilmAltButtonlar();
            addChild(filmleralt);
            filmleralt.name = "trouble";
            //OBJECT ADDED HERE

            TweenLite.to(dagitimButton, 1, {y:DAGITIM_ORIGINAL_Y+400 } );
        }else {
            state = " ";
            traceState();

            closeAllMenus();
        }
    }

    private function onhakkimizdaClick(e:MouseEvent):void 
    {
        if (state != "anasayfa"&&state!="hakkimizda"&&state!="filmler"&&state!="dagitim") {
            closeAllMenus();
            state = "hakkimizda";
            traceState();

            hakkimizdaButton.gotoAndStop(2);

            TweenLite.to(filmlerButton, 1, {y:FILMLER_ORIGINAL_Y+200 } );
            TweenLite.to(dagitimButton, 1, {y:DAGITIM_ORIGINAL_Y+200 } );
        }else {
            state = " ";
            traceState();

            closeAllMenus();
        }
    }

    private function onAnasayfaClick(e:MouseEvent):void             //&& girecek
    {
        if (state != "anasayfa"&&state!="hakkimizda"&&state!="filmler"&&state!="dagitim") {
            closeAllMenus();
            state = "anasayfa";
            traceState();

            anasayfaButton.gotoAndStop(2);

            TweenLite.to(hakkimizdaButton, 1, {y:hakkimizdaButton.y+200 } );
            TweenLite.to(filmlerButton, 1, {y:filmlerButton.y+200 } );
            TweenLite.to(dagitimButton, 1, {y:dagitimButton.y+200 } );
        }else {
            state = " ";
            traceState();

            closeAllMenus();
        }
    }

    private function closeAllMenus() {
        TweenLite.to(hakkimizdaButton, 1, {y:HAKKIMIZDA_ORIGINAL_Y } );
        TweenLite.to(filmlerButton, 1, {y:FILMLER_ORIGINAL_Y } );
        TweenLite.to(dagitimButton, 1, { y:DAGITIM_ORIGINAL_Y } );

        anasayfaButton.gotoAndStop(1);
        hakkimizdaButton.gotoAndStop(1);
        filmlerButton.gotoAndStop(1);
        dagitimButton.gotoAndStop(1);

        //NEED TO REMOVE OBJECT HERE
        //BUT THIS FUNCTION GETS CALLED A LOT 
        //SO I NEED TO CHECK IF THE OBJECT EXISTS 
        //BEFORE TRYING TO REMOVE TO AVOID AN NULL
        //OBJECT REFERENCE SO I DO:
        if (getChildByName(filmleralt.name) != null) {
            removeFilmleralt();
        }

    }

    private function removeFilmleralt() {
        Main.instance.narfilm.mainmenu.removeChild(filmleralt);
    }

    private function traceState() {
        trace("current state is: "+state);
    }

    public function repos():void                //call on stage resize so it aligns to left babs
    {
        x = Main.instance.canvaswidth;
    }

}
}

Important part:

    //NEED TO REMOVE OBJECT HERE
    //BUT THIS FUNCTION GETS CALLED A LOT 
    //SO I NEED TO CHECK IF THE OBJECT EXISTS 
    //BEFORE TRYING TO REMOVE TO AVOID AN NULL
    //OBJECT REFERENCE SO I DO:
    if (getChildByName(filmleralt.name) != null) {
        removeFilmleralt();
    }

this is the part where I get the null object reference error.

what am I missing?

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

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

发布评论

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

评论(1

猥琐帝 2024-11-24 04:25:56

嗯,我猜实际的错误发生在函数removeFilmleralt() 中。然后,您尝试删除与您不需要的上述函数相同的类中初始化的实例(并且具有 filmleralt 的范围):

Main.instance.narfilm.mainmenu.removeChild(filmleralt);

但只需 removeChild(filmleralt); 即可完成这项工作。我猜测“Main”对象实际上导致了错误,该对象没有在任何地方初始化并且没有范围。

尝试在设置中使用“允许调试”,这样您就可以准确地知道哪一行发生了错误。但不要忘记在释放时禁用此选项,因为垃圾收集。

Well i'm guessing actual error happens in function removeFilmleralt(). And sicne you try to remove instance which is initialized (and have scope to filmleralt) in same class as mentioned function u don't need:

Main.instance.narfilm.mainmenu.removeChild(filmleralt);

but simply removeChild(filmleralt); will do the job. I'm guessing "Main" object actually causing the error, which is not initialized anywhere and don't have scope to it.

Try to use "permit debugging" in settings, that way you'll know exactly in which line error occurred. But don't forget to disable this option when go release, because of garbage collection.

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