我正在使用Flex 4和AS3,我试图让用户可以用光标绘制一个手绘线 - 我已经完成了这部分。使用光标绘制手绘线条时,如何使其成为虚线?
但是,我也需要该行是一条虚线,而不是像现在这样一条实线。下面是我使用的代码。我已经找到了一些关于如何做到这一点的例子,但是它们都是直线,而不是徒手画线。
任何人都可以帮我完成这件事吗?
类文件(绘图区):
package
{
import flash.display.BitmapData;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.utils.ByteArray;
import mx.core.UIComponent;
import mx.events.FlexEvent;
import mx.graphics.codec.PNGEncoder;
public class DrawingArea extends UIComponent
{
private var isDrawing:Boolean = false;
private var x1:int;
private var y1:int;
private var x2:int;
private var y2:int;
public var drawColor:uint = 0x0000FF;
public function DrawingArea()
{
super();
addEventListener(FlexEvent.CREATION_COMPLETE, function(event:FlexEvent):void {
graphics.clear();
graphics.beginFill(0xffffff, 0.00001);
graphics.drawRect(0, 0, width, height);
graphics.endFill();
});
addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
addEventListener(MouseEvent.MOUSE_UP, mouseUp);
function mouseDown(event:MouseEvent):void {
x1 = mouseX;
y1 = mouseY;
isDrawing = true;
}
function mouseMove(event:MouseEvent):void {
if (!event.buttonDown)
{
isDrawing = false;
}
x2 = mouseX;
y2 = mouseY;
if (isDrawing)
{
graphics.lineStyle(1, drawColor);
graphics.moveTo(x1, y1);
graphics.lineTo(x2, y2);
x1 = x2;
y1 = y2;
}
}
function mouseUp(event:MouseEvent):void {
isDrawing = false;
}
}
}
}
应用MXML代码:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<DrawingArea id="drawingArea" xmlns="*" width="100%" height="100%"/>
</s:Application>
感谢您的帮助提前!
感谢 - 如果你将鼠标移动速度快,但我现在必须弄清楚如何使它做同样的,如果你慢慢地移动鼠标,它的工作原理。有什么建议么? – Nutkraker
我已经使用此代码用于我的项目,并根据鼠标的移动完美地工作。我为你的问题重建了这段代码。请调试此代码找出错误显示的位置。 –