2012-07-24 125 views
0

我目前在線按照Flash教程創建交互式畫板。鏈接到教程是http://flashexplained.com/actionscript/making-an-interactive-drawing-sketchpad/將ActionScript 2.0代碼轉換爲ActionScript 3.0

本教程的唯一問題是代碼是針對actionscript 2.0而不是3.0。我知道如何重新定義這些變量,但除此之外,我無能爲力,所以我希望有人能夠幫助我將代碼轉換爲ActionScript 3.0。

這裏是ActionScript 2.0代碼:

lineThickness = 0; 
selectedColor = "0x000000"; 

_root.onMouseDown = startDrawing; 
_root.onMouseUp = stopDrawing; 

function startDrawing() 
{ 
    if(_xmouse < 455) 
    { 
     _root.lineStyle(lineThickness, selectedColor); 
     _root.moveTo(_root._xmouse, _root._ymouse); 
     _root.onMouseMove = drawLine; 
    } 
} 

function drawLine() 
{ 
    _root.lineTo(this._xmouse, this._ymouse); 
} 


function stopDrawing() 
{ 
    delete this.onMouseMove; 
} 

line0.onPress = function() 
{ 
    lineThickness = 0; 
} 

line3.onPress = function() 
{ 
    lineThickness = 3; 
} 

line6.onPress = function() 
{ 
    lineThickness = 6; 
} 

colorRed.onPress = function() 
{ 
    selectedColor = "0xFF0000"; 
} 

colorGreen.onPress = function() 
{ 
    selectedColor = "0x00FF00"; 
} 

回答

3

AS2:

lineThickness = 0; 
selectedColor = "0x000000"; 

AS3:

var lineThickness:int = 0; 
var selectColor:uint = 0x000000; 

AS2:

_root.onMouseDown = startDrawing; 
_root.onMouseUp = stopDrawing; 

function startDrawing() 
{ 
    if(_xmouse < 455) 
    { 
     _root.lineStyle(lineThickness, selectedColor); 
     _root.moveTo(_root._xmouse, _root._ymouse); 
     _root.onMouseMove = drawLine; 
    } 
} 

function stopDrawing() 
{ 
    delete this.onMouseMove; 
} 

function drawLine() 
{ 
    _root.lineTo(this._xmouse, this._ymouse); 
} 

AS3:

stage.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing); 
stage.addEventListener(MouseEvent.MOUSE_UP, stopDrawing); 

function startDrawing(e:MouseEvent):void 
{ 
    if(mouseX < 455) 
    { 
     this.graphics.lineStyle(lineThickness, selectedColor); 
     this.graphics.moveTo(mouseX, mouseY); 

     stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove); 
    } 
} 

function stopDrawing(e:MouseEvent):void 
{ 
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove); 
} 

function mouseMove(e:MouseEvent):void 
{ 
    this.graphics.lineTo(mouseX, mouseY); 
} 

AS2:

line0.onPress = function() 
{ 
    lineThickness = 0; 
} 

line3.onPress = function() 
{ 
    lineThickness = 3; 
} 

line6.onPress = function() 
{ 
    lineThickness = 6; 
} 

colorRed.onPress = function() 
{ 
    selectedColor = "0xFF0000"; 
} 

colorGreen.onPress = function() 
{ 
    selectedColor = "0x00FF00"; 
} 

AS3:

line0.addEventListener(MouseEvent.CLICK, changeLine); 
line3.addEventListener(MouseEvent.CLICK, changeLine); 
line6.addEventListener(MouseEvent.CLICK, changeLine); 
colorRed.addEventListener(MouseEvent.CLICK, changeColor); 
colorGreen.addEventListener(MouseEvent.CLICK, changeColor); 

function changeLine(e:MouseEvent):void 
{ 
    switch(e.currentTarget) 
    { 
     default: lineThickness = 1; break; 

     case line0: lineThickness = 0; break; 
     case line3: lineThickness = 0; break; 
     case line6: lineThickness = 0; break; 
    } 
} 

function changeColor(e:MouseEvent):void 
{ 
    switch(e.currentTarget) 
    { 
     default: selectedColor = 0x000000; 

     case colorRed: selectedColor = 0xFF0000; break; 
     case colorGreen: selectedColor = 0x00FF00; break; 
    } 
} 

附加(清除圖形):

eraser_btn.addEventListener(MouseEvent.CLICK, erase); 
function erase(e:MouseEvent):void 
{ 
    this.graphics.clear(); 
} 
+0

我複製了您的代碼並測試了閃存,並且收到了一條錯誤消息,指出「1120:訪問未定義的屬性selectedColor」。我需要以某種方式定義它嗎? – Learn123 2012-07-24 01:25:16

+0

我更正了1120錯誤。當您在頂部聲明變量時,您鍵入selectColor而不是selectedColor。但是,我現在收到一條錯誤消息:錯誤#1063:paintcanvas_fla :: MainTimeline/stopDrawing()上的參數計數不匹配。預計0,得到1.這是什麼意思? – Learn123 2012-07-24 01:34:22

+0

@ Learn123哎呀,在'stopDrawing()'中添加'e:MouseEvent'。 – Marty 2012-07-24 01:35:33

0

我建議你閱讀這篇http://www.actionscriptcheatsheet.com/downloads/as3cs_migration.pdf

但是,這裏有一些提示。

活動你的

_root.lineStyle(lineThickness, selectedColor); 
_root.moveTo(_root._xmouse, _root._ymouse); 

的情況下,像這樣的代碼

object.addEventListener(Event.EVENT_TYPE,myFunctionToHandleTheEvent); 

您使用此

this.graphics.lineStyle(lineThickness, selectedColor); 
this.graphics.moveTo(stage.mouseX, stage.mouseY); 

刪除事件偵聽器使用

object.removeEventListener(Event.EVENT_TYPE,myFunction); 
+0

謝謝,我會看着你第一次提供的鏈接。 – Learn123 2012-07-24 01:12:25

+0

僅供參考 - 該鏈接已損壞。 – HungryBeagle 2017-01-17 14:09:50

相關問題