Am currently working on a tool created by a colleague of mine and would like to improve performance. Basically, it's a tool for drawing on screen and uses a combination of Sprites and the Graphics class to draw a line segment every time the mouse is moved when the mouse button is down. Here is the drawing bit:
// on MouseMove
protected function drawLine(e:MouseEvent):void {
if (currentTool.thickness > 0){
//pen
var line:Sprite = new Sprite();
line.graphics.lineStyle(currentTool.thickness, currentColour);
line.graphics.moveTo(lastKnownPoint.x, lastKnownPoint.y);
line.graphics.lineTo(e.localX, e.localY);
inkLayer.addChild(line);
lastKnownPoint.x = e.localX;
lastKnownPoint.y = e.localY;
e.updateAfterEvent();
} else {
//eraser
var inkChildren:int = inkLayer.numChildren;
for (var i:uint = inkChildren; i > 0; i--){
if (toolCursor.hitTestObject(inkLayer.getChildAt(i - 1))){
inkLayer.removeChildAt(i - 1);
}
}
}
}
As you can see, it checks if the line 'thickness' property and draws if it is and erases if it isn't.
I did think of using a technique similar to blitting here where it'd draw to a bitmap but I'm not sure this'd give the performance boost I want or if, indeed, there would be any way to have an eraser function.
Any ideas on a better way to do this? The drawing itself works nicely - this isn't the problem, it's the performance of the subsequent 'drawn' sprites.