如何使用BitmapData用铅笔绘图?

时间:2012-01-06 11:48:58

标签: actionscript-3 bitmapdata

现在我只有这段代码,但我没有使用BitmapData.draw()。如何使用BitmapData.draw()编写代码?

penSprite.graphics.lineStyle(3,045666);

addChild(penSprite);

addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
addEventListener(MouseEvent.MOUSE_UP,   mouseUp);

penSprite = new Sprite();                  //var penSprite:Sprite = new Sprite();
mouseDownFlag = new Boolean();            //var mouseDownFlag:Boolean = false;

private function mouseDown(e:MouseEvent):void
{
    penSprite.graphics.moveTo(e.localX, e.localY);
    mouseDownFlag = true;
}

private function mouseMove(e:MouseEvent):void
{
    if (mouseDownFlag) penSprite.graphics.lineTo(e.localX, e.localY);
}

private function mouseUp(e:MouseEvent):void
{
    mouseDownFlag = false;
}

2 个答案:

答案 0 :(得分:1)

以下是基于您的代码的代码示例。你只需将它作为电影的基类就可以试试。

package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;

/**
 * Drawing app
 */
public class DrawingApp extends MovieClip {

    /** Drawing sprite */
    protected var penSprite     : Sprite = new Sprite();

    /** Bitmap where drawing will be saved */
    protected var canvasBitmap  : Bitmap;

    /**
     * Class constructor
     */
    public function DrawingApp () {

        // create bitmap that hold a bitmap data, where your drawing will displayed
        this.canvasBitmap = new Bitmap( new BitmapData( this.stage.stageWidth, this.stage.stageHeight ), "auto", true );

        // add children. Canvas must be below sprite
        this.addChild( canvasBitmap );
        this.addChild(penSprite);

        this.penSprite.graphics.lineStyle(3, 0x000000 );

        // listen to stage, not to the root or you won't get mouse down events root sprite is empty.
        this.stage.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseDown);

    }

    /**
     * Start drawing 
     */
    private function mouseDown(e:MouseEvent):void {

        this.penSprite.graphics.moveTo(e.localX, e.localY);

        // add movement listeners here instead of using flag
        this.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.mouseMove);
        this.stage.addEventListener(MouseEvent.MOUSE_UP,   this.mouseUp);

    }

    /**
     * Draw line on sprite
     */
    private function mouseMove(e:MouseEvent):void {
        penSprite.graphics.lineTo( e.localX, e.localY );
    }

    /**
     * Draw sprite graphics to canvas and end drawing by cleaning up sprite graphics and unregistering movement listeners
     */
    private function mouseUp(e:MouseEvent):void {

        // draw sprite graphics to bitmap data (last parameter makes drawing smooth)
        canvasBitmap.bitmapData.draw( penSprite, null, null, null, null, true );

        // remove listeners 
        this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
        this.stage.removeEventListener(MouseEvent.MOUSE_UP,   mouseUp);

    }           

}

}

答案 1 :(得分:0)

好的,你的问题不是很清楚,我想你想要在Bitmap中复制你的内容。 如果这是你想要完成的,只需创建一个新的bitmapData,在其中绘制精灵并将其用作Bitmap的源。 例如,当您完成在penSprite中绘制时,可以将其复制到Bitmap中,如下所示:

 var bData:BitmapData = new BitmapData(penSprite.width, penSprite.height,true,0xFFFFFFFF);
 bData.draw(penSprite);
 var image:Bitmap=new Bitmap(bData);
 addChild(image);

希望这是你正在寻找的。

编辑: 我看到你只是在你的问题中添加了一条评论,说你要画“使用bitmapData.draw”。所以,我认为你误解了这种方法的用法。如果您查看文档:

“使用Flash运行时矢量渲染器将源显示对象绘制到位图图像上。”

所以你基本上用它在bitmapData中绘制另一个显示对象。