0

我在 AS3 中做一个小练习项目,但遇到了一个问题。我正在制作一个 MS Paint 风格的绘图程序,我希望用户能够使用输入字段更改画笔大小。我有两个问题。第一个是我不知道如何通过代码制作一个输入字段,第二个是我不知道如何将舞台上输入字段的变量传递给控制画笔大小的变量。

    package  
{
    import flash.display.Sprite;
    import flash.events.MouseEvent; 
    import flash.text.TextField;
    import flash.events.KeyboardEvent;

    public class DrawingApp extends Sprite
    {
        var square0:Sprite;
        var textField: TextField;
        var textField2: TextField;
        var textField3: TextField;
        var square1:Sprite;
        var square2:Sprite;
        var square3:Sprite;
        var lineColor:uint = 0x000000;
        var brushSize:Number = 1;

        public function DrawingApp() 
        {

            square0 = new Sprite();
            addChild(square0);  
            square0.graphics.lineStyle(1,0x000000);
            square0.graphics.beginFill(0x000000);
            square0.graphics.drawRect(0,0,30,20);
            square0.graphics.endFill();
            square0.x = 500;
            square0.y = 10;

            square1 = new Sprite();
            addChild(square1);  
            square1.graphics.lineStyle(1,0x000000);
            square1.graphics.beginFill(0x0000FF);
            square1.graphics.drawRect(0,0,30,20);
            square1.graphics.endFill();
            square1.x = 500;
            square1.y = 40;

            square2 = new Sprite();
            addChild(square2);  
            square2.graphics.lineStyle(1,0x000000);
            square2.graphics.beginFill(0xff0000);
            square2.graphics.drawRect(0,0,30,20);
            square2.graphics.endFill();
            square2.x = 500;
            square2.y = 70;

            textField = new TextField;
            addChild(textField);
            textField.x = 500;
            textField.y = 100;
            textField.width = 30;
            textField.height = 20;
            textField.text = "Eraser";
            textField.selectable = false;  
            textField.border = true; 

            textField2 = new TextField;
            addChild(textField2);
            textField2.x = 500;
            textField2.y = 130;
            textField2.width = 30;
            textField2.height = 20;
            textField2.text = "Clear";
            textField2.selectable = false;  
            textField2.border = true; 

            textField3 = new TextField;
            addChild(textField3);
            textField3.x = 500;
            textField3.y = 160;
            textField3.width = 30;
            textField3.height = 20;
            textField3.text = brushSize;
            textField3.selectable = true;  
            textField3.border = true; 


            init();
        }

        private function init():void
        {
            graphics.lineStyle(1, lineColor, 1);
            square0.addEventListener(MouseEvent.CLICK, changetoDefault);
            square1.addEventListener(MouseEvent.CLICK, changetoBlue);
            square2.addEventListener(MouseEvent.CLICK, changetoRed);
            textField.addEventListener(MouseEvent.CLICK, changetoEraser);
            textField2.addEventListener(MouseEvent.CLICK, clearAll);
            textField3.addEventListener(KeyboardEvent.KEY_DOWN, adjustSize);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseIsDown);
            stage.addEventListener(MouseEvent.MOUSE_UP, mouseIsUp);
        }

        private function changetoEraser(event: MouseEvent):void
        {
            lineColor = 0xffffff;
            graphics.lineStyle(brushSize, lineColor, 1);
        }

        private function adjustSize(event: KeyboardEvent)
        {
            brushSize = textField3.text;
            graphics.lineStyle(brushSize, lineColor, 1);
        }

        private function clearAll(event:MouseEvent):void
        {
            graphics.clear();
            graphics.lineStyle(brushSize, lineColor, 1);
        }

        private function changetoDefault(event: MouseEvent):void
        {
            lineColor = 0x000000;
            graphics.lineStyle(brushSize, lineColor, 1);
        }

        private function changetoBlue(event: MouseEvent):void
        {
            lineColor = 0x0000ff;
            graphics.lineStyle(brushSize, lineColor, 1);
        }

        private function changetoRed(event: MouseEvent):void
        {
            lineColor = 0xff0000;
            graphics.lineStyle(brushSize, lineColor, 1);
        }

        private function mouseIsDown(event: MouseEvent):void
        {
            graphics.moveTo(mouseX, mouseY);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, moveMyMouse);
        }

        private function mouseIsUp(event: MouseEvent):void
        {
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveMyMouse);
        }

        private function moveMyMouse(event:MouseEvent):void
        {
            graphics.lineTo(mouseX, mouseY);
        }
    }
}

我意识到将 BrushSize 设置为 textField3.text 不起作用,因为它将字符串转换为数字。我不知道如何才能做到这一点。感谢任何可以为我阐明这一点的人。

4

1 回答 1

0

TextField您可以通过制作 a并将其类型设置为Flash 中的代码来制作输入字段TextFieldType.INPUT

为了将字符串转换为数字,您必须使用parseFloat()or parseInt()(我不知道您是否需要整数或浮点数)。

虽然很少,但我认为这应该足以帮助你重新开始。

于 2012-04-23T15:10:32.047 回答