Archive for December, 2007

notes from O’reilly’s Essential Actionscript 3.0 (eas3)

Monday, December 24th, 2007 | actionscript | No Comments

I read a lot of programming books and jot down some notes when I find something interesting that I didn’t know or reinforces something I’ve only previously found out through trial and error. Here are some I’d like to share from Colin Moock’s book Essential Actionscript 3.0

Namespaces
Namespaces in flash are the same ones used in xml. Infact, the built in ‘access-control’ modifiers in AS3 like private
and public are namespaces that can also be used with the Namespace class. Using the import directive technically opens the public namespace of the imported package to the current scope and all nested scopes.

The mx_internal namespace used in internal flash and flex framework classes is a custom namespace they’ve defined to share methods and properties between their framework classes, without making those things immediately visible to outside classes. If marked public, code outside the framework could access it, if marked internal it couldn’t be shared across packages. Thus mx_internal.

Other useful tidbits:

  • User defined namespaces can be used as attributes within the top level of a class definition only
  • you can use variable references to a namespace as in: var p:Namespace = mypredefinednamespace;
  • you can refer to a namespaced variable via namespacename::variableormethod: trace(p::variableormethod);
  • you can’t use variable refrences to namespaces in ‘open namespace’ however

Shared Events
An Alternative to allowDomain() = Shared Events
Allows .swf files from different domains to share events without allowing full cross-scripting privileges.
When a swf loads a swf from a different domain, the first can forward its events to the latter by dispatching
its events to the latter’s loaderInfo.sharedEvents object. (ex. loader.contentLoaderInfo.sharedEvents.dispatchEvent(event));

Garbage Collection
Even if an event listener holds the only reference to a clip, the clip won’t be destroyed and the listener will keep listening.
One can’t depend on useWeakReference in events. Though the item will get garbage collected, until it eventually happens, the event will still be dispatched, especially pertinent to something like enterframe events

Events
You can prevent default behavior in many built-in events like TextEvent.Text_INPUT.
By default the text event automatically adds the input to the text. But you could do evt.preventDefault()
and add X’s instead. Check the event’s cancelable property.
Just like built-in events, custom events can define default behavior that can be canceled via preventDefault()
in the event listener check isDefaultPrevented() to see whether an event has had its default behavior prevented
ex.
var toggleEvent:ToggleEvent = newToggleEvent(ToggleEvent.TOGGLE_ATTEMPT, true, true);
dispatchEvent(toggleEvent);
if (!toggleEvent.isDefaultPrevented()){
toggle();
}

Marker Interfaces
An empty interface to “mark” a class as having some feature.
Ex. flash API includes a marker interface IBitmapDrawable, which desginates a class as eligible for drawing into a Bitmap Data object.
The BitmapData class will draw only those classes that implement IBitmapDrawable.

null and undefined
Both null and undefined conceptually represent the absence of data. The null value represents the absence of data for variables, parameters, and return values with a specified type annotation set to anything but Boolean, int, uint, and Number.

undefined represents the absence of data for variables, parameters, and return values without a specified type annotation.
Ex. dynamic variables on a property. Also represents the complete absence of a variable or method on an object whose class is defined as dynamic.

Casting
Difference between casting: AsClass(Class) and (Class as AsClass)
Both make the compiler and runtime see one class as another class (unless casting to primitive wherein it gets converted)
but as returns null in all cases where a cast operation would generate a runtime error.

For legacy reasons, the cast syntax ToClass(Class) cannot be used to case a value to the built-in Date or Array classes. The result of the expression Date(someValue) is identical to new Date().toString() (which returns a string representing the current time). The result of the expression Array(someValue) is identical to new Array(someValue) (which creates a new Array object with someValue as its first element).

Static Methods
though static methods and static variables are not inherited, parent static methods and static variables can be referred to directly without the class name.
Static methods will maintain the scope of the original class they are defined in

Package Level and Global Functions
To create a function that is available throughout a package or an entire program, place a function definition directly within a package body. An ActionScript source file’s name must match the name of its sole externally visible definition even if its a function.

Functions defined at the package-level within the ‘unnamed package’ [package thats generated for all path-less packages] are known as global functions because they can be referenced globally, throughout a program, without the need for the import statement. like trace().

You can also put a access-control modifier less function outside the package, it’ll be accessible to that file only.

Optimization
Code in the class initializer (includes constructor code) runs in interpreted mode, and is not compiled by the JIT compiler. Because JIT-compiled code executes much faster than interpreted code, you should moving processor-intensive code out pf the class initializer when performance is a priority.

Hello World!

Monday, December 24th, 2007 | Uncategorized | No Comments

So I’ve been planning to put up a flash related blog for some time. Hopefully it’ll be as useful as I’ve found some other developer blogs. Here we go!

Meta

Search