02.02.2013 Views

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Flash</strong> <strong>MX</strong> <strong>2004</strong> <strong>Games</strong><br />

Extracting a letter from a string<br />

Line 5 uses the method ‘charAt’ <strong>to</strong> extract a single letter from the string, ‘person’. The number in<br />

the brackets is a parameter passed <strong>to</strong> the method, the index of the letter <strong>to</strong> extract, starting with<br />

zero as the first letter from the left. The letter with index 2 in ‘Steve’ is the first ‘e’.<br />

Extracting a section from a string<br />

Another useful method is ‘substr’; this takes two parameters, first where <strong>to</strong> start extracting as an<br />

index and the second the number of characters <strong>to</strong> extract. Using the parameters 4 as the index and<br />

6 as the length from the string, ‘desc’, gives the sub-string ‘winner’.<br />

Dividing up a string<br />

The method ‘split’ can be used <strong>to</strong> divide a string in<strong>to</strong> separate letters or words. It takes a single<br />

parameter, in this example we use‘’(asingle space). The return value is a list of all the words that<br />

we can display, separated by commas using the trace command as shown in line 7.<br />

Finding a string inside another<br />

Line 8 shows how <strong>to</strong> search through a string <strong>to</strong> find another. It returns either the index <strong>to</strong> the<br />

sub-string or -1 if the sub-string cannot be found.<br />

Getting the code value of a character<br />

Characters in a string are given code values based on the ASCII character codes. You can find a<br />

table of codes at http://www.asciitable.com/. Line 9 shows how this can be done.<br />

Setting a character using the code value<br />

Line 11 shows how you can use the method ‘fromCharCode’ which takes a single parameter, the<br />

ASCII code and returns a string. In this example using the ASCII value 65 returns a capital letter A.<br />

Converting case<br />

The string object contains methods <strong>to</strong> convert the entire string <strong>to</strong> either upper (<strong>to</strong>UpperCase) or<br />

lower (<strong>to</strong>LowerCase) case.<br />

Although strings are usually used <strong>to</strong> s<strong>to</strong>re textual information, they can be useful <strong>to</strong> s<strong>to</strong>re more<br />

complex data in a very compact way. For example the string<br />

W12U6T<br />

could be used <strong>to</strong> represent walk forward 12 paces (W12), then up 6 steps (U6) and finally turn (T).<br />

A simple code reader could extract this information using the string methods. In this way you<br />

could create complex new actions using a simple code formula.<br />

92

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!