
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Working with Strings
|
103
Character Code Functions
In “Unicode escape sequences,” we saw how to insert characters into a string using
escape sequences. ActionScript also includes two built-in functions for working with
character code points in strings: fromCharCode() and charCodeAt().
Example 4-4. Character case animation
// Add a new caseAni( ) method to the built-in TextField class.
TextField.prototype.caseAni = function (msg) {
// Store a reference to the current text field in tf. We'll need to
// access it from the nested function moveUppercaseLetter( ).
var tf = this;
tf.upperIndex = 0; // The current location of the uppercase letter.
tf.msg = msg; // The message to animate.
// Start the animation running by calling
// the moveUppercaseLetter( ) function every 100 milliseconds.
var id = setInterval(moveUppercaseLetter, 100);
// Define the nested moveUppercaseLetter( ) function. Here's where we do
// all our string manipulation.
function moveUppercaseLetter ( ) {
// Chop the msg string into three pieces and make the middle one uppercase.
var part1 = tf.msg.slice(0, tf.upperIndex);
var part2 = tf.msg.charAt(tf.upperIndex);
var part2 = part2.toUpperCase( );
var part3 = tf.msg.slice(tf.upperIndex+1, tf.msg.length);
tf.msg = part1 + part2 + part3;
// Display the formatted msg ...