Right now I’m still reading “Learning ActionScript 3.0” book so I can improve my AS 3 skills. In the “Draving with pixels” chapter there is a great example of a little drawing application that I like. Here you can see the code for that little application:
var mouseIsDown:Boolean;
var erasing:Boolean;
var canvas:Sprite = new Sprite();
addChild(canvas);
var w:Number = stage.stageWidth;
var h:Number = stage.stageHeight;
var bmd:BitmapData = new BitmapData(w, h, false, 0xffffffff);
var bm:Bitmap = new Bitmap(bmd);
canvas.addChild(bm);
var brush:Sprite = createBrush(0x66cc00);
var eraser:Sprite = createBrush(0xffffff);
function createBrush(col:uint):Sprite{
var sp:Sprite = new Sprite();
sp.graphics.beginFill(col, 1);
sp.graphics.drawCircle(0, 0, 30);
sp.graphics.endFill();
return sp;
}
canvas.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
canvas.addEventListener(MouseEvent.MOUSE_UP, onUp);
this.addEventListener(Event.ENTER_FRAME, onLoop);
function onDown(e:MouseEvent):void{
mouseIsDown = true;
if(e.shiftKey)
erasing = true;
}
function onUp(e:MouseEvent):void{
mouseIsDown = false;
erasing = false;
}
function onLoop(e:Event):void{
if(mouseIsDown && erasing){
eraser.x = mouseX;
eraser.y = mouseY;
bmd.draw(eraser, eraser.transform.matrix);
}else if(mouseIsDown){
brush.x = mouseX;
brush.y = mouseY;
bmd.draw(brush, brush.transform.matrix);
}
} |
var mouseIsDown:Boolean;
var erasing:Boolean;
var canvas:Sprite = new Sprite();
addChild(canvas);
var w:Number = stage.stageWidth;
var h:Number = stage.stageHeight;
var bmd:BitmapData = new BitmapData(w, h, false, 0xffffffff);
var bm:Bitmap = new Bitmap(bmd);
canvas.addChild(bm);
var brush:Sprite = createBrush(0x66cc00);
var eraser:Sprite = createBrush(0xffffff);
function createBrush(col:uint):Sprite{
var sp:Sprite = new Sprite();
sp.graphics.beginFill(col, 1);
sp.graphics.drawCircle(0, 0, 30);
sp.graphics.endFill();
return sp;
}
canvas.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
canvas.addEventListener(MouseEvent.MOUSE_UP, onUp);
this.addEventListener(Event.ENTER_FRAME, onLoop);
function onDown(e:MouseEvent):void{
mouseIsDown = true;
if(e.shiftKey)
erasing = true;
}
function onUp(e:MouseEvent):void{
mouseIsDown = false;
erasing = false;
}
function onLoop(e:Event):void{
if(mouseIsDown && erasing){
eraser.x = mouseX;
eraser.y = mouseY;
bmd.draw(eraser, eraser.transform.matrix);
}else if(mouseIsDown){
brush.x = mouseX;
brush.y = mouseY;
bmd.draw(brush, brush.transform.matrix);
}
}
And if you click here you can see the result (click to draw and hold the shift key + click to erase)
This book has a lot of good examples and it’s very easy to understand. I recommend it to anyone how wants to start with ActionScript 3.0 😉