PDA

View Full Version : Clear function in simple paint program



SchmackLab
March 26th, 2009, 11:48 AM
So i have been messing around with a script found for a simple paint program. After adapting the code a bit for my own needs and getting an understanding of how it works I had a couple questions.

first of all I need to create a button that will clear the stage (or more specifically lvg1)
second what does e reference in lines similar to


e.target.graphics.lineTo(e.localX,e.localY);
Thank you for your help!



import flash.events.MouseEvent;
import flash.geom.Matrix;

// flag per il detect del mousedown
var md:Boolean=false;

// creo una lavagna front
var lvg1:Sprite = new Sprite();

lvg1.graphics.lineStyle(0,0x999999);
lvg1.graphics.beginFill(0x999999);
lvg1.graphics.drawRect(0,0,550,400);
lvg1.graphics.endFill();
addChild(lvg1);
lvg1.x=0;
lvg1.y=0;

lvg1.addEventListener(MouseEvent.MOUSE_DOWN, _onMouseDown);
lvg1.addEventListener(MouseEvent.MOUSE_MOVE, _onMouseMove);
lvg1.addEventListener(MouseEvent.MOUSE_UP, _onMouseUp);

function _onMouseDown(e:MouseEvent):void
{
trace("_onMouseDown");
var c:uint=0x666666;
e.target.graphics.lineStyle(50,c,1);
e.target.graphics.moveTo(e.localX,e.localY);
md=true;
}


function _onMouseUp(e:MouseEvent):void
{
md=false;
}

function _onMouseMove(e:MouseEvent):void {

trace("_onMouseMove");

if (md) {
e.target.graphics.lineTo(e.localX,e.localY);
}
}

pixelsguy
March 26th, 2009, 12:53 PM
you will see e:MouseEvent or e:Event in the function declaration arguments. e is the event object that triggered the function. e.target on a mouse event is the mouse event object's target object. with a click, it is whatever was clicked. however, you should be using currentTarget as it is designed to replace 'target' in AS3, and from my experience works better.


to clear the graphics, just use this in your clear button actions-
lvg1.graphics.clear

SchmackLab
March 26th, 2009, 01:11 PM
thanks pixels, works like a charm!

I really appreciate your help!