« June 2005 | Main | October 2005 »
July 18, 2005
[Flash Platform] Delegates are not just for Events!
Some of you may already be familiar with the usefulness of mx.util.Delegate for events. However, you may not have realized their full potential!
Delegates are great, they resolve the scoping issue related to how functions are scoped to the invoking object. However, most examples I’ve seen using Delegates use them in events:
someComponent.addEventListener(this,Delegate.create(this, this.someMethod));
However, they can be most useful for solving other technical design issues as well:
Callbacks:
When you don’t need an event to broadcast itself to N number of listeners you can use a simple callback which will only respond to 1 object. Most callback implementations I’ve seen (and have written myself) look something like this:
class SomeClass
{
private var m_callbackObject:Object;
private var m_callbackMethod:String;
function setCallback(obj:Object, method:String):Void
{
this.m_callbackObject = obj;
this.m_callbackMethod = method;
}
function doCallback():Void
{
this.m_callbackObject[this.m_callbackMethod]();
}
}
// code on timeline
function sayHi(){
trace(“Hi!”);
}
var myClass = new SomeClass();
myClass.setCallback(this,”sayHi”);
myClass.doCallback();
// output
Hi!
While this works it is not ideal. Using a Delegate here could greatly reduce the amount of code (by 50%) which is especially useful when dealing with multiple callbacks:
import mx.utils.Delegate;
class SomeClass
{
private var m_callback:Function;
function setCallback(obj:Object, method:Function):Void
{
this.m_callback = Delegate.create(obj, method);
}
function doCallback():Void
{
this.m_callback();
}
}
NOTE: Delegate takes a reference to a function for the second parameter
Now you might find yourself going back to the first example and asking why you couldn’t use a single reference and eliminate the Delegate:
this.m_callback = obj[method];
The above code will invoke the function but that function will be scoped to SomeClass! The Flash scoping rule comes into play here because SomeClass executed the callback, that function gets scoped to the instance of SomeClass which is what Delegate resolves.
Event Handlers
Another common problem Delegate resolves is when dealing with Event Handlers such as MovieClip events, Buttons events, etc:
function sayHi(){
trace(“Hi!”);
}
function someFunction(){
this.sayHi();
}
myMovieClip.onPress = this.someFunction;
// User
myMovieClip Pressed
// output
Nothing, the “this” keyword is scoped to myMovieClip.
The above implementation shows bad design because someFunction is getting scoped to myMovieClip. So when someFunction tries to invoke sayHi it is looking for the sayHi method on myMovieClip which does not exisit. Also note that the compiler doesn’t apply these types of scoping rules so no error is ever thrown.
Another implantation I have seen (and used myself) is:
function sayHi(){
trace(“Hi!”);
}
myMovieClip.onPress = function ()
{
this._parent.sayHi();
}
This example IMO is better then the first but I am manually resolving the scope which assumes that the required function will always be on _parent. The compiler doesn't check the scope here either so it essentially ignores any reference to _parent. Using a Delegate however is a much more flexible (and compile safe) option which I find to be more readable and consistent to how other scope related issues are dealt with (consistency == good):
myMovieClip.onPress = Delegate.create(this,this.someFunction);
Also, in any of the above examples you could do some neat optimization tricks as well:
var myDelegate = Delegate.create(this,this.someFunction);
myMovieClip.onPress = this.myDelegate;
myMovieClip2.onPress = this.myDelegate;
myMovieClip3.onPress = this.myDelegate;
Some food for thought =)
-erik
Posted by erikbianchi at 08:04 AM | Comments (45)
July 12, 2005
[Flash Quirks] MediaDisplay Component
A designer bud of mine was having some issues with Macromedia’s MediaDisplay component. I tend to sway away from mm’s component set due to issues like this but after some mucking around I was able to resolve the issue.
The Scenario
In my bud’s Flash site he had a parent movieclip load another swf which contained the MediaDisplay component. The first couple of frames contained some transitional animation with the MediaDisplay component located on the final keyframe.
When clicking on the close button the close routine would unload the swf. This worked fine except for the fact that the audio would continue to play.
To try to fix this issue I added in a mediaPlayer.stop() command. This stopped the audio issue but when reloading the swf that contained the MediaDisplay component the video would not replay. I then tried adding mediaDisplay.play(0) and setting all the MediaDisplay properties dynamically rather then using the panel and this did not work either.
To finally resolve the issue I had to add the following to the close routine:
mediaDisplay.swapDepths(this.getNextHighestDepth());
mediaDisplay.removeMovieClip();
I tried searching the web but couldn’t find anything on a workaround (although I do remember reading something somewhere) so hopefully this will help save you sometime should you come across a similar issue.
-erik
Posted by erikbianchi at 04:33 AM | Comments (3)
July 10, 2005
[Flash] Coauthor of an upcoming Flash book
I recently signed a deal to be a Coauthor of an upcoming Flash book. I’ve contributed to books before in the past and have been a tech editor on several but this will be my first full blown authorship!
More information to follow in the coming weeks as my coauthor and I get closer to completion.
-erik
Posted by erikbianchi at 02:04 AM | Comments (3)
July 09, 2005
[OT] The Best Video Ever
(Work Safe)
Same Movie, Different sizes:
http://www.bluetights.net/qt_small.html
http://www.bluetights.net/qt_medium.html
http://www.bluetights.net/qt_large.html
-erik
Posted by erikbianchi at 04:39 AM | Comments (137)
July 07, 2005
[OT] London Terrorist Attack
My sincerest condolences to our brothers across the pond. After hearing about the attacks I immediately thought about some of our (as a community) and my Flash friend’s such as Peter Hall, Guy Watson, Aral Balkan and Mike Downey (who is there on a business trip).
My thoughts and prayers go out to those affected by this tragedy and hope that our shared wounds can only help but bring the UK and US closer together.
-erik.
Posted by erikbianchi at 05:09 PM | Comments (182)
July 06, 2005
[Flash Platform] ARP Advisory Committee Member
Just a quick note to announce that I’ve been appointed a seat on the ARP Advisory Committee!
For those who don’t know, ARP is an Open Source Pattern-Based Rich Internet Application (RIA) Framework for the Flash Platform and currently supports both Flash and Flex RIA Development in ActionScript 2.
To learn more about ARP check out: http://osflash.org/arp
For a list of current ARP committee Members check out: http://www.openarp.org/index.php?page=ARP+Advisory+Committee
-erik
Posted by erikbianchi at 06:04 AM | Comments (3)