<< Chapter < Page Chapter >> Page >

An object of the class named NumericTextAreaA

Figure 3 shows the visual manifestation of an object of this class with two lines of numeric and space characters having been entered. (The entry of additional lines of text causes scroll bars to automatically appear.)

An object of the class named numerictextareaa.

Missing image.
An object of the class named NumericTextAreaA.

A class definition is probably required

It is possible to create simple custom components by embedding ActionScript code in a Flex MXML file. However, I don't believe that it is possible tocreate an object with this behavior without defining a new ActionScript class. Even if it is possible, defining a new class is the approach that makes the mostsense to me.

The file named NumericTextArea01.mxml

The MXML code beginning with cc in Listing 3 instantiates the object shown in Figure 3.

The file named numerictextarea01.mxml.

<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"xmlns:cc="CustomClasses.*"><cc:NumericTextAreaA/></mx:Application>

The class definition for NumericTextAreaA

Listing 4 shows the class definition from which the object shown in Figure 3 was instantiated.

The class definition for numerictextareaa.

/*This is a custom component. This class extends the TextArea class. It recognizes only the followingcharacters: 0, 1, 2, 3, 4, 5, 6, 7, 8,9space backspacereturn key The next numeric character is appended onto the end ofthe string in the text area regardless of the current position of the cursor.The backspace key deletes characters from the end of the string, one character at a time.*********************************************************/ package CustomClasses{import mx.controls.TextArea; import mx.controls.Alert;import flash.events.* public class NumericTextAreaA extends TextArea{private var theText:String = "";public function NumericTextAreaA(){ this.addEventListener("keyUp",processKey);}//end constructor private function processKey(event:KeyboardEvent):void{ if(event.charCode==48){theText+="0"; }else if(event.charCode==49){theText+="1"; }else if(event.charCode==50){theText+="2"; }else if(event.charCode==51){theText+="3"; }else if(event.charCode==52){theText+="4"; }else if(event.charCode==53){theText+="5"; }else if(event.charCode==54){theText+="6"; }else if(event.charCode==55){theText+="7"; }else if(event.charCode==56){theText+="8"; }else if(event.charCode==57){theText+="9"; }else if(event.charCode==8){//backspacetheText= theText.substr(0,theText.length-1);}else if(event.charCode==32){//space theText+=" ";}else if(event.charCode==13){//return key theText+="\n";}//end else this.text=theText;}//end processKey method }//end class}//end package

May not be familiar code

There may be quite a lot of code in Listing 4 with which you are not familiar. However, it is not my purpose in writing this lesson to get into thedetails of defining classes in ActionScript, so I won't take the time to explain this code in this lesson. Suffice it to say that Listing 4checks the character code associated with each keystroke in the text area and rejects all but the numeric characters, the backspace character, the spacecharacter, and the return key.

I will explain code like this in detail in future lessons. For now, simply accept this as an example of why you may need to learn OOP in order toadvance your career as an ActionScript programmer.

Running the ActionScript program named NumericTextArea01

If you have the Flash Player plug-in (version 9 or later) installed in your browser you should be able to run this program by clicking on NumericTextArea01 .

Enter some alphabetic and numeric text in the white box to see how the GUI component behaves. Click the "Back" button in your browser to returnto this page when you are finished experimenting with the component.

If you don't have the proper Flash Player installed, you should be notified of that fact and given an opportunity to download and install the Flash Playerplug-in program.

Resources

I will publish a list containing links to ActionScript resources as a separate document. Search for ActionScript Resources in the Connexions searchbox.

Miscellaneous

Housekeeping material
  • Module name: What is OOP and Why Should I Care?
  • Files:
    • ActionScript0104\ActionScript0104.htm
    • ActionScript0104\Connexions\ActionScriptXhtml0104.htm
PDF disclaimer: Although the Connexions site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.

-end-

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with actionscript. OpenStax CNX. Jun 04, 2010 Download for free at http://cnx.org/content/col11202/1.19
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?

Ask