Simple Drawing in AS3

This code draws blue blocks onto the screen when the mouse is down and moving.

import flash.display.BitmapData;
import flash.geom.Rectangle;

var myBitmapData:BitmapData = new BitmapData(200,200, false, 0xFFCCCCCC);
var myBitmap:Bitmap = new Bitmap(myBitmapData,”auto”,false);
var maze:Sprite = new Sprite();
var size:Number = 10;
var offSet:Number = Math.floor(size/2);

maze.addChild(myBitmap);
addChild(maze)
maze.addEventListener(MouseEvent.MOUSE_DOWN, startDraw);
maze.addEventListener(MouseEvent.MOUSE_UP, endDraw);
maze.addEventListener(MouseEvent.ROLL_OUT, endDraw);

function startDraw(event:MouseEvent):void {
 maze.addEventListener(MouseEvent.MOUSE_MOVE, drawPoint);
}
function endDraw(event:MouseEvent):void {
 maze.removeEventListener(MouseEvent.MOUSE_MOVE, drawPoint);
}
function drawPoint(event:MouseEvent):void {
 var rect:Rectangle = new Rectangle(maze.mouseX-offSet, maze.mouseY-offSet,size,size)
 myBitmapData.fillRect(rect,0xFF0000FF);
}

Tags: , , ,

Leave a Reply