I have to create a white board in Flash Action Script 3.. I'm unable to create the text box property in the white box. When i open the swf i need a text box property with whch i can create a text box f开发者_JS百科ield where ever the user wishes. Please help..
Something like this?
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);
}
}
精彩评论