0
votes

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?

1

1 Answers

0
votes

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.