Hallg3/FlashCS5:Importing text

From virtualMV2015wiki
Jump to: navigation, search
 Home  Flash10 (CS5) <


Introduction - Importing Text and CSS to a TextBox and using scroll commands to veiw the text

Linking Text and CSS to a TextBox and applying scroll buttons with AS3

Icon Objective.png

Objective

By the end of this page you will be able to:

  • Link a text document (txt or xml) and css document to a text box in Flash CS4 or CS5 using AS3
  • Be able to apply scroll buttons to the text box using AS3

Create documents

Text Document

Create a text document named text.xml using notepad or similar (you could name this text.txt but will need to rename this within the AS3 code).

Add required text to this document and formate it with HTML code such as

 <p>_</p>, <h1>_</h1>, <li>_</li>, etc.


CSS Document

Create a CSS document named styles.css using notepad or similar.

Formate styles using HTML code

p {fontFamily: Arial; color:#ffffff; text-align: left; font-weight:normal; font-size: 11px;} etc.


Importing into Flash

Flash Movie

Create a new flash movie with 2 layers.

Name one layer text_field and place a text box in it. Give the Text box an Instance name of tField in the properties box and select Dynamic Text. Ensure the Render text as HTML box is activated.

Name the other layer as actions and add the following script

/*############## Import text and CSS ####################*/
 
/*create an URLRequest object this object takes as 
parameter the path to our file */
var cssUrl:URLRequest = new URLRequest("styles.css");
 
/*create an URLLoader object, (this will load our file ) 
and pass the path to our file (the path is hold by URLRequest object)*/
var cssLoader:URLLoader = new URLLoader(); 
cssLoader.load(cssUrl); 
 
/*add a COMPLETE event that will fire when our file is complete loaded*/
cssLoader.addEventListener(Event.COMPLETE,cssLoaded); 
 
/*Inside the cssLoaded function that handles the COMPLETE event 
we create a StyleSheet object, parse the file loaded by our 
URLLoader as a CSS file and on the last line we setup assign 
the css object to the styleSheet property of the our tField.*/
function cssLoaded(event:Event):void 
{ 
var css:StyleSheet = new StyleSheet(); 
css.parseCSS(URLLoader(event.target).data); 
tField.styleSheet = css; 
} 
 
/*In first section we create an URLRequest object and passed 
the path to our txt file.This object knows where our file is.*/
var textUrl:URLRequest = new URLRequest("text.xml"); 
 
/*Next we create an URLLoader object, this will load our txt file.*/
var textLoader:URLLoader = new URLLoader(); 
textLoader.load(textUrl); 
 
/*We add an Event.COMPLETE to our textLoader, that will fire when 
our external file is loaded complete. */
textLoader.addEventListener(Event.COMPLETE,fileLoaded); 
 
/*We assign the loaded text to our text_field this 
process is handle by the fileLoaded function.*/
function fileLoaded(event:Event):void 
{
tField.htmlText= event.target.data;
tField.background = false;
}

Press control/enter and the text should be seen in the text box.


Using scroll commands to veiw imported text

Applying up and down scroll buttons to a text box with AS3

Icon Objective.png

Objective

By the end of this page you will be able to:

  • * Be able to apply scroll buttons to the text box using AS3

Add scroll arrows

Scroll Arrows

In Flash, create a new layer and call it buttons Open windows/Common Libraries/Buttons and choose and place a button with an arrow on it on the stage. Rotate the button so the arrow is pointing up and give it an instance name scrollUp

Copy the button onto the same layer (so there are two buttons) and rotate the new istance 180 degrees so the arrow is pointing down and give it an instance name scrollDown

Position the up button at the top right of the text box and the down button at the bottom right of the text box

Click on the actions layer, open the actionscript box and enter the following code

/*############## Scroll buttons ####################*/
//------------------make buttons to scroll the loaded text------- 
var scrolling:Boolean = false; 
var scrollDirection:String; 
scrollDown.addEventListener(MouseEvent.MOUSE_DOWN,scrollTextDown); 
scrollUp.addEventListener(MouseEvent.MOUSE_DOWN,scrollTextUp); 
stage.addEventListener(MouseEvent.MOUSE_UP,stopScroll); 
 
function scrollTextDown(event:MouseEvent):void 
{ 
scrolling = true; 
scrollDirection = "down"; 
tField.scrollV +=2; 
stage.addEventListener(Event.ENTER_FRAME,checkButtons); 
} 
 
function scrollTextUp(event:MouseEvent):void 
{ 
scrolling = true; 
scrollDirection = "up"; 
tField.scrollV -=2; 
stage.addEventListener(Event.ENTER_FRAME,checkButtons); 
} 
 
function stopScroll(event:MouseEvent):void 
{ 
stage.removeEventListener(Event.ENTER_FRAME,checkButtons); 
scrolling = false; 
} 
//---------ckecking to see if buttons are pressed----------- 
function checkButtons(event:Event):void 
{ 
if (scrolling) 
	{
	if (scrollDirection =="down") 
	{
		tField.scrollV +=2; 
	} 
	else if (scrollDirection == "up") 
		{ 
		tField.scrollV -=2; 
		} 
	} 
} 
}

Press control/enter and the text should be seen in the text box and scrollable. Hallg3 22:17, 4 May 2011 (EDT)

Resources


Icon References.png References


Hallg3/FlashCS5:Importing text. (2018). In virtualMV's ( Michael Verhaart ) wiki. Retrieved January 19, 2018, from http://www.virtualmv.com/wiki/index.php?title=Hallg3/FlashCS5%3AImporting_text    (zotero)