2014-04-01 63 views
1

這不僅僅是尋找代碼,而是一個邏輯問題,儘管它可能是需要的。AS3 lineTo方法替代方案

因此,我創建了一個繪圖應用程序,首先要做的就是使用lineTo方法繪製一條線 - 這一切都很好。但我知道這不是我想要的,它確實畫得很好等,但我想要一個不會「畫線」的工具。我想要一個工具,當你放開點擊的時候,它不會排成一行,但是當你點擊的時候它會放下來。

現在我真的不知道如何創建這個;有人可以解釋一下如何編碼這樣的東西嗎?這可能是lineTo的替代方案 - 抱歉,如果這聽起來令人困惑,這很難說出來。

+0

我沒有得到你所需要的。你需要點擊一個點,然後你會有一條線 - 從哪裏或從哪裏? – Vesper

回答

4

下面是兩個例子:

Pen Drawing (SWF Example)

這繪製線條爲曲線以匹配輸入設備,持續的線,因爲它是得出:

draw-lines

代碼:

package 
{ 
    import flash.display.Graphics; 
    import flash.display.Sprite; 
    import flash.display.StageAlign; 
    import flash.display.StageScaleMode; 
    import flash.events.MouseEvent; 
    import flash.geom.Point; 

    [SWF(percentWidth = 100, percentHeight = 100, backgroundColor = 0xefefef, frameRate = 30)] 
    public class DrawingExample extends Sprite 
    { 

     //------------------------------ 
     // model 
     //------------------------------ 

     protected var lastPoint:Point; 


     //------------------------------ 
     // lifecycle 
     //------------------------------ 

     public function DrawingExample() 
     { 
      super(); 

      stage.scaleMode = StageScaleMode.NO_SCALE; 
      stage.align = StageAlign.TOP_LEFT; 

      beginDrawing(); 
     } 

     protected function beginDrawing():void 
     { 
      stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); 
     } 

     protected function mouseDownHandler(event:MouseEvent):void 
     { 
      stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); 

      // mark mouse down location 
      lastPoint = new Point(mouseX, mouseY); 

      // listen for movement or up/out 
      stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); 
      stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler); 
      stage.addEventListener(MouseEvent.MOUSE_OUT, mouseUpHandler); 
     } 

     protected function mouseMoveHandler(event:MouseEvent):void 
     { 
      var g:Graphics = graphics; 
      g.lineStyle(1, 0x0000ff); 

      // draw line segment 
      g.moveTo(lastPoint.x, lastPoint.y); 
      g.lineTo(mouseX, mouseY); 

      // mark end of line segment 
      lastPoint = new Point(mouseX, mouseY); 
     } 

     protected function mouseUpHandler(event:MouseEvent):void 
     { 
      stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); 
      stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler); 
      stage.removeEventListener(MouseEvent.MOUSE_OUT, mouseUpHandler); 

      beginDrawing(); 
     } 

    } 
} 


Line Drawing (SWF Example)

雖然調整爲箭頭頭部,這個概念是爲線路是相同的。當輸入設備處於關閉狀態時,線/箭頭會暫停,但在輸入設備啓動之前不會保留。一旦繪製完成,該行仍然存在。

draw-arrows

代碼:

package 
{ 
    import flash.display.CapsStyle; 
    import flash.display.Graphics; 
    import flash.display.JointStyle; 
    import flash.display.LineScaleMode; 
    import flash.display.Sprite; 
    import flash.display.StageAlign; 
    import flash.display.StageScaleMode; 
    import flash.events.Event; 
    import flash.events.MouseEvent; 
    import flash.geom.Point; 

    [SWF(percentWidth = 100, percentHeight = 100, backgroundColor = 0xefefef, frameRate = 30)] 
    public class Arrows extends Sprite 
    { 

     public var arrows:Vector.<Sprite> = new Vector.<Sprite>(); 

     public var canvas:Sprite; 

     public var lineColor:uint = Math.random() * 0xffffff; 

     public var lineWeight:Number = 5; 

     private var startPoint:Point; 


     public function Arrows() 
     { 
      super(); 

      addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler); 
     } 

     protected function addedToStageHandler(event:Event):void 
     { 
      stage.scaleMode = StageScaleMode.NO_SCALE; 
      stage.align = StageAlign.TOP_LEFT; 

      addCanvas(); 
     } 

     protected function addCanvas():void 
     { 
      canvas = new Sprite(); 
      addChild(canvas); 

      lineColor = Math.random() * 0xffffff; 

      // give alpha for interaction 
      var g:Graphics = canvas.graphics; 
      g.beginFill(0x0, 0.0); 
      g.drawRect(0, 0, stage.stageWidth, stage.stageHeight); 
      g.endFill(); 

      // listen for mouse down 
      canvas.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); 
     } 

     protected function mouseDownHandler(event:MouseEvent):void 
     { 
      canvas.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); 

      // mark start point 
      startPoint = new Point(event.localX, event.localY); 

      // start rendering 
      canvas.addEventListener(Event.ENTER_FRAME, enterFrameHandler); 

      // listen for mouse up/out to end arrow 
      canvas.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler); 
      canvas.addEventListener(MouseEvent.MOUSE_OUT, mouseUpHandler); 
     } 

     protected function enterFrameHandler(event:Event):void 
     { 
      var angle:Number = polarAngle(new Point(mouseX, mouseY), new Point(startPoint.x, startPoint.y)); 

      // draw line 
      var g:Graphics = canvas.graphics; 
      g.clear(); 

      // give alpha for interaction 
      g.beginFill(0x0, 0.0); 
      g.drawRect(0, 0, stage.stageWidth, stage.stageHeight); 
      g.endFill(); 

      g.lineStyle(lineWeight, lineColor, 1, true, LineScaleMode.NORMAL, CapsStyle.SQUARE, JointStyle.MITER); 
      g.moveTo(startPoint.x, startPoint.y); 
      g.lineTo(mouseX, mouseY); 

      // draw arrow head 
      g.moveTo(mouseX - (20 * Math.cos((angle - 45) * Math.PI/180)), 
        mouseY - (20 * Math.sin((angle - 45) * Math.PI/180))); 

      g.lineTo(mouseX + (5 * Math.cos((angle) * Math.PI/180)), 
        mouseY + (5 * Math.sin((angle) * Math.PI/180))); 

      g.lineTo(mouseX - (20 * Math.cos((angle + 45) * Math.PI/180)), 
        mouseY - (20 * Math.sin((angle + 45) * Math.PI/180))); 
     } 

     protected function polarAngle(point:Point, center:Point=null):Number 
     { 
      if (!center) 
       center = new Point(0, 0); 

      return Math.atan2(point.y - center.y, point.x - center.x) * 180/Math.PI; 
     } 

     protected function mouseUpHandler(event:MouseEvent):void 
     { 
      canvas.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler); 
      canvas.removeEventListener(MouseEvent.MOUSE_OUT, mouseUpHandler); 

      // stop rendering 
      canvas.removeEventListener(Event.ENTER_FRAME, enterFrameHandler); 

      // push canvas to arrows collection 
      arrows.push(canvas); 

      // add a fresh canvas 
      addCanvas(); 
     } 

    } 
} 
+0

謝謝賈森,我能夠完美地完成我的代碼並得到我想要的東西。謝謝:) – user3418126

+0

@ user3418126不要忘了接受這個答案,通過點擊左邊的勾號,如果你覺得這已經令人滿意地回答了你的問題 – CyanAngel

+0

@JasonSturges所有鏈接似乎已經死了... – Teejay