AS3滚动在按钮悬停上切换方向的背景

时间:2014-04-11 14:55:48

标签: actionscript-3 flash background scroll

我在闪光灯中有一个滚动的背景。它目前在输入框架上向左滚动。我有2个左右箭头,我想控制滚动背景。左箭头现在正常工作,但当我将鼠标悬停在右箭头上时,事情就会发生: 1:背景不重复 2:影片剪辑抖动,从而导致影片剪辑之间出现间隙。

有什么想法吗?

            //Adds an event listener to the stage.
            stage.addEventListener(Event.ENTER_FRAME, leftScroll); 

            //This function moves both the images to left. If the first and second 
            //images goes pass the left stage boundary then it gets moved to 
            //the other side of the stage. 
            function leftScroll(e:Event):void{
                pro1.x += scrollSpeed;  
                pro2.x += scrollSpeed;  

                if(pro1.x - scrollSpeed < -pro1.width){
                        pro1.x = pro1.width;
                        }else if(pro2.x - scrollSpeed < -pro2.width){
                        pro2.x = pro2.width;
                        }
            }
            L_arrow.addEventListener(MouseEvent.MOUSE_OVER, FastLeft);
            L_arrow.addEventListener(MouseEvent.MOUSE_OUT, SlowLeft);

            R_arrow.addEventListener(MouseEvent.MOUSE_OVER, FastRight);
            L_arrow.addEventListener(MouseEvent.MOUSE_OUT, SlowRight);


            function FastLeft(e:MouseEvent):void
            {
                trace("left");
                stage.addEventListener(Event.ENTER_FRAME, leftScroll); 
                changeLeft();
            }

            function changeLeft()
            {
                scrollSpeed = gas;
                idle = false;

            }

            function SlowLeft(e:MouseEvent):void
            {
                trace("left");
                if(idle == false){
                        scrollSpeed = neutral;
                }
            }


            function FastRight(e:MouseEvent):void
            {
                trace("left");
                stage.removeEventListener(Event.ENTER_FRAME, leftScroll);
                changeRight();
            }

            function changeRight()
            {
                stage.addEventListener(Event.ENTER_FRAME, rightScroll);
                scrollSpeed = gas;
                idle = false;

            }

            function rightScroll(e:Event):void{
                pro1.x -= scrollSpeed;  
                pro2.x -= scrollSpeed;
                if(pro1.x - scrollSpeed > -pro1.width){
                        pro1.x = pro1.width;
                        }else if(pro2.x - scrollSpeed > -pro2.width){
                        pro2.x = pro2.width;
                        }
            }

            function SlowRight(e:MouseEvent):void
            {
                trace("Right");
                if(idle == false){
                        scrollSpeed = neutral;
                }
                stage.removeEventListener(Event.ENTER_FRAME, rightScroll);
            }

1 个答案:

答案 0 :(得分:1)

我会提出几点意见。

通常,我会避免在运行时添加和删除这么多事件侦听器。

通过拥有这么多单独的enterFrame侦听器,你会引起灾难和混乱。

这是我的建议:

    stage.addEventListener(Event.ENTER_FRAME, onEnterFrame); 
    L_arrow.addEventListener(MouseEvent.MOUSE_OVER, FastLeft);
    L_arrow.addEventListener(MouseEvent.MOUSE_OUT, SlowLeft);

    R_arrow.addEventListener(MouseEvent.MOUSE_OVER, FastRight);
    R_arrow.addEventListener(MouseEvent.MOUSE_OUT, SlowRight);

    scrollSpeed = 0;

    function onEnterFrame(e:Event)
    {
        pro1.x += scrollSpeed;  
        pro2.x += scrollSpeed;

        if (pro1.x < 0 - pro1.width)
        {
            pro1.x = stage.stageWidth;
        }
        else if (pro1.x >= stage.stageWidth)
        {
            pro1.x = 0 - pro1.width;
        }

        if (pro2.x < 0 - pro1.width)
        {
            pro2.x = stage.stageWidth;
        }
        else if (pro2.x >= stage.stageWidth)
        {
            pro2.x = 0 - pro2.width;
        }
    }


    function FastLeft(e:MouseEvent):void
    {
        trace("left");
        scrollSpeed = -gas;
    }

    function SlowLeft(e:MouseEvent):void
    {
        trace("left");
        scrollSpeed = -neutral;
    }


    function FastRight(e:MouseEvent):void
    {
        trace("left");
        scrollSpeed = gas;
    }

    function SlowRight(e:MouseEvent):void
    {
        trace("Right");
        scrollSpeed = neutral;
    }

这会尝试解决您的一些问题,并简化您的代码。