AS3 - 单击鼠标,添加和删除onenterframe EventListener

时间:2014-01-29 00:02:26

标签: actionscript-3 event-listener

尝试添加EventListener,并单击鼠标删除EventListener。添加一个作品,并且它不起作用。 on buton是gall_mc,off是gall_close_mc

gall_mc.addEventListener(MouseEvent.CLICK, scroller);

function scroller(event:MouseEvent):void
{

    var verticalCenter:Number = stage.stageHeight / 2;
    var limit:Number = stage.stageHeight - galleryHolder.height;
    var speed:Number = 0.1;
    var scrollY:Number = 0;

    stage.addEventListener(Event.ENTER_FRAME, scrollContent);

    function scrollContent(e:Event):void
    {
        scrollY =  -  speed * (mouseY - verticalCenter);
        galleryHolder.y +=  scrollY;
        if (galleryHolder.y>0) { galleryHolder.y= 0;}
        else if (galleryHolder.y< limit) { galleryHolder.y= limit; }
    }
    function eventHandler(event:Event):void
    {
    event.currentTarget.removeEventListener(Event.ENTER_FRAME, MouseEvent.CLICK.gall_close_mc);     
    }


}
========

感谢net.uk.sweet我结合了我们的代码并且它有效!

gall_mc.addEventListener(MouseEvent.CLICK, scroller);

function scroller(event:MouseEvent):void
{

    var verticalCenter:Number = stage.stageHeight / 2;
    var limit:Number = stage.stageHeight - galleryHolder.height;
    var speed:Number = 0.1;
    var scrollY:Number = 0;

    stage.addEventListener(Event.ENTER_FRAME, scrollContent);

    function scrollContent(e:Event):void
    {
        scrollY =  -  speed * (mouseY - verticalCenter);
        galleryHolder.y +=  scrollY;

    }
    gall_close_mc.addEventListener(MouseEvent.CLICK, closeClickHandler);

    function closeClickHandler(event:MouseEvent):void
    {

        stage.removeEventListener(Event.ENTER_FRAME, scrollContent);
    }



}

1 个答案:

答案 0 :(得分:2)

gall_close_mc.addEventListener(MouseEvent.CLICK, closeClickHandler);

function closeClickHandler(event:MouseEvent):void 
{
     // removeEventListener method params are :- 
     // 1. the event name, 2. the handler you assigned to it
     stage.removeEventListener(Event.ENTER_FRAME, closeClickHandler);
}

请参阅EventDispatcher docs