2013-04-22 38 views
0

我改變了我下載的圖像傳送帶,當我點擊我添加的兩個按鈕之一時旋轉到下一張圖片(左或右)。原始轉盤根據鼠標移動/位置而旋轉。上一個功能被稱爲/事件監聽器被「延遲」了一個

由於某種原因,無論何時點擊'right'或'left'按鈕,每個按鈕都會在相應的方向上旋轉輪播,事件監聽器/處理程序就是一個'後面'。它做了我以前點擊一個按鈕時應該做的任何事情。說得更清楚,第一個按鈕點擊它什麼都不做。第二個按鈕點擊它響應我上次點擊的內容。

例子:

  • 我點擊左邊的按鈕,沒有任何反應。
  • 然後我點擊右邊的按鈕,傳送帶向左旋轉(因爲我點擊了左邊的按鈕,然後點擊)
  • 然後我點擊左邊的按鈕,傳送帶向右旋轉(同上)。

請參閱下面的代碼。看起來很簡單;沒有複雜的結構或其他。

我想可以忽略大多數變量和定位(如focalLength,vanishingPointX,radius等)。我猜這個bug或者與XML的導入/處理,for()循環或者.as文件所具有的結構有關。

package { 
     //here are all the imports 

     public class Imagereel extends Sprite { 
      var imgurl:URLRequest = new URLRequest() 
      var loadedimgs:uint = 0; 
      var images_num = 0; 
      var imageHolders:Array = new Array(); 
      var imageHolder:MovieClip; 
      var btnLeft:BtnLeft = new BtnLeft; 
      var btnRight:BtnRight = new BtnRight; 

     //Set the focal length 
     var focalLength:Number = 2000; 

     //Set the vanishing point 
     var vanishingPointX:Number = stage.stageWidth/2; 
     var vanishingPointY:Number = stage.stageHeight/2; 

     //The 3D floor for the images 
     var floor:Number = 40; 

     //Radius of the circle 
     var radius:Number = 350; 

     //We use 70x70 sized images (change this if different for your images) 
     const IMAGE_WIDTH:uint = 393; 
     const IMAGE_HEIGHT:uint = 249; 

     var xmlLoader:URLLoader = new URLLoader(); 
     var xmlData:XML = new XML();   

     public function Imagereel() { 
      //here's the positioning of the buttons 
      //here are the button addChilds 

      xmlLoader.load(new URLRequest("carousel.xml")); 
      xmlLoader.addEventListener(Event.COMPLETE, LoadXML); 
      btnLeft.addEventListener(MouseEvent.CLICK, prevImg); 
      btnRight.addEventListener(MouseEvent.CLICK, nextImg); 

     } 
     function LoadXML(e:Event):void { 
      xmlData = new XML(e.target.data); 
      Parseimage(xmlData); 
     } 
     function Parseimage(imageinput:XML):void { 
      var imageurl:XMLList = imageinput.image.iurl; 

      images_num = imageurl.length(); 
      for (var i:int = 0; i < images_num; i++) { 
       var urlElement:XML = imageurl[i]; 

       imageHolder = new MovieClip(); 
       var imageLoader = new Loader(); 
       imageHolder.addChild(imageLoader); 
       imageHolder.mouseChildren = false; 
       imageLoader.x = - (IMAGE_WIDTH); 
       imageLoader.y = - (IMAGE_HEIGHT); 
       imageHolders.push(imageHolder); 
       imgurl.url = imageurl[i]; 
       imageLoader.load(imgurl); 
       imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded); 
      } 
     } 
     function imageLoaded(e:Event) { 
      //Update the number of loaded images 
      loadedimgs++; 

      //Check to see if this is the last image loaded 
      if (loadedimgs == images_num) { 
       //Set up the carousel 
       initializeCarousel(); 
      } 
     } 
     function initializeCarousel() { 
      //Calculate the angle difference between the images (in radians) 
      var angleDifference:Number = Math.PI * (360/images_num)/180; 

      //Loop through the images 
      for (var i:uint = 0; i < imageHolders.length; i++) { 
       //Assign the imageHolder to a local variable 
       var imageHolder:MovieClip = (MovieClip)(imageHolders[i]); 

       //Get the angle for the image (we space the images evenly) 
       var startingAngle:Number = angleDifference * i -0.30; 

       //Position the imageHolder 
       imageHolder.xpos3D = radius * Math.cos(startingAngle); 
       imageHolder.zpos3D = radius * Math.sin(startingAngle); 
       imageHolder.ypos3D = floor; 

       //Set a "currentAngle" attribute for the imageHolder 
       imageHolder.currentAngle = startingAngle; 
       var scaleRatio = focalLength/(focalLength + imageHolder.zpos3D); 

       //Position the imageHolder to the stage (from 3D to 2D coordinates) 
       imageHolder.x = vanishingPointX + imageHolder.xpos3D * scaleRatio; 
       imageHolder.y = vanishingPointY + imageHolder.ypos3D * scaleRatio; 

       //Add the imageHolder to the stage 
       addChild(imageHolder); 
      } 
      sortZ(); 
     } 

     function prevImg(e:MouseEvent) { 
      //Loop through the images 
      for (var i:uint = 0; i < imageHolders.length; i++) { 
       var imageHolder:MovieClip = (MovieClip)(imageHolders[i]); 
       //Set a new 3D position for the imageHolder 
       imageHolder.xpos3D = radius * Math.cos(imageHolder.currentAngle); 
       imageHolder.zpos3D = radius * Math.sin(imageHolder.currentAngle); 
       var scaleRatio; 

       //Calculate a scale ratio 
       scaleRatio = focalLength/(focalLength + imageHolder.zpos3D); 

       //Update the imageHolder's coordinates 
       imageHolder.x = vanishingPointX+imageHolder.xpos3D * scaleRatio; 
       imageHolder.y = vanishingPointY+imageHolder.ypos3D * scaleRatio; 

       //spinning the carousel 
       imageHolder.currentAngle += 0.6285; 
      } 
      //Call the function that sorts the images so they overlap each others correctly 
      sortZ(); 
     } 
     function nextImg(e:MouseEvent) { 
      //Loop through the images 
      for (var i:uint = 0; i < imageHolders.length; i++) { 
       var imageHolder:MovieClip = (MovieClip)(imageHolders[i]); 
       //Set a new 3D position for the imageHolder 
       imageHolder.xpos3D = radius * Math.cos(imageHolder.currentAngle); 
       imageHolder.zpos3D = radius * Math.sin(imageHolder.currentAngle); 
       var scaleRatio; 

       //Update the imageHolder's coordinates 
       imageHolder.x = vanishingPointX+imageHolder.xpos3D * scaleRatio; 
       imageHolder.y = vanishingPointY+imageHolder.ypos3D * scaleRatio; 

       //spinning the carousel 
       imageHolder.currentAngle -= 0.6285; 
      } 
      sortZ(); 
     } 
     //This function sorts the images so they overlap each others correctly 
     function sortZ():void { 
      imageHolders.sortOn("zpos3D", Array.NUMERIC | Array.DESCENDING); 

      //Set new child indexes for the images 
      for (var i:uint = 0; i < imageHolders.length; i++) { 
       setChildIndex(imageHolders[i], i); 
      } 
     } 
    } 
} 

那麼這個代碼:

  1. carousel.xml導入
  2. XML被處理,以使得圖像路徑那裏被轉換成旋轉木馬是做出來的所顯示的圖像
  3. 圖像
  4. sortZ()函數確保圖像在3D透視圖中正確對齊;就像在CSS中的z-index一樣。
  5. 單擊btnLeftbtnRight時,傳送帶向左或向右旋轉(通過更新imageHolder.currentAngle的值完成)。

當我把trace的內部prevImg()和nextImg()函數,我看到了跟蹤時 屬於正確的函數,而不是前一次單擊一個。所以看來Flash 確實呼叫正確的事件。

那麼我該如何擺脫這個bug? 幫助和提示,非常感謝!

+0

聽起來像一個簡單的,會做一些更多的跟蹤,只是在一些斷點下降,並通過代碼幾次,看看是否有什麼變得明顯(檢查所有變量似乎設置「正確」爲一個簡單的測試用例) – shaunhusain 2013-04-22 12:54:33

+0

感謝編輯@shaunhusain,這看起來好多了:D – poepje 2013-04-22 12:56:58

+0

有了'斷點',你的意思是調試嗎?我不太瞭解這是如何工作的,但當嘗試一些東西時我找不到任何東西。另外,你的意思是'變量設置正確'? – poepje 2013-04-22 13:24:02

回答

0

移動imageHolder.currentAngle賦值(您更改它的行)在改變imageHolder的3D位置的代碼之前。

for (var i:uint = 0; i < imageHolders.length; i++) { 
      var imageHolder:MovieClip = (MovieClip)(imageHolders[i]); 
      //Set a new 3D position for the imageHolder 
      imageHolder.currentAngle += 0.6285; // <== HERE 
      imageHolder.xpos3D = radius * Math.cos(imageHolder.currentAngle); 
      imageHolder.zpos3D = radius * Math.sin(imageHolder.currentAngle); 
      var scaleRatio; 

      //Calculate a scale ratio 
      scaleRatio = focalLength/(focalLength + imageHolder.zpos3D); 

      //Update the imageHolder's coordinates 
      imageHolder.x = vanishingPointX+imageHolder.xpos3D * scaleRatio; 
      imageHolder.y = vanishingPointY+imageHolder.ypos3D * scaleRatio; 

      //spinning the carousel 
     } 

對於其他功能相同。

+0

是的,就是這樣!謝謝!你能否告訴我爲什麼它現在起作用?因爲我不太明白。在我看來,'x','y'和'scaleRatio'與'currentAngle'或..無關? – poepje 2013-04-22 14:36:12

+0

那麼,當你執行一個回合時,你從你的座標派生出來的東西就是'currentAngle'。這意味着,如果你想旋轉你的旋轉木馬,你必須首先確定角度,然後重新計算所有相關數據。你做了相反的事,想知道,數據如何才能顯示以前的狀態。而且,它們通過用於賦值'x'和'y'的'xpos3D'和'zpos3D'來關聯。 – Vesper 2013-04-23 05:40:04

+0

這很有道理哈哈。感謝澄清:) – poepje 2013-04-23 07:24:45

相關問題