0

我目前在网上按照 Flash 教程创建交互式画板。教程的链接是http://flashexplained.com/actionscript/making-an-interactive-drawing-sketchpad/

本教程的唯一问题是代码是针对 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";
}
4

2 回答 2

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();
}
于 2012-07-24T01:13:27.937 回答
0

我建议您阅读此http://www.actionscriptcheatsheet.com/downloads/as3cs_migration.pdf

但是,这里有一些提示。

你这样编码的事件

object.addEventListener(Event.EVENT_TYPE,myFunctionToHandleTheEvent);

如果是

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

你用这个

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

删除事件侦听器使用

object.removeEventListener(Event.EVENT_TYPE,myFunction);
于 2012-07-24T01:10:22.907 回答