Skip to main content
Inspiring
February 21, 2014
Question

Using a function and array to remove stage instances

  • February 21, 2014
  • 1 reply
  • 871 views

I'm trying to figure out the best way to handle this.

I have an array of items I want to be looked through and removed if a specific function is running and if the stage contains them.

removalArray = [axScene, bridgeScene];

...

public function Removal(event:Event) {

            trace("cheese");

            for (var i:uint = 0; i>removalArray.length; i++) {

                if (stage.contains(removalArray)) {

                    removeChild(removalArray);

                    removeEventListener(Event.ENTER_FRAME, Removal);

                    addEventListener(Event.ENTER_FRAME, CampScene);

                } else {

                    removeEventListener(Event.ENTER_FRAME, Removal);

                    addEventListener(Event.ENTER_FRAME, CampScene);/**/

                }

            }

}

...

addEventListener(Event.ENTER_FRAME, Removal);

I'm able to get trace("cheese"); to work at its current position, but the function does not seem to like my "for" and "if" statements because putting trace("cheese") amongst them does not work.

As always, help is appreciated, and let me know if you need more info.

EDIT: Perhaps I should also note I am able to get

if (stage.contains(axScene)) {

                removeChild(axScene);

               

                } else if (stage.contains(bridgeScene)) {

                    removeChild(bridgeScene);

                    } else {

                        null;

}

to work correctly in that function.

EDIT2: Would this have something to do with addChild only being able to remove one instance at a time? I can't imagine that's it since this suppose to be run through a loop.

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
February 21, 2014

You appear to have an unhealthy obsession with using the ENTER_FRAME event.  What you might try doing instead of creating all manners of them is to have just one and within that one you manage your objects using conditionals.

That ENTER_FRAME listener is being removed as fast as it was added, whether or not there is something in the array to remove.  If that is the intention, you do not need the ENTER_FRAME listener to call that function.

When you are removing objects using an array and a loop you need to work from the other end back to 0, otherwise you end up skipping every other one.... you remove an object and whoever is after it takes over its spot.

tytbone2Author
Inspiring
February 21, 2014

You appear to have an unhealthy obsession with using the ENTER_FRAME event. 

I'm sure you're right, but realize that I'm learning by the seat of my pants and am not great with logic and stuff that doesn't happen chronological. (I'm 26, and graduated with a degree in graphic design.) If I find something that works I'm going to keep using that until I find a better way, and thus far ENTER_FRAME has been all I've had. (I can find tons of AS3 information online, but a lot of it is forum posts seems to be lacking useful context for my purposes.)

For what it's worth (if you remember another post I did that had a ton of event listeners), I've started using more booleans and gotten rid of a lot of listeners adding and removing.

What you might try doing instead of creating all manners of them is to have just one and within that one you manage your objects using conditionals.

So something like this?

if (campSceneOnStage == true) {

     addChild(campScene)

     } else if (campSceneOnStage == false) {

          removeChild(campScene);

     }

}

EDIT: Although this would keep adding a campScene instance, wouldn't it?

EDIT2: Could I write something like this, have one constant event listener (addEventListener(Event.ENTER_FRAME, Scenes)...

public function Scenes(event:Event) {

            if (campSceneOn == true) {

                addChild(campScene);

                if (stage.contains(campScene)) {

                   .................

                }

        }

}

and use a line to tell the function to temporarily stop listening if campSceneOn = true and campScene is on the stage?

Would "null" work?

Also, is there an equivalent to "stage.contains" that looks to see if the stage does not contain?

That ENTER_FRAME listener is being removed as fast as it was added, whether or not there is something in the array to remove.

So the even though the intention is to only have the removeEventListener only occur if something is found in the array to remove, the script "skips over" that and removes the event listener even if it doesn't "use" the for loop or if statements?

tytbone2Author
Inspiring
February 22, 2014

Okay, I may have found a better workaround with your Boolean suggestion.

public function SceneChange(event:Event) {

        

            if (campScene.guyAtCampScene.currentLabel == "guyGoingToAxDone") {

                axSceneUp = true;

            }

        }

       

public function SceneCreation(event:Event) {

           if (axSceneUp == true) {

                addChild(axScene);

                campSceneUp = false;

           }

               

            if (campSceneUp == true) {

                addChild(campScene);

               axSceneUp = false;

                } else if (campSceneUp == false) {

                    if (stage.contains(campScene)) {

                        removeChild(campScene);

                    }

                }

       

        }