Reputation: 105
Right now I only have this code, but I'm not using BitmapData.draw()
. How can I write my code using 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;
}
Upvotes: 0
Views: 1522
Reputation: 1377
Here's the code sample based on you code. You try it out by just making it a base class of the movie.
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);
}
}
}
Upvotes: 1
Reputation: 5740
Ok, your question is not very clear, I suppose that you want to copy your content in a Bitmap. If this is what you want to accomplish, just create a new bitmapData, draw your sprite in it and use it as a source of a Bitmap. For example, when you finish to draw in your penSprite, you can copy it in a Bitmap like this:
var bData:BitmapData = new BitmapData(penSprite.width, penSprite.height,true,0xFFFFFFFF);
bData.draw(penSprite);
var image:Bitmap=new Bitmap(bData);
addChild(image);
Hope this is what you're looking for.
EDIT: I see that you just add a comment in your question, saying that you want to draw "using bitmapData.draw". So, I think you are misunderstanding the use of that method. If you look in the documentation:
"Draws the source display object onto the bitmap image, using the Flash runtime vector renderer. "
So you basically use it to draw another display object in a bitmapData.
Upvotes: 0