Thursday, September 6, 2007

Text Fields

This is how you can create a new text field with actionscript yo (amongst other things):
var myText:TextField = new TextField(); //make a new text field
var myFormat:TextFormat = new TextFormat(); //make text format object

myFormat.font = "Verdana"; //set font
myFormat.color = 0xFF0000; //set text color
myFormat.size = 14; //set size

addChild(myText); //add it to the stage (top-left)
myText.text = "text goes in here"; //put text into it
myText.autoSize = TextFieldAutoSize.LEFT; //text will auto size from the left

myText.setTextFormat(myFormat); //You need this before you can modify proporties



Capture data from text field:
//assuming there is a button called "submit_btn" and a text field called "name_txt"

var body_txt:TextField = new TextField(); //create a text field called "body_txt"
yourName:String; //set up variable
body_txt.x = 98; //you can place text where ever
body_txt.y = 66;

submit_btn.addEventListener(mouseEvent.CLICK, onClick); //set up click mouse event

function onClick(event:MouseEvent):void{
addChild(body_txt); //place body_txt on stage (this is where user will type)
yourName = body_txt.text; //put whatever user types into yourName variable
name_txt.text = "Hi " + yourName + "!"; //put that text in the name_txt field (plus "hi")
}


Loading External Text File:
//Assume there's a field called external_txt

var externalReq:URLRequest = new URLRequest("news.txt"); //new URL Request (request file)
var externalLoad:URLLoader = new URLLoader(); //new URL Loader

externalLoad.load(externalReq); //Start Loading

externalLoad.addEventListener(Event.COMPLETE, textReady); //Listen for load completion

external_txt.wordWrap = true; //this will wrap the text

function textReady(event:Event):void{
external_txt.text = event.target.data; //places data in txt file into text field
}

Scroll Text
Assume there is a text field full of dynamic text called myText_txt. Also assume there is a button called up_btn and one called down_btn:

up_btn.addEventListener(MouseEvent.CLICK, scrollUp);
down_btn.addEventListener(MouseEvent.CLICK, scrollDown);

function scrollUp(event:MouseEvent):void{
external_txt.scrollV --; //this moves the text down
}

function scrollDown(event:MouseEvent):void{
external_txt.scrollV ++; //this moves the text up
}

No comments: