Flash/Flex学习笔记(14):制作涂鸦板

关键点知道如何画线就行了代码不复杂直接看吧从同事一本书上抄的

var drawing:Boolean;

Mouse.hide(); //隐藏默认的光标

stage.addEventListener(MouseEvent.MOUSE_MOVE,Stage_MouseMove);
stage.addEventListener(MouseEvent.MOUSE_DOWN,Stage_MouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP,Stage_MouseUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN,Stage_KeyDown);

function Stage_MouseDown(e:MouseEvent):void {
	this.graphics.moveTo(this.mouseX,this.mouseY);//设置划线的起点
	this.graphics.lineStyle(3,0x99cc00,100);//设置笔触大小及颜色
	drawing=true;
}


function Stage_MouseMove(e:MouseEvent):void {
	//铅笔鼠标跟随
	pen1.x=this.mouseX;
	pen1.y=this.mouseY;

	if (drawing) {
		this.graphics.lineTo(this.mouseX,this.mouseY);//关键代码就在这里
	}
	e.updateAfterEvent();
}

function Stage_MouseUp(e:MouseEvent):void {
	drawing=false;
}

function Stage_KeyDown(e:KeyboardEvent):void {
	if (e.keyCode==Keyboard.DELETE) { //注意这里虽然代码只写了Delete键但实际测试中发现如果按Ctrl + Delete组合键也能触发
		this.graphics.clear();
	}
}

 

 做为对比大家也可以参看:silverlight手写板/涂鸦/墨迹/InkPresenter示例程序

上一篇:Flash/Flex学习笔记(13):对象拖动(startDrag/stopDrag)


下一篇:Flash/Flex学习笔记(11):如何检测摄像头是否被占用