画一个动画片段

时间:2013-10-29 08:42:37

标签: actionscript-3

我有一个涂料应用程序。我想要一个图层(带有正方形的movieclip),我想绘制它。 直到现在我都不能画画但是当我试图添加movieclip的时候,我无法画出那个movieclip。制作这样的东西的方法是什么? 这是我的代码:

import flash.display.Sprite;

var myThick=10;
var currentColor = 0X000000;
var myCanvas:Sprite;

init();

function init():void {
    drawCanvas();
    var clearSwatch:Sprite = new Sprite();
    clearSwatch.graphics.beginFill(0xFFFFFF);
    clearSwatch.graphics.lineStyle(2, 0x000000);
    clearSwatch.graphics.drawCircle(30, 370, 20);
    clearSwatch.graphics.endFill();
    addChild(clearSwatch);
    clearSwatch.buttonMode = true;
    clearSwatch.addEventListener(MouseEvent.CLICK, clearCanvas);//clear
    }

function drawCanvas():void {
    myCanvas = new Sprite();
    myCanvas.graphics.beginFill(0xF0F0F0);
    myCanvas.graphics.drawRect(60, 20, stage.stageWidth-80, stage.stageHeight+40);
    myCanvas.graphics.endFill();
    myCanvas.graphics.lineStyle(myThick, currentColor);
    addChild(myCanvas);
    myCanvas.addEventListener(MouseEvent.MOUSE_DOWN, startDraw);
  //stage.addEventListener(MouseEvent.MOUSE_UP, stopDraw);
    myCanvas.buttonMode = true;
}

function startDraw(event:MouseEvent):void {
    myCanvas.graphics.moveTo(event.localX, event.localY);
    myCanvas.addEventListener(MouseEvent.MOUSE_MOVE, paintLine);
}

function stopDraw(event:MouseEvent):void {
    myCanvas.removeEventListener(MouseEvent.MOUSE_MOVE, paintLine);
}

function paintLine(event:MouseEvent):void {
    myCanvas.graphics.lineTo(event.localX, event.localY);
    event.updateAfterEvent();
}
function clearCanvas(event:MouseEvent):void {
    myCanvas.graphics.clear();
    drawCanvas();
}

我怎样才能让它发挥作用? 谢谢!

1 个答案:

答案 0 :(得分:1)

将两个侦听器添加到舞台,以及鼠标移动侦听器,但使用myCanvas上的图形命令。

stage.addEventListener(MouseEvent.MOUSE_DOWN, startDraw);
function startDraw(event:MouseEvent):void {
    myCanvas.graphics.moveTo(event.localX, event.localY);
    myCanvas.addEventListener(MouseEvent.MOUSE_MOVE, paintLine);
}