2015-01-03 47 views
0

我是as3的初學者,並且一直遵循簡單的Flash草圖板腳本,這裏是鏈接>http://www.sitepoint.com/create-flash-sketchpad/。它的編碼爲as2,但我想將代碼轉換爲as3.I一直試圖將它們轉換爲as3,但我似乎無法使其工作。我非常感謝有人可以幫助我。將簡單腳本從actionscript2轉換爲actionscript3

createEmptyMovieClip("Line",1); 
Line.lineStyle(3,0x000000,300); 

onMouseDown = function() { 
    Line.moveTo(_xmouse, _ymouse); 
    onMouseMove = function() { 
     Line.lineTo(_xmouse, _ymouse); 
    } 
} 

onMouseUp=function() { 
    onMouseMove=null; 
} 
+0

分享您的嘗試,我們會幫您解決問題。 – loxxy

回答

1

這將在AS3給:

var Line:Sprite = new Sprite(); 
addChild(Line); 
Line.graphics.lineStyle(3, 0x000000, 1); 

stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); 
addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler); 

function mouseDownHandler(e:MouseEvent):void { 
    Line.graphics.moveTo(e.stageX, e.stageY); 
    stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); 
} 

function mouseMoveHandler(e:MouseEvent):void { 
    Line.graphics.lineTo(e.stageX, e.stageY); 
} 

function mouseUpHandler(e:MouseEvent):void { 
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); 
} 
2

我決定創建一個完整的工作示例,這樣你就可以理解它是如何工作的;

package { 
    import flash.display.Shape; 
    import flash.display.Sprite; 
    import flash.events.Event; 
    import flash.events.MouseEvent; 

    /** 
    * ... 
    * @author Martyn 
    */ 
    public class Main extends Sprite { 

     public var draw:Shape = new Shape(); 

     public function Main():void { 
      if (stage) init(); 
      else addEventListener(Event.ADDED_TO_STAGE, init); 
     } 

     private function init(e:Event = null):void { 
      removeEventListener(Event.ADDED_TO_STAGE, init); 
      stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); 
      stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); 

      trace("init"); 
      addChild(draw); 
      draw.graphics.lineStyle(3, 0x000000, 300); 

     } 
     private function onMouseDown(e:MouseEvent):void 
     { 
      stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); 
      draw.graphics.moveTo(mouseX, mouseY); 
      trace("onMouseDown"); 
     } 

     private function onMouseUp(e:MouseEvent):void 
     { 
      stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); 
      trace("onMouseUp"); 
     } 

     private function onMouseMove(e:MouseEvent):void 
     { 
      draw.graphics.lineTo(mouseX, mouseY); 
      trace("onMouseMove"); 
     } 

    } 

} 

希望這會有所幫助。

+0

我現在想了解這一點。非常感謝您的幫助。 –

+0

@p ppt - 沒問題。如果你不知道爲什麼我在init()中聲明瞭一些代碼,那只是爲了確保當類添加事件偵聽器並調用addChild()時,類可以訪問對該階段的引用。你會發現使用init函數的其他主題。 –