I’ve been a fan of the O’Reilly Cookbooks in the past and have found valuable info in both their ActionScript 3.0 Cookbook and Flex 3 Cookbook. So, when O’Reilly released their Flex 4 Cookbook earlier this year I purchased a PDF copy as soon as it became available. However, I’ve been disappointed by this volume as I’ve blogged previously (Flex 4 Cookbook Sample Code Difficult to Use ). Unfortunately, as I continue to use the book I continue to be frustrated.
My most recent frustration was occasioned by an attempt to use the code in section 11.8, “Create Custom Animation Effects.” I started by creating a new Flex 4 project and then copying the book’s two downloaded sample files, CustomTweenEffect.as and CustomTweenInstance.as, into the new project. I then added some code to my new MXML application to use CustomTweenEffect but I was confronted with four compile errors in the Cookbook code. The first error was caused by what appeared to be an extra constructor in the CustomTweenEffect class. This extra constructor was for a class called CustomDisplacementEffect. This was the only reference to CustomDisplacementEffect in the two downloaded source code files so I commented out this function.
I moved on to an error in the CustomTweenEffect class declaration—a missing import for the Flex Animate class. I added the import statement and, while I was at it, I removed the unneeded import for mx.effects.TweenEffect.
At this point, CustomTweenEffect compiled successfully so next I continued with the one remaining error which was in the CustomTweenInstance class. In this case, the problem appeared to be an incorrectly named constructor function: NewTweenInstance. The class in question is CustomTweenInstance so I changed the constructor function name to match. One step forward, one step back. Now, I had a newly reported error on line 11:
1180: Call to a possibly undefined method Tween. CustomTweenInstance.as /CustomEffects/src/oreilly/cookbook line 11 Flex Problem
Another missing import, easy to fix but annoying. Meanwhile, back in CustomTweenEffect another new error had popped up:
1046: Type was not found or was not a compile-time constant: IEffectInstance. CustomTweenEffect.as /CustomEffects/src/oreilly/cookbook line 16 Flex Problem
Yet another missing import, this time for IEffectInstance. After fixing this, the two cookbook classes finally compiled. Dare I hope that it would run? No. Here’s the run-time error (at CustomTweenEffect.as, line 21):
TypeError: Error #1034: Type Coercion failed: cannot convert spark.effects.supportClasses::AnimateInstance@1d45ae51 to oreilly.cookbook.CustomTweenInstance.
Here’s the code at line 21:
CustomTweenInstance(instance).finalAlpha = this.finalAlpha;
This looked to me like CustomTweenInstance was extending the wrong class (TweenEffectInstance). So, I tried changing it to extend AnimateInstance. That led to a compile error on the onTweenUpdate function which is not a member of AnimateInstance. So, I changed that method name to animationUpdate, which is the corresponding method in IAnimationTarget (which is implemented by AnimateInstance), but then the passed-in parameter was wrong. I fixed that. One more step forward, one more step back. Now, I had a compile error in the CustomTweenInstance play function.
Long story short, I ended up completely rewriting the Cookbook’s two sample classes so that they look almost nothing like the originals. To see an example of creating a custom tween/animate effect see my post “Creating a Custom Animation effect in Flex 4.”
Before I started all this debugging I checked the book’s download & errata pages on the O’Reilly website. However, the latest download available was dated June, 2010—the same version I already had. And there was nothing in the book’s errata page about these problems. It appears O’Reilly has taken the approach of having its customers become alpha testers. But even with a public alpha/beta test one can hope that eventually the software will be debugged. Unfortunately, the Flex 4 Cookbook, with a release date of May, 2010, seems to be stuck somewhere around V0.8 as of the end of 2010.
Update 7/20/11
A reader emailed me to tell of the problems he was having while trying to use the DataGrid examples in chapter 9. Indeed, these examples are a mess. Here are the changes I had to make to example 9_1_b to get them working:
- Create a project in Flash Builder and copy the sample files into it.
- Rename the MXML file to start with a non-numeric character.
- Remove the “oreilly.cookbook.flex4” package on the DataGrid’s itemrenderer property.
- Copy the file example.mxml from the parent directory of the samples into the Flash Builder project directory.
- Change the URL on the HTTPService to point to example.mxml instead of assets/homesforsale.xml.
- Change the mx: namespace declaration to xmlns:mx="library://ns.adobe.com/flex/mx"
It’s really a shame that these examples were so thoughtlessly packaged. If you can get over these hurdles the sample code is actually quite good. But someone trying to learn Flex might easily be overwhelmed and give up.


{ 2 comments… read them below or add one }
Have you been able to get recipes 8.11 and 8.12 working? They seem to be dependent on 8.10, but its difficult to see exactly how the subsequent code additions fit onto the base code. I spent way too much time jerking around with it, before throwing up my hands in disgust.
Awesome post, Dave! Is so frustrating that the authors and publisher seem to have done a rush job at getting this out to the public. Thankfully you’re helping newbies like me
{ 1 trackback }