Jrex
Jrex

Reputation: 37

Flash AS3 | Removing or Stopping a Function

I need the answer to what seems like it should be a very obvious question, yet I can't find it anywhere:

How do you stop a function in Flash AS3? For basic example, when I push a button, the function starts working. When I press the button again, the function stops.

More specifically, how can I tailor this to an if/boolean (or similar) statement so that when/while the variable is true, the function starts working. Once the variable goes false, the function stops. When it goes true again, the process repeats.

The code in issue is this panning function that another user helped create for me.

        if(Zoomed == true) {
                MovieClip.y = stage.stageHeight / 2;
                MovieClip.x = stage.stageWidth / 2;
                addEventListener(MouseEvent.MOUSE_MOVE, panFunction);
                function panFunction(me: MouseEvent): void {
                    MovieClip.x = stage.stageWidth / 2 - (stage.mouseX - stage.stageWidth / 2);
                    MovieClip.y = stage.stageHeight / 2 - (stage.mouseY - stage.stageHeight / 2);
                }

}

As you can see, I want it so that when the boolean function 'Zoomed' is true, the function activates and the panning feature works. The problem is that when I go back to Zoomed == false, the function is still running. I am looking to find out how to stop this from happening.

I have tried a 'while' loop but it ultimately just freezes the animation (likely because its a moving image and it's trying to render every frame with this code).

Summarising: I need a way for a function to be able to be toggled on and off. Alternatively, being able to add it and remove it when it's needed/not needed should work the same. Any help on this would be a great help, thanks!

Upvotes: 1

Views: 624

Answers (1)

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

What you need to do is remove the event listener when you no longer want panning.

Something like this, which assumes you change the value of zoomed on a click:

var zoomed:Boolean = false; //a var to store whether we are currently zoomed

//change the zoom on mouse click
addEventListener(MouseEvent.CLICK, zoomClick);

function panFunction(me:MouseEvent): void {
    mc.x = stage.stageWidth / 2 - (stage.mouseX - stage.stageWidth / 2);
    mc.y = stage.stageHeight / 2 - (stage.mouseY - stage.stageHeight / 2);
}

function zoomClick(me:MouseEvent):void {
    zoomed = !zoomed; //toggle the zoomed value

    if(zoomed){
        //we're zoomed, so add the panning mouse move listener
        addEventListener(MouseEvent.MOUSE_MOVE, panFunction);
        //do whatever else you need to when first zoomed.
    }else{
        //no longer zoomed, remove the mouse move listener
        removeEventListener(MouseEvent.MOUSE_MOVE, panFunction);
    }
}

Some notes, the word MovieClip is not a good instance name, as it may conflict the class name MovieClip, I'd recommend renaming it mc or something else to avoid potential problems later on.

Also, avoid inline functions (functions declared/contained within other functions), they can be especially problematical when attached to event listeners and are a good way to introduce memory leaks into your program.

Upvotes: 3

Related Questions