Quiz Review

 

Homework Review

 

??????

Where are they? Only 8 people handed in! Only 4 included a doc! Whatup?

Errata

 

How to setFocus

Source

In the playGame_btn and interface_mc.enter_btn callbacks put:

selection.setFocus ("interface_mc.input");

getRandom Does Work!

The original reference in class09


Source

The trick was in mistaking a string variable for a number variable.

More And More Objects

 

Methods And Properties

Anything in flash with methods and/or properties is an Object.

 

Built-in

Math.random()

Color.getRGB

 

Assigned-name

myClip_mc._x

myClip.gotoAndPlay()

myStringVariable.length()

myArray.push("last thing")

 

Customized - build your own

myObject.someMethod(argument)

 

There are 33 objects in Flash MX ActionScript,
we've covered:

arguments

Array

Color

Function

_global

Math

_root

Useful Objects

Source

 

Date Object

37 methods!

Create a date object with the current date in it

myDate = new Date()

 

This fixes the current date in an object. You can check it later to see when it was set. Different than:

myDate = new Date()

 


The Code...


Traces As

myDate

Mon May 5 20:15:53 GMT-0700 2003

myDate.getDat()

5

myDate.getDay()

1

myDate. getFullYear()

2003

myDate.getHours

20

myDate. getMilliseconds()

983

myDate.getMinutes()

15

myDate.getTime()

10520018582938
Based on the number of seconds since midnight January 1, 1970, universal time

myDate. getTimeZoneOffset()

420

myDate. getUTCDate()

4

myDate.getYear()

103

*These numbers are not "live"

 

Number weirdness - remember Array[0]?


Parameter


Format

year

0 to 99 indicates 1900 though 1999; otherwise all four digits of the year must be specified

month

0 (January) to 11 (December)

date

1 to 31, not 0 to 30

hour

0 (midnight) to 23 (11 p.m.).

minute

0 to 59

second

0 to 59

millisecond

0 to 999

 

You store a date in the Date object too:

Code this:

new Date(year, month, date, hour, minute, second, millisecond)

myBirthday = new Date(1954,6 ,19, 5, 23,59,999 );

.. and myBirthday traces as:

Mon Jul 19 05:23:59 GMT-0700 1954

 

You can also set properties of the Date object

myBirthDay.setYear(2003)

Fri Jul 18 05:23:59 GMT-0700 2003

String Object

The way to deconstruct and manipulate text strings. AKA parsing.

 

myString.charAt(n)

Returns the character at the "n" location in the string, counting from 0

 

myString.charCodeAt(n)

Returns a number that represents the unicode character code at the "n" location in the string

Unicode includes more characters than can be displayed on some computers

link http://www.unicode.org/ standard/WhatIsUnicode.html)

Currently 236,029 characters

 

myString.concat(thisString, thatString, ... anyNumberOfStrings)

Combines the text of two strings and returns a new string. Similar to +

 

myString. romCharCode(c1,c2,...cN)

Returns a string made up of the characters specified in the parameters. Can be a useful way to dynamically create strings that use unusual characters.

 

myString.indexOf(test,[startPoint])

Searches the string and returns the index of the substring specified in the parameters. This is the way to search for a word in a string, but is especially useful to find if a word is not in a string, which returns -1.

myString="This sentence is about Harry"

if (myString.indexOf("Fred") == -1) {

trace("Fred isn't in the sentence");

}

 

myString.lastIndexOf(test,[startPoint])

Returns the index of the last substring within the string that appears before the start position specified in the parameter, or -1 if not found.

 

myString.slice(startPosition, [endPosition])

Extracts a section of a string and returns a new string.

myString = "lexington"

Traces as: "ex"

 

myString.split("delimiter", [limit])

Splits a string into an array of strings by separating the string into substrings. Most useful when "delimiter" is a space. Optional limit is the number of items placed into the array (not the element numbers).

myString="This sentence is about Harry"

myString.split(" ")

Traces as: "This,sentence,is,about,Harry"

myString.split("", 6) //empty string and limit

Traces as: "T,h,i,s, ,s"

 

myString.substr(start, length)

Returns a specified number of characters in a string beginning at the location specified in the parameter. Useful to break out words from a longer string.

myString="This sentence is about Harry"

myString.substr(5,8)

Traces as: "sentence"

 

myString.substring(start, finish)

Like .substr(), but goes from start to finish locations, not start to length. To get the same result as above with myString: myString.substring(5,13)

 

String.toLowerCase()

Converts the string to lowercase and returns the result; does not change the contents of the original object. Useful for testing the content of a string without worrying about capitalization. Same myString as above.

myString.indexOf("harry")

Traces as: "-1"

lcString = myString.toLowerCase

lcString.indexOf("harry")

Traces as: "23"

 

String.toUpperCase()

Obvious, I hope.

 

String.length

Returns the length of the string.

Key Object

Key.methodName()

Two general approaches:

1. Check if key is currently depressed. Useful in loops

rotate_mc.onEnterFrame = function() {

if (Key.isDown(Key.RIGHT)) {

rotate_mc._rotation+=10;

output=rotate_mc._rotation

}else if (Key.isDown(Key.LEFT)){

rotate_mc._rotation-=10;

output=rotate_mc._rotation

}

};

Other useful stuff:

Key.property - complete list in actionScript popup.

2. Check the last key pressed. Useful for key-based UI's

hearMe = new Object();

hearMe.onKeyDown = function() {

outputAscii = "Ascii="+Key.getAscii();

outputCode = "KeyCode="+Key.getCode();

if (Key.getCode() == 65) {

A_mc.gotoAndStop(2)

}

if (Key.getCode() == 66) {

B_mc.gotoAndStop(2)

}

};

hearMe.onKeyUp = function() {

outputAscii = "Ascii="+Key.getAscii();

outputCode = "KeyCode="+Key.getCode();

if (Key.getCode() == 65) {

A_mc.gotoAndStop(1)

}

if (Key.getCode() == 66) {

B_mc.gotoAndStop(1)

}

};

Key.addListener(hearMe);

 

New Concept:
Listeners

Fundamental new thing in MX:

addListener()

 

You can also addListener's to:

Mouse

Listens for mouse events

 

Selection

Listens for text field selection focus

 

Stage

Listens for resizing of stage

 

TextField

Listens for events in text fields

Button Object

We already know some of its Events:

.onPress

.onRelease

 

But there's also:

.onReleaseOutside

.onRollOut

.onRollOver

.onDragOut

.onDragOver

.enabled

MovieClip Object

Another day...

Exercises

Source

 

Exercise One

Date Object

Based on the "birthday.fla" source file, build your own version of the movie below:

Exercise Two

Date Object

Based on the previous movie, modify it to show the full string of the day, for example "Monday" instead of "Mon"

Hints:

Build an array of complete day names

Use the getDay() method of the date object to get the number of the day from 0 - 6

Use that number to get the right day from the days array

Exercise Three

String Object

Based on the "parseInitials.fla" source file, build your own version of the movie below:

Exercise Four

String Object

Based on the previous movie, force the initials to always be capitals, and add a field that displays the middle name.

Hints:

Put the initials into a string first and use the toUpperCase() method.

Reuse indexOne and indexTwo and the slice()method to get the middle name

Exercise Five

Advanced

Force the middle name field in the previous movie to have its first initial capitalized. There are many ways to solve this, none of them straightforward.

Exercise Six

Key Object

Use the movie from exercise 1 or 3 and replace the ENTER button with a Key object that detects the ENTER from the keyboard.