Search code examples
actionscript-3

as3 check if an object is removed from the stage


this is the first time I'm posting something on this forum, so if I am doing something wrong please tell me. I am trying to make a game with as3. In the game you see a word which represents a color. And when you click the block disapears. The question is how do I check if the block is on stage or not. Because although I click on the block and therefor remove it the boolean doesn't seem to become true. Thanks in advance

here's all of my code, so I made 8 blocks all with a word in it(which represents a color). And what I want is that a word which is randomely created corresponds with those words in the blocks and if you click on the blocks with the right word you win. So the question is mainly about he piece that is commented out

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.events.MouseEvent;
    import flash.utils.Timer;
    import flash.events.TimerEvent; 

    [SWF(backgroundColor = "0x000000")]

    public class Main extends Sprite 
    {
        public var blok:blokken = new blokken();
        public var blok2:blokken = new blokken();
        public var blok3:blokken = new blokken();
        public var blok4:blokken = new blokken();
        public var blok5:blokken = new blokken();
        public var blok6:blokken = new blokken();
        public var blok7:blokken = new blokken();
        public var blok8:blokken = new blokken();
        public var kleuren:Array = new Array("rood", "blauw", "geel", "groen");
        public var kleur:int = Math.floor(Math.random() * kleuren.length);
        public var tekstKleur:TextField = new TextField;
        public var grootheidswaanzin:TextFormat = new TextFormat();
        public var aanwezig:Boolean = true;
        public var tijdje:Timer = new Timer(15000,1);
        public var eindTekst:TextField = new TextField;

        public function Main() 
        {
            grootheidswaanzin.size = 25;

            tekstKleur.defaultTextFormat = grootheidswaanzin;
            tekstKleur.text = kleuren[kleur];
            tekstKleur.x = 415;       
            tekstKleur.y = 80;
            tekstKleur.textColor = 0xFFFFFF;
            addChild(tekstKleur);           

            eindTekst.x = 415;
            eindTekst.y = 200;
            eindTekst.textColor = 0xFFFFFF;
            eindTekst.text = "je faalt";
            eindTekst.defaultTextFormat = grootheidswaanzin;

            blok.x = 315;
            blok.y = 150;
            blok.naamTekst("rood");
            addChild(blok);

            blok2.x = 440;
            blok2.y = 150;
            blok2.naamTekst("rood");
            addChild(blok2);

            blok3.x = 315;
            blok3.y = 235;
            blok3.naamTekst("blauw");
            addChild(blok3);

            blok4.x = 440;
            blok4.y = 235;
            blok4.naamTekst("blauw");
            addChild(blok4);

            blok5.x = 315;
            blok5.y = 320;
            blok5.naamTekst("geel");
            addChild(blok5);

            blok6.x = 440;
            blok6.y = 320;
            blok6.naamTekst("geel");
            addChild(blok6);

            blok7.x = 315;
            blok7.y = 405;
            blok7.naamTekst("groen");
            addChild(blok7);

            blok8.x = 440;
            blok8.y = 405;
            blok8.naamTekst("groen");
            addChild(blok8);

            tijdje.addEventListener(TimerEvent.TIMER, vijfSecondenRegel);
            tijdje.start();
            /*if (blok.stage || blok2.stage) 
            {
                aanwezig = false;
            }else 
            {
                aanwezig = true;
                trace (aanwezig);
            }
            trace (aanwezig);

            if (kleuren[kleur] == "rood" && aanwezig == true) 
            {
                trace ("victory");
            }*/
        }

        public function vijfSecondenRegel(e:TimerEvent):void
        {
            removeEventListener(TimerEvent.TIMER,vijfSecondenRegel);

            removeChild(blok);
            removeChild(blok2);
            removeChild(blok3);
            removeChild(blok4);
            removeChild(blok5);
            removeChild(blok6);
            removeChild(blok7);
            removeChild(blok8);
            removeChild(tekstKleur);
            addChild(eindTekst);
        }
    }

}



package 
{
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.events.MouseEvent;
    import flash.text.TextFormat;

    public class blokken extends Sprite 
    {
        public var blokAchtergrond:Sprite = new Sprite;
        public var tekstInBlok: TextField = new TextField;
        public var TekstGrootte:TextFormat = new TextFormat();


        public function blokken()
        {
            blokAchtergrond.graphics.beginFill(Math.random() * 0xFFFFFF);
            blokAchtergrond.graphics.drawRect(0, 0, 120, 80);
            addChild(blokAchtergrond);

            TekstGrootte.size = 25; 
            tekstInBlok.defaultTextFormat = TekstGrootte;
            tekstInBlok.width = 120;
            tekstInBlok.height = 40;
            tekstInBlok.y = 40;
            tekstInBlok.x = 30;
            addChild(tekstInBlok);

            addEventListener(MouseEvent.MOUSE_DOWN, klikjes);
        }

        public function naamTekst(label:String):void 
        {
            tekstInBlok.text = label;
        }

        public function klikjes(e:MouseEvent):void 
        {
            removeChild(blokAchtergrond);
            removeChild(tekstInBlok);

        }
    }

}

Solution

  • Your issue is that you are setting the aanwezig var in the constructor of your class (which runs just once and before any of the display tree stuff is even initialized). So the value will always be true if you don't set the var again later.

    For the boolean var to change it's value, you have to have code that updates it

    So, at the same time you removed the block, you'd have to update that boolean:

    removeChild(blok);
    aanwezig = false; //or however you compute the value
    

    Perhaps the behavior you'd like to do, is instead of a normal variable (which has to be explicitly updated if you want the value to change), create a getter function: (getters/setters acts like a variable, but use a function to compute the return value every time it's accessed)

    public function get aanwezig():Boolean {
        return !blok.stage && !blok2.stage 
    }
    

    Then when you do: trace(aanwezig) it will return true if neither of those objects are on the display list.

    Additionally, it looks like in your blokken class, you're just removing children of the block, not the block itself (so block2.stage will actually still be true because it's still on the display list as you only removed its children).

    Instead of doing:

    removeChild(blokAchtergrond);
    removeChild(tekstInBlok);
    

    Do this (remove itself from its parent)

    if(parent){
        parent.removeChild(this);
    }