Tuesday, July 24, 2007

Thank you Microsoft! :)
http://www.adobe.com/devnet/activecontent/articles/devletter.html

Saturday, July 21, 2007

Frame 0

Where does your playhead lie when you finish loading a movieclip.
Answer: Frame 0. Try loading in a swf via MovieClipLoader. In the onLoadComplete, make the mc stop playing: mc.stop();

Now, if you try to target something that you have put on stage in frame 1, you will get a null reference. Because at this point, the playhead is not in frame 1 yet. So if you want to stop your loaded swf from playing,

perhaps try
1) gotoAndStop(1) (I have found this not to work sometimes),

2) do a two-liner:
mc.nextFrame();
mc.stop();

3) put a stop frame in the original fla.

Make sure you charge your client extra for knowing stuff like this (jk!)

Have you gotten bitten in the ass by reference assigment lately?

References are cool. References are lightweight. References are bulletproof. Hmmm, not on the last one.

Reference assignment can really bite you in the S if you are not careful of its use. And more often times than not, because they are so darn convenient, you just wouldn't pay a second thought to using it...that's where the the ass-bitten timer starts counting down on you.

Take a look at this scenario:

Let's say you are building a puzzle game. You have an app that's pulling in dynamic data from your server. And you store it in your Game class property called puzzles.
Fine, now you can access the data via an instance of your game class, say game.puzzles.

Now, somewhere in your app, in order to make things easy to understand, you set a member variable to point to game.puzzles, like so:

class PuzzleManager
{
function PuzzleManager(game:Game)
{
_puzzles = game.puzzles;
}


function getCompletedPuzzles()
{
var completedPuzzles = new Array();
for (var p in _puzzles)
{
...check for score in each puzzle, pick out the ones with fullscore, etc.
}

return completedPuzzles;
}
}


Everything was working cool, until one day, your puzzle data requirement has changed. No longer do you just query your puzzle data at the start of the game. Now the puzzle data gets updated every time you finish solving a puzzle. That means game.puzzles points to a new set of contents every time you solve a puzzle. All good? No, your _puzzles member variable in PuzzleManager is still pointing to the old content, because reference in Actionscript acts more like pointer instead of reference in C++. That is, you are free to change your reference to point to anything during its lifetime, and just because you are pointing at what B is pointing at now doesn't mean it will always remain that way. At some point, B can be pointing at something else and you won't have a clue about it and remain pointing at the what B *USED TO* point to.

The moral of the story is - Use local variable where possible. Use reference assigment with extreme care.

Friday, July 20, 2007

What's wrong with the following code and when will it go wrong?
obj.addEventListener("event", Delegate.create(this, func));

Answer:

If you happen to call the code multiple times (say you enter the function more than once), then you are basically adding multiple delegate objects to the queue. From the surface, it looks like only one function has been set to listen to the event. But what actually happens is that you are adding multiple function object (aka delegate) to the event listening queue, and that will cause your event handler (func) to be called multiple times.

So the right way to do this would be to either make sure your addEventListener only gets called once, but if it has to be in a place where it might be called multiple times, set up a delegate table to save the delegate objects and use it in your Delegate.create call later.

e.g.

function init()
{
_handlerTable = new Array();
_handlerTable["onReleaseHandler"] = Delegate.create(this, onReleaseHandler);
_handlerTable["handleSomethingHandler"] = Delegate.create(this, handleSomethingHandler);
...
}

// function might be called multiple times
function initButtonStates()
{
obj.addEventListener("event", _handlerTable["onReleaseHandler"]);
...
}

Thursday, July 19, 2007

Plug and Play design

We have heard about plug and play a lot with hardware interfacing with OS, and it has taken us years to get to a point where PnP is working well enough to justify its name. What about software design? When will it reach a point of true PnP? These days a lot of people are buying into buzz words such as design patterns, MVC, etc. The more important question when you design your app is, how well can your app (or parts of it) be plugged into another app with minimal hassles? Will your app continue to function if it is loaded into another app? What happen if the path of the other app is different? Will your data files continue to load properly? If not, how do you make it so that it's easy to accommodate such changes.

Wednesday, July 18, 2007

My conversation with coworker...about wife and computer

Me [1:29 PM]:
I believe you know computer better than your wife, oh yes i do
Co [1:34 PM]:
that's true, my wife doesn't know about computers
Me [1:34 PM]:
i mean you know computer better than you know your wife
Me [1:34 PM]:
pls say it ain't true
Co [1:36 PM]:
I've been with computers longer, so to speak
Co [1:36 PM]:
computers make more sense, more logical
Co [1:36 PM]:
and you can switch them off

How often do you find yourself having to hunt down fonts for PC when you got a FLA file prepared for you on the Mac that uses Mac fonts?

The whole font hunting affair is another product idea waiting to be implemented.

Speaking of which, I installed a TradeGothic LT Bold Condensed No.20 today, but in Flash font mapping dialog's Substitute font listbox, I am not seeing it!

After some fiddling, my coworker popped up the font, and below the big ass title name, you can see a Typeface name: TradeGothic LT CondEighteen! Sure enough, that font name shows up in the Substitute font listbox.

Have you thought about creating something that would be useful to the Flash folks out there?

One such thing would be a layout manager that can do what FlexBuilder can do. This is useful for app that changes in size in reaction to the browser window resize. Everything within the app will stay in relation to each other depending on what anchor points they have, whether their aspect ratio should be maintained, etc. Currently people are still rolling their own custom solutions for this. I am sure someone will be happy to pay for something like this.

Another thing that would be useful (especially for building sites for global corporations) is a framework or toolkit that allows people to easily switch from one language to another from within any "page" in the Flash application and the application state remains the same, only the content has now changed to the French content. By content I don't mean just text changes, this can mean any changes in thestage contents.

So there, start your company around these two things to boot. :)

Sunday, July 15, 2007

There are at least two issues with the way EventDispatcher is being implemented right now (AS 2.0) -- one is that there is no way to assign priority to your event listener. So if you want to have a listener execute before another, there is no way to do that. Also, if you examine EventDispatcher closely, you will notice that event gets dispatched by looping through the queue list. Using for ( ) construct in Actionscript actually retrieves things in reverse order, so the last thing that gets added to your event queue gets called first.

Here is another interesting scenario with event dispatch. Imagine you have two objects listening to a particular event. Say if the first listener calls gotoAndPlay(), which causes certain movieclips to go out of existence, the second listener that refers to those movieclips will get undefined reference instead. This problem won't exist if the listeners are being called in a different order.

Sunday, July 01, 2007

I have frequently found a need to know who calls a particular method. Unfortunately, this is not something that can be easily achieved with Flash right now. It would be nice to have a facility such as trace("caller: " + this.caller) to print out the name of the obj.func that calls the method the trace statement is in.