1 / 31

Text and Arrays

7. Text and Arrays. Topic. Objectives of this topic:. You can …. create, style and scroll text fields using AS. hold multiple variables using arrays. Overview. Creating a Text Field Styling a Text Field Capturing Data from a Text Field Loading External Text Arrays

hayley
Download Presentation

Text and Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 7 Text and Arrays Topic

  2. Objectives of this topic: You can … create, style and scroll text fields using AS hold multiple variables using arrays

  3. Overview • Creating a Text Field • Styling a Text Field • Capturing Data from a Text Field • Loading External Text • Arrays • Using Text and Arrays to Create a Game

  4. Creating a Text Field • Text field can be created easily using Adobe Flash’s tools • The TextField class is used to create display objects for text display and input. • All dynamic and static input text fields in a SWF file are instances of the TextField class.

  5. A text field is given an instance name in the Property inspector and use the methods and properties of the TextField class to manipulate it with ActionScript.

  6. Using AS code, the designer can create a text field dynamically  text field will be displayed when necessary without provide a space and resources Var myText:TextField = new TextField(); addChild(myText);

  7. The text property of TextField class accepts a string  insert any combination numbers and letters in the quotation marks Example: myText.text = “my brand new text field.”;

  8. ActionScript provides several ways to format your text at runtime. • The TextFormat class lets you set character and paragraph formatting for TextField objects. • We can apply Cascading Style Sheets (CSS) styles to text fields by using the TextField.styleSheet property and the StyleSheet class.

  9. Styling a Text Field • Styling a text field by changing the font face, font color and font size through AS using a TextFormat object • Steps to styling a text field: • Create a TextFormat object  creating a style sheet for the text field • define all the formatting in a single object which can be linked to as many text fields as we want • Modify some of the TextField attributes directly using its properties (textColor, textHeight etc) var myFormat:TextFormat = new TextFormat();

  10. Define the TextFormat properties myFormat.font = “Helvetica”; myFormat.color = 0xFF0000; myFormat.size = 24; • Color property accepts a number, a hexadecimal value – is a six-digit number representing the RGB that make up a hue. • First two digit  red • Second two digit  green • Last two digit  blue

  11. Hexadecimal Colors Value

  12. Parsing the formatting to the text field by specifying the TextFormat object’s name in the parenthesis myText.setTextFormat(myFormat);

  13. Automatic resizing the text field can be done by increasing the width of the text field automatically using autoSize property of TextField • Must define which side the field should grow (eg. LEFT) myText.autoSize = TextFieldAutoSize.LEFT;

  14. Capturing Data from a Text Field • Capture data from an input text field and display it somewhere else • Input text field consists two type: • Static input • Text is created in AS code (hard coded) • Dynamic input • Input text doesnot display text, but it does allow to user to insert text

  15. Dynamic text field: • When it used in combination with AS, it will display as a text based on our instruction • There are a lot of properties for this object • By giving a name _txt suffice, it will display only a single line of text and the field will not be selected once the movie is exported

  16. Example: var body_txt:TextField = new TextField(); var yourName:String; body_txt.x = 98; body_txt.y = 66; body_txt.autoSize = TextFieldAutoSize.LEFT; submit_btn.addEventListener(MouseEvent.CLICK, onClick); function onClick(event:MouseEvent):void { addChild(body_txt); yourName = name_txt.text; board_txt.text = yourName; }

  17. Loading External Text • To fill a text field with text loaded from an external  add text to the field by • assigning a string to the TextField’s text property, or • loading it from the external file • Steps: • Create a new object, URLRequest variable to hold the information about where the file is located varexternalReq:URLRequest = new URLRequest(“external.txt”);

  18. 2. load data from a file external into Flash document using the load() method varexternalLoad:URLLoader = new URLLoader(); • Build a relationship between the two objects externalLoad.load(externalReq);

  19. 4. specify the destination for the data • add an event listener to URLLoader – trigger the function when the load is complete that will add the data to the text field. externalLoad.addEventListener(Event.COMPLETE, textReady); • COMPLETE is a type of event triggered when any methods or functions that are run on an object are complete

  20. 5. create an event handler that makes the text property of the text field equal to the data of the event target function textReady(event:Event):void { external_txt.text = event.target.data; }

  21. Wrap the text using wordWrap property to make the paragraphs will be spaced over more than one line external_txt.wordWrap = true;

  22. Scroll the Text • Create the control buttons to scroll up and down. • add event listener to the buttons • create an event listener using scrollV property to scroll the external text. Example: external_txt.scrollV ++;

  23. up_btn.addEventListener(MouseEvent.CLICK, scrollUp); down_btn.addEventListener(MouseEvent.CLICK, scrollDown); function scrollUp(event:MouseEvent):void { external_txt.scrollV --; } function scrollDown(event:MouseEvent):void { external_txt.scrollV ++; }

  24. Understanding Arrays • Array – a special type of variable that can hold multiple variables also known as a container for whatever type of data Example: Develop a Web site with basic security, to check the input against a list of registered users. The designer can use an array to hold the list of the user names for all the registered users of the Web site.

  25. An array can contain: • objects other than strings, and • a set of variables that are not even of the same class • Define or create a new array Example: var users:Array = new Array();

  26. Giving an initial input to array: Example: users[0] = “Todd”; users[1] = “Jimmy”; Or; var users:Array = ["Todd", "Jimmy"];

  27. Adding an input data to array using push() method Example: users.push(user_txt.text);

  28. To check every value in the array, create a conditional loop such as for statement that also contain an index • The advantage of using loop statement: • check for matching strings, and • check for matching index numbers for (var i:Number =0; i < users.length; i++);

  29. Array’s length property is refer to the number of values in the array • Loop statement will run over and over again, by increasing the value of i by one every time, until i is less than the length of the array

  30. Example: var users:Array = ["Todd", "Jimmy", "Mary"]; enter_btn.addEventListener(MouseEvent.CLICK, onClick); function onClick(event:MouseEvent):void { for (var i:Number =0; i < users.length; i++) { if(users[i] == user_txt.text) { trace("Access granted"); } } }

  31. Using Text and Arrays to Create a Game Interactive Word Game • Once the user types their name, a verb and a noun, and click generate, Flash will generate a sentence using those terms.

More Related