AS3 - 在白板中创建文本字段

时间:2011-09-16 07:23:55

标签: actionscript-3 actionscript

我必须在Flash Action Script 3中创建一个白板。我无法在白框中创建文本框属性。当我打开swf我需要一个文本框属性,我可以创建一个文本框字段,用户希望。请帮忙..

1 个答案:

答案 0 :(得分:0)

这样的东西?


Whiteboard.as

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

    public class Whiteboard extends Sprite
    {

        private var _whiteboard : Sprite;
        private var _currentText : TextBox;

        public function Whiteboard()
        {
            super();

            createWhiteboard();

            enableUserInput();
        }

        private function createWhiteboard() : void
        {
            // create whiteboard sprite
            _whiteboard = new Sprite();

            // add to displaylist
            addChild(_whiteboard);

            // draw graphics
            with(_whiteboard.graphics)
            {
                lineStyle(10, 0x666666, 1);
                beginFill(0xFFFFFF, 1);
                drawRect(0, 0, 800, 600);
                endFill();
            }
        }

        private function enableUserInput() : void
        {
            _whiteboard.addEventListener(MouseEvent.CLICK, onUserInteract);
        }

        private function onUserInteract(event : MouseEvent) : void
        {
            // remove if empty
            if(_currentText && _currentText.htmlText.length == 0)
            {
                // remove from displaylist
                _whiteboard.removeChild(_currentText);
            }

            // add new
            if(event.target == _whiteboard)
            {
                _currentText = new TextBox();
                _currentText.x = event.stageX;
                _currentText.y = event.stageY;

                // add to displaylist
                _whiteboard.addChild(_currentText);
            }
            else
            {
                // use clicked text
                _currentText = event.target as TextBox;
            }

            // set selection
            _currentText.setSelection(0, _currentText.htmlText.length);

            // set focus
            stage.focus = _currentText;
        }

    }

}

import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFieldType;
import flash.text.TextFormat;

class TextBox extends TextField
{
    function TextBox()
    {
        super();

        background = true;
        backgroundColor = 0xFF88FF;
        multiline = false;
        autoSize = TextFieldAutoSize.LEFT;
        type = TextFieldType.INPUT;
        htmlText = "";
        selectable = true;
        defaultTextFormat = new TextFormat("_sans", 18, 0xFFFFFF);

    }
}

enter image description here