2014-04-11 53 views
0

我在Flash中包含一個滾動背景。它目前在輸入框中向左滾動。我有2個箭頭,左右,我想控制滾動背景。左箭頭現在工作,但當我懸停在右箭頭2事情發生: 1:背景不重複 2:電影剪輯混亂,從而導致電影剪輯之間的差距。AS3滾動按鈕懸停切換方向的背景

任何想法?

  //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

我想說的幾點。

通常,我會避免在運行時添加和刪除如此多的事件偵聽器。

通過擁有如此多的獨立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; 
    } 

這試圖解決一些您的問題,並簡化你的代碼。

+0

謝謝Glitcher。像魅力一樣工作。由於「onEnterFrame」引發了一個錯誤,但一切仍然有效。 – user3524142

+0

這是Flash引起的一個令人討厭的警告,認爲每個人都是愚蠢的^ _ ^它可以被安全地忽略。如果這有幫助,請不要忘記標記爲正確的答案 – Glitcher