简体   繁体   中英

How to remove a function or event stop it from stage.addEventListener in as3

I´m trying to make object follow the mouse in as3. My wish is when I roll over a movieclip(btn1) I want the function that make object follow the mouse(my_object) stop until I roll out of it. HERE IS THE SCRIPT:

btn1.addEventListener(MouseEvent.ROLL_OVER, JD);

function JD(event:MouseEvent):void{
stage.removeEventListener(Event.ENTER_FRAME, follow_me);

}

btn1.addEventListener(MouseEvent.ROLL_OUT, kk);

function kk(event:MouseEvent):void{
play();
}

stage.addEventListener(Event.ENTER_FRAME,follow_me)
function follow_me(event:Event):void {

var dx:int = bracketL.x - mouseX;
var dy:int = bracketL.y - mouseY;
 my_object.x -= dx / 9+5;
 my_object.y -= dy /9;
}

Even I roll over the btn1, the my_object does not stop, it still follow the mouse !! WHAT SHOULD I DO ?

Using a different approach for smoother animation, you could create a enter frame handler that checks for a paused state variable.

Each frame, your object is animated following the mouse cursor; however, if the mouse rolls over your button the object is paused from tracking the mouse.

鼠标跟随

Code:

import flash.events.Event;
import flash.events.MouseEvent;

var paused:Boolean = false;

addEventListener(Event.ENTER_FRAME, frameHandler);
button.addEventListener(MouseEvent.ROLL_OVER, buttonOverHandler);
button.addEventListener(MouseEvent.ROLL_OUT, buttonOutHandler);

function buttonOverHandler(event:MouseEvent):void
{
    paused = true;
}

function buttonOutHandler(event:MouseEvent):void
{
    paused = false;
}

function frameHandler(event:Event):void
{
    if (!paused)
    {
        object.x -= (object.x - mouseX) * 0.1;
        object.y -= (object.y - mouseY) * 0.1;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM