Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 如何使用BitmapData用铅笔画图?_Actionscript 3_Bitmapdata - Fatal编程技术网

Actionscript 3 如何使用BitmapData用铅笔画图?

Actionscript 3 如何使用BitmapData用铅笔画图?,actionscript-3,bitmapdata,Actionscript 3,Bitmapdata,现在我只有这段代码,但我没有使用BitmapData.draw()。如何使用BitmapData.draw()编写代码 好的,你的问题不是很清楚,我想你想把内容复制到位图中。 如果这是您想要完成的,只需创建一个新的bitmapData,在其中绘制精灵并将其用作位图源。 例如,当完成绘制penSprite时,可以将其复制到位图中,如下所示: var bData:BitmapData = new BitmapData(penSprite.width, penSprite.height,true,0

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


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

 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中绘制另一个显示对象

这是基于您的代码的代码示例。你可以试着把它作为电影的一个基类

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);

    }           

}

}

你到底想做什么?我想用我的鼠标或钢笔用bitmapData.draw()画图,在代码中,我没有使用bitmapData.draw()。
。bitmapData.draw()是将flash显示对象光栅化到位图表面的关键。您可以使用
.setPixel()
直接绘制位图,可能是使用类似“Ok”的方法,我现在尝试了它,但发现以下错误:TypeError:error#1009:无法访问空对象引用的属性或方法。在DrawingApp()[/Users/VVT/Documents/Adobe Flash Builder 4.6/SuperDraw/src/DrawingApp.as:25]在SuperDraw()[/Users/VVT/Documents/Adobe Flash Builder 4.6/SuperDraw/src/SuperDraw.as:59]好的,现在它可以工作了。。。您的代码工作完美,谢谢。。。问题解决
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);

    }           

}