我正在开发一个Paint Application是AS3。我想要的是基本上模仿MS Paint。我需要创建画笔工具。刷子工具中有很多形状(刷子的尖端),如sqaure,circle,rhombus。我最初计划使用图形类,但有些聪明的人建议我使用Bitmap类。我已经开发了这个类,它似乎工作正常。但现在影响我的两个问题
包{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.*;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.ui.Keyboard;
[SWF(width=600,height=400,backgroundColor=0x000000)]
/**
* Demonstrates BitmapData's draw() command by duplicating the entirety of the stage using draw()
* whenever the space bar is pressed. A smaller copy is also drawn onto the stage to show how draw()
* can be used to transform the pixels in the image being drawn.
*/
public class DrawTest extends Sprite {
private var _circle:Sprite;
private var _bitmapData:BitmapData;
/**
* Constructor. Creates bitmap that will be drawn into and circle that will follow the mouse.
* A listener is also set up to handle then a key is pressed.
*/
public function DrawTest() {
createBitmap();
createCircle();
stage.addEventListener(MouseEvent.MOUSE_DOWN,_handleMouseEventBrush);
stage.addEventListener(MouseEvent.MOUSE_UP,_handleMouseEventBrush);
}
/**
* Creates bitmap data that the stage contents will be drawn into and adds this to the stage.
*/
private function createBitmap():void {
_bitmapData=new BitmapData(stage.stageWidth,stage.stageHeight,true,0x000000);
var bitmap:Bitmap=new Bitmap(_bitmapData);
addChild(bitmap);
}
/**
* Creates a circle shape that is set to follow the mouse as it moves.
*/
private function createCircle():void {
_circle=new Sprite ;
_circle.graphics.beginFill(0xFF0000);
_circle.graphics.drawCircle(0,0,10);
_circle.graphics.endFill();
addChild(_circle);
_circle.x=stage.mouseX;
_circle.y=stage.mouseY;
_circle.startDrag();
}
private function _handleMouseEventBrush(e:MouseEvent):void {
switch (String(e.type)) {
case "mouseDown" :
_bitmapData.draw(stage);
stage.addEventListener(MouseEvent.MOUSE_MOVE,_handleMouseEventBrush);
break;
case "mouseUp" :
stage.removeEventListener(MouseEvent.MOUSE_MOVE,_handleMouseEventBrush);
break;
case "mouseMove" :
_bitmapData.draw(stage);
}
}
}
有没有人想过如何克服这个问题。
答案 0 :(得分:1)
您可以尝试使用Event.ENTER_FRAME来执行绘图命令。 MOUSE_MOVE执行得更频繁,屏幕由闪存重新渲染,具体取决于帧速率。但我不确定这是否更好。 招呼
答案 1 :(得分:0)