2012-06-21 26 views
0

字符串效果我想做一個像這個網站的字符串效果http://www.bcaa.it/ 我可以學習什麼樣的腳本或教程?如何實現像http://www.bcaa.it/

基本上我想實現與網站相同。有彈跳的字符串效果,拖動項目,並遠離其他項目,靠近它,拖動和子對象後跟緩動...

+0

你的意思是你只是想畫一條線,附加兩個元素? – Marty

+0

Yah就像那個網站,當你點擊1個對象時,它會出現另一個對象,它帶有字符串並附加在主對象上。當拖動主要對象時,子對象跟隨緩動/反彈。我可以遵循的任何教程? – pizza0502

+0

包括圓圈,線條繪製和彈跳補間讓這個問題變得相當寬泛。我可以寫一個答案,涵蓋在兩個移動物體之間繪製一條線。 – Marty

回答

1

嘿剛開始編碼這個,需要類似的圖形顯示我工作。這裏是我的開始,只是兩個按鈕,但這個想法可以外推/優化:

<?xml version="1.0" encoding="utf-8"?> 
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx" 
         creationComplete="windowedapplication1_creationCompleteHandler(event)"> 

    <fx:Script> 
     <![CDATA[ 
      import mx.events.FlexEvent; 

      private var optimalDistanceUpdateTimer:Timer; 

      private var optimalDistance:Number = 100; 

      private var objectUserGrabbed:Button; 

      private var delayDenominator:Number = 6; 

      protected function button1_mouseDownHandler(event:MouseEvent):void 
      { 
       objectUserGrabbed = event.target as Button; 
       objectUserGrabbed.startDrag(); 
      } 

      protected function button1_mouseUpHandler(event:MouseEvent):void 
      { 
       objectUserGrabbed.stopDrag(); 
      } 

      protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void 
      { 
       optimalDistanceUpdateTimer = new Timer(33); 
       optimalDistanceUpdateTimer.addEventListener(TimerEvent.TIMER, optimalDistanceTickedHandler); 
       optimalDistanceUpdateTimer.start(); 
      } 
      private function optimalDistanceTickedHandler(event:TimerEvent):void 
      { 
       var targetPoint:Point; 
       var deltaX:Number; 
       var deltaY:Number; 

       if(!objectUserGrabbed) 
        return; 

       if(objectUserGrabbed == button1) 
       { 
        //Basically here I'm playing of Pythagorean theorem for a 45 degree triangle the x component and the y component are the distance of the hypotenuse over Sqrt(2) for different offsets, radially you'd need to adjust this to compute the target point for each object... would do something like var angleForEach:Number = 360/numObjects; then figure out the target point for each of the objects in a loop var angleForThis = angleForEach*index; (in this example only two objects, no loop needed) 
        targetPoint = new Point(objectUserGrabbed.x-optimalDistance/Math.SQRT2, objectUserGrabbed.y-optimalDistance/Math.SQRT2); 


        deltaX = (targetPoint.x-button2.x); 
        deltaY = (targetPoint.y-button2.y); 

        button2.move(button2.x+deltaX/delayDenominator, button2.y+deltaY/delayDenominator); 
       } 
       else 
       { 
        targetPoint = new Point(objectUserGrabbed.x+optimalDistance/Math.SQRT2, objectUserGrabbed.y + optimalDistance/Math.SQRT2); 

        deltaX = (targetPoint.x-button1.x); 
        deltaY = (targetPoint.y-button1.y); 

        button1.move(button1.x+deltaX/delayDenominator, button1.y+deltaY/delayDenominator); 
       } 
       lineDrawingArea.graphics.clear(); 
       lineDrawingArea.graphics.lineStyle(1); 
       lineDrawingArea.graphics.moveTo(button1.x + button1.width/2,button1.y + button1.height/2); 
       lineDrawingArea.graphics.lineTo(button2.x + button2.width/2,button2.y + button2.height/2); 
      } 
     ]]> 
    </fx:Script> 

    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 
    </fx:Declarations> 
    <s:Group id="lineDrawingArea"/> 
    <s:Button id="button1" label="Item 1" mouseDown="button1_mouseDownHandler(event)" mouseUp="button1_mouseUpHandler(event)" x="0" y="0"/> 
    <s:Button id="button2" label="Item 2" mouseDown="button1_mouseDownHandler(event)" mouseUp="button1_mouseUpHandler(event)" x="0" y="0"/> 
</s:WindowedApplication> 
+0

哎呀抱歉,我沒有提到我只熟悉Flash AS3,但是我可以將您的代碼實現到AS3嗎? – pizza0502

+0

@ pizza0502是的,你可以在AS3 pure中實現這個代碼,沒有太多問題......在Sprite上存在startDrag/stopDrag,幾乎所有的東西都可以在Flash中使用,沒有Flex。您可能想要在構造函數中處理該代碼,而不是使用創建完成,否則這應該是一個非常好的起點,將用一條線顯示一個對象連接到另一個對象,並在其中一個對象被另一個對象拖動時「追逐「。爲了進一步擴展這個概念,你必須處理一組對象而不是兩個。 – shaunhusain

+0

@ pizza0502基本上我在這裏碰到的問題是,現在這對於我繼續使用我自己的數據結構(一個圖形實現)來填充這條線路網絡的工作非常好,所以我做的剩餘工作這將面向我希望它爲我的實現/數據結構工作。您必須想出數據結構/值對象設置,以便以某種對此交互有意義的方式(parent - > children,其中parent具有isOpen的屬性)存儲所有「節點」的數據。 – shaunhusain