gpt4 book ai didi

actionscript-3 - Actionscript 徒手画类不起作用

转载 作者:行者123 更新时间:2023-12-04 05:06:21 32 4
gpt4 key购买 nike

我正在尝试编写一个简单的 as3 绘图类,但它不起作用。甚至没有触发事件。可能是什么问题?

用法:

var drawingBoard:Drawing = new Drawing();
drawingBoard.x = 0;
drawingBoard.y = 0;
drawingBoard.width = 200;
drawingBoard.height = 100;

addChild(drawingBoard);

类(class):
package {
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.MouseEvent;

public class Drawing extends Sprite {
private var prevX:Number = 0;
private var prevY:Number = 0;
private var board:Shape = new Shape();

public function Drawing() {
trace("Drawing initialized.");

addChild(board);

addEventListener(MouseEvent.MOUSE_DOWN, startDrawing);
addEventListener(MouseEvent.MOUSE_UP, stopDrawing);
addEventListener(MouseEvent.MOUSE_OUT, stopDrawing);
}

private function startDrawing(event:MouseEvent):void {
trace("Start drawing.");

addEventListener(MouseEvent.MOUSE_MOVE,handleDrawing);
}

private function stopDrawing(event:MouseEvent):void {
trace("Stop drawing.");

prevX = 0;
prevY = 0;
removeEventListener(MouseEvent.MOUSE_MOVE,handleDrawing,false);
}

private function handleDrawing(event:MouseEvent):void {
trace("Handling drawing.");

if (prevX == 0 || prevY == 0) {
prevX = event.localX;
prevY = event.localY;
} else {
board.graphics.lineStyle(10, 0x000000, 1);
board.graphics.moveTo(prevX, prevY);
board.graphics.lineTo(event.localX, event.localY);
prevX = event.localX;
prevY = event.localY;
}
}
}
}

最佳答案

首先设置width/height一个空 Sprite 不做任何事情。如果您不想拥有尺寸为 200/100 的空 Sprite ,则需要在其中绘制一些内容。

我修改了您的类(class),以便在您将其添加到舞台后开始工作:

package {
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.MouseEvent;

public class Drawing extends Sprite {
private var prevX:Number = 0;
private var prevY:Number = 0;
private var board:Shape = new Shape();

public function Drawing() {
trace("Drawing initialized.");

addChild(board);
//clear board to white
board.graphics.beginFill(0xffffff,1);
board.graphics.drawRect(0,0,200,100);
board.graphics.endFill();

addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}

private function onAddedToStage(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

addEventListener(MouseEvent.MOUSE_DOWN, startDrawing);
addEventListener(MouseEvent.MOUSE_UP, stopDrawing);
addEventListener(MouseEvent.MOUSE_OUT, stopDrawing);
}

private function startDrawing(event:MouseEvent):void {
trace("Start drawing.");

addEventListener(MouseEvent.MOUSE_MOVE,handleDrawing);
}

private function stopDrawing(event:MouseEvent):void {
trace("Stop drawing.");

prevX = 0;
prevY = 0;
removeEventListener(MouseEvent.MOUSE_MOVE,handleDrawing,false);
}

private function handleDrawing(event:MouseEvent):void {
trace("Handling drawing.");

if (prevX == 0 || prevY == 0) {
prevX = event.localX;
prevY = event.localY;
} else {
board.graphics.lineStyle(10, 0x000000, 1);
board.graphics.moveTo(prevX, prevY);
board.graphics.lineTo(event.localX, event.localY);
prevX = event.localX;
prevY = event.localY;
}
}
}
}

关于actionscript-3 - Actionscript 徒手画类不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15502061/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com