04
instance

instances

why?

why instances? sometimes you need variants of an item, each with different characteristics. think of polka dots, cars, or t-shirts, or forks. you can have many of the same type of object, but each individual one may exhibit different instance-level properties (position, paint color, size, number of tongs).

how?

use the new keyword and call the object's constructor function.
new Thingy();
some object constructors expect parameters.
new Thingy(3, "fred");
calling a constructor with the new keyword will generate a custom instance just for you, so you'll want to store it in a variable (of the appropriate type) for later.
var he:Thingy = new Thingy(2, "fred");
var she:Thingy = new Thingy(3, "frieda");

some examples

all native object types can be instantiated with a constructor function or a literal declaration.

Boolean

var B:Boolean = new Boolean(true);
var B:Boolean = true;

var B:Boolean = new Boolean(6 > 4);
var B:Boolean = (6 > 4);

Number

var N:Number = new Number(5);
var N:Number = 5;

String

var S:String = new String("abcdefg");
var S:String = "abcdefg";

Array

var A:Array = new Array();
var A:Array = [];

var A:Array = new Array("a", "b", "c");
var A:Array = ["a", "b", "c"];

Object

var O:Object = new Object();
var O:Object = {};
non-native objects can only be instantiated via a constructor function—there are no means of literal declaration.

Date

var D:Date = new Date();
var S:Sprite = new Sprite();

a quick review

var week:Array = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
var D:Date = new Date();
var dayIndex:Number = D.day; 
var day:String = week[dayIndex];

// see http://livedocs.adobe.com/flex/3/langref/Date.html#day for day indices

trace("today is");
trace(day);

static objects

some objects are built only to collect a set of properties and methods and the set is always the same; there need be only one. these are sometimes called static objects. Math and Number are examples. you just access their properties and methods through their name without instantiating anything.

custom objects, a.k.a. classes

the designers of actionscript did not intend for the objects native to flash to be the only objects a flash programmer might ever need. programmers can create their own custom objects (called classes) to implement functionality and data structures that suit their specific needs.

classes are defined in actionscript files (plain text files) that exist outside of any flash file. as long as a flash file can find the class file, you are free to create instances of the new class in code in the flash movie. for highly specific classes that will only be used within a single project, putting the class file in the same folder as the flash movie is enough—flash will magically find it. for more general purpose classes that may be usable in many different projects, it's easier to manage them as a package, or library of classes than can be located in one location and imported by any flash movie that needs access to a custom class.

classpaths

when classes are defined somewhere other than the same folder that the flash movie is in, flash needs help to be able to locate them. by adding an entry to the classpath list, you can specify a location for flash to check for the definition of a class you may be using. see the Flash Help for more info.

 

homework submissions

you will submit homework by copying only the final files for your assignments into folders that you create inside your Z:\HTML folder. start with a pca folder, then put your assignment folders inside that, e.g. Z:\HTML\pca\task03. then name the main html file for the assignment with the same name as the folder and .html, e.g. Z:\HTML\pca\task03\task03.html. your homework assignment will be linked to from the student work page. you are responsible for checking that your homework link works before the beginning of class when it's due.

HTML export template

if you save this HTML template to C:\Documents and Settings\<you>\Local Settings\Application Data\Adobe\Flash CS3\en\Configuration\HTML, you'll have a new entry in the Template drop-down on the Publish Settings panel in flash (CTRL-SHFT-F12). Select PCA Homework Template and publish.

you'll also need to save this script in the same directory as your .swf