assertTrue is the professional blog of Luke Bayes and Ali Mills

Custom Events in ActionScript 3.0

Posted by: Luke Bayes Sun, 15 Oct 2006 06:18:00 GMT

I just lost at least 2 hours to this one and figured I’d share with the hope that maybe I’ll save someone else the same headache…

If you’re working with the Flex 2 framework and writing custom components as directed, you’re probably creating custom event data types.

If your event handlers are written to expect an instance of your custom data type, be sure you override the “clone” method and return a new, configured instance of your new event data type.

I’ve been staring at this error message for hours now:

TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::Event@148cb29 to lifebin.events.ActionEvent.

It was driving me nuts because the stack trace begins with a method that actually dispatches a correctly configured instance of an ActionEvent!

I even went so far as to create isolated test cases that create, dispatch and bubble custom events through a Flex and non-flex composite structure. I was unable to reproduce the error outside of our application and still don’t know why or whom is calling clone – but I just tried implementing the clone method out of desparation and voila! Everything works.

I’d love to know why this is happening – but for now – I’m content with spreading the word as to how to avoid it!

Good Luck.

Tags  | 3 comments

AS3 Bug When Nesting 'if' Within 'switch'

Posted by: Ali Mills Tue, 29 Aug 2006 05:33:00 GMT

Luke and I ran into an ActionScript 3.0 bug today when nesting an if statement within a switch. It looks like the compiler has problems referencing object properties when this happens. The following code demonstrates what we found:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package {
    import flash.display.Sprite;

    public class SwitchBug extends Sprite {
        public function SwitchBug() {
            var num:Number = 1;
            var firstNumber:Number = num;
            var secondNumber:Number = num;
            tryNumSwitch("go", firstNumber, secondNumber);

            var now:Date = new Date();
            var firstDate:Date = now;
            var secondDate:Date = now;
            tryDateSwitch("go", firstDate, secondDate);            

            // this method causes a crash
            tryCrashingDateSwitch("go", firstDate, secondDate);
        }

        private function tryNumSwitch(val:String, firstNumber:Number, secondNumber:Number):void {
            switch(val) {
                case "go":
                    if(firstNumber == secondNumber) {
                        trace(">> tryNumSwitch successful !!");
                    }
            }
        }

        private function tryDateSwitch(val:String, firstDate:Date, secondDate:Date):void {
            switch(val) {
                case "go":
                    if(firstDate == secondDate) {
                        trace(">> tryDateSwitch successful !!");
                    }
            }
        }

        private function tryCrashingDateSwitch(val:String, firstDate:Date, secondDate:Date):void {
            switch(val) {
                case "go":
                    // the compiler crashes on DATE.milliseconds call
                    if(firstDate.milliseconds == secondDate.milliseconds) {
                        trace(">> tryCrashingDateSwitch successful !!");
                    }
                    // uncommenting the trace below fixes the situation
//                    trace("Fixed");
            }
        }
    }
}

Oddly, adding a trace (or any line of code) after the if statement fixes the issue.

Tags ,  | 2 comments

Invest Regulary in Your Knowledge Portfolio

Posted by: Ali Mills Wed, 26 Jul 2006 06:30:00 GMT

I must have learned something from the first chapter of The Pragmatic Programmer by Dave Thomas (who’s probably better known these days for his contributions to the Ruby community through his publishing company and must-have book Programming Ruby) when I read it long ago. In that chapter, Dave’s 8th tip is to, “Invest Regulary in Your Knowledge Portfolio”. One of the ways he suggests doing so is to learn one new language a year. Well, this year I seem to be making up for years gone by, because I’ve been spending a lot of my extra time working with several different languages and their frameworks. This year, I’ve been working with – in order as of today – haXe, Flash Lite 1.1, Ruby, ActionScript 3.0, XUL, and the Nullsoft Scriptable Install System. I’ve been working with their frameworks Ruby on Rails, Flex, and XULRunner.

All of these languages are tools to build the kind of software that I’m interested in building: software that’s fun to write, easy to distribute, a pleasure to use, useful, and affordable. For software to be distributed easily and a pleasure to use has to be universally available. And, to be universally available it needs to be truely cross-platform – cross-software and cross-hardware. It needs to work on win, mac, and nix and also in a browser, on a desktop, and on a palmtop. Today, the best format to make software universally available is the SWF format. It’s undeniable, more people have the Flash Player than any other piece of software .

It is, granted, worth noting that some feel like traditional Java, non-traditional Java, Python, Ruby, .NET, and the Mozilla Foundation’s XUL are also acceptable tools to make available software, and depending on the goal, they’re correct. In many cases, these technologies can even enhance a SWF’s funcionality. For example, it’s possible to use Mozilla’s XULRunner as a desktop wrapper for SWFs, or if you’re really smart, to do what Rich Kilmer did and write a SWF view to a Ruby controller and Berkeley DB model. Rich gave me and Luke a sneak peak at his indi product at RailsConf, and we were blown away. The product is a great idea, and it’s built on an extremely smart and interesting architecture. Get indi when it’s available.

Invest regularly in your knowledge portfolio.

Tags , , , , , , , , ,  | no comments

San Francisco Design Patterns Study Group meeting tonight

Posted by: Ali Mills Wed, 14 Jun 2006 17:09:00 GMT

The San Francisco Design Patterns Study Group is meeting at 6:00PM tonight at Frog Design. Frog is located at 420 Bryant St. in San Francisco. Tonight’s pattern is the Strategy Pattern.

Luke and I started the group a couple years ago to motivate us to work through Addison-Wesley’s original Design Patterns book, but we’re not leading the group right now. Ben Honda Rottler is. Essentially, Ben has activated the group from its dormant state. I believe that Ben – like us – is working through the book in the order and fashion suggested by Joshua Kerievsky’s A Learning Guide To Design Patterns which is good because the order worked really well for us.

If you’re interested in design patterns and live in San Francisco, you should consider attending the group.

If you’re interested but live in Boston, you should consider attending the Boston Flash Platform User Group. Since April they’ve been hosting a design patterns sub-group.

Besides the groups in San Franciso and Boston, I don’t know of any other design patterns study groups that focus on ActionScript. I know that when Paul Spitzer (Flair and SWFObject Publish Profile Extension author) lived in Santa Cruz, he started a group. But, since Paul lives in San Francisco now (and was a core attendee to the San Francisco Design Patterns Study group when Luke and I were working through the book for the first two times), that group’s now gone.

If you know of other design pattern study groups, please post the group’s information in the comments section.

Thanks!

Tags ,  | 1 comment

AutoCompleted :: AutoComplete [Embed]/Legacy SWF/AS3.0 Project example updated for Beta 3

Posted by: Ali Mills Wed, 24 May 2006 06:24:00 GMT

I’ve updated the AutoComplete ActionScript Project that includes compile time and run time library type assets for Flex 2.0 Beta 3. Here it is:

You can download the complete AutoComplete ActionScript Project files from here.

This project was built by example over the three previous posts ActionScript Projects in Flex Builder 2.0, Library Type Assets in ActionScript 3.0 :: using the [Embed] metatdata tag, and Library Type Assets in ActionScript 3.0 :: using assets created with current releases of Flash Authoring. In some of those posts, I commented on a few quirky issues I’d found with Flex. Well, I’m glad to say that with the advance to Flex 2.0 Beta 3 many of those them have been resolved. Woo Hoo! The menu option to create new ActionScript projects is now grouped with the other project options. The trace() method is no longer packaged in the flash.util package and can be called without an import. The resulting SWF file size is still shrinking; for AutoComplete it dropped from 18k in beta 2 to 6k in beta 3 – wow. And, most exciting is that the framework source code is shipping. The one question that I still have is, will the framework be open-sourced? That would be exciting! The updated New in the beta 3 release notes say, “Flex Builder 2 now includes a copy of the source code for the core Flex framework, enabling you to debug into the Flex source code or looking up the implementation from a component instance.” It doesn’t sound like they’re open-sourced yet. Hopefully they will be when the final release ships.

Updating AutoComplete for Flex 2.0 Beta 3 didn’t take much work. Using Beta 2 to Beta 3 Changes as a reference, I renamed getClassByName() to getDefinitionByName() and changed the import for it and describeType() to flash.utils. I also changed the import for TextField and TextFieldType to flash.text. That was pretty much it.

Read more...

Tags  | 6 comments

Library Type Assets in ActionScript 3.0 :: using assets created with current releases of Flash Authoring

Posted by: Ali Mills Wed, 17 May 2006 03:12:00 GMT

In the previous post Library Type Assets in ActionScript 3.0 :: using the [Embed] metatdata tag, I mentioned that an ActionScript 3.0 application can load library type assets (i.e. SWFs) created in current releases of Flash Authoring during run time. This is a big deal – a huge deal – because without this ability there wouldn’t be any way to create a MovieClip and include it in a Flex Builder 2.0 project until Blaze becomes available.

The simplest way to load a legacy SWF is with the Loader class. After it’s loaded, you can manipulate its DisplayObject properties with the Flash 9 Player’s AVM1Movie adapter class.

While this method works well for canned widgets like video and music players that don’t need to communicate with their host application, it doesn’t work well when host communication is a requirement. When this is a requirement, it’s necessary to take advantage of features available through the LocalConnection class, because the only way for ActionScript 3.0 running in the AVM2 to communicate with legacy ActionScript running in the AVM1 is over a LocalConnection .

While researching an elegant solution to the problem of how two pieces of code (AS2 and AS3) running in two separate virtual machines (AVM1 and AVM2) can communicate, I looked to the book Enterprise Integration Patterns : Designing, Building, and Deploying Messaging Solutions from the signature series of one of my favorite authors Martin Fowler. Chapter 2 of the book explores different integration styles and suggests that the Remote Procedure Invocation approach is a, “mechanism for one application to invoke a function in another application, passing the data that needs to be shared and invoking the function that tells the receiver application how to process the data.” After reading this and the rest of the section (the full Remote Procedure Invocation section can be found online at http://www.awprofessional.com/articles/article.asp?p=169483&seqNum=4), I was convinced that the Remote Procedure Invocation approach would work and designed an AS3 stub and AS2 skeleton class. They follow:

Read more...

Tags  | no comments

Library Type Assets in ActionScript 3.0 :: using the [Embed] metatdata tag

Posted by: Ali Mills Wed, 19 Apr 2006 05:49:00 GMT

In a comment to the previous post ActionScript Projects in Flex Builder 2.0, Ben asked how to get library type assets into Flex Builder 2.0 projects. There are two ways. The first is by using the [Embed] (not the Embed from Object/Embed) metadata tag to include assets at compile time. The second is using the Loader class to load them at run time. What’s interesting is that assets loaded at run time can be created in current releases of Flash Authoring and contain legacy ActionScript. What’s challenging is that loaded SWFs containing legacy ActionScript are extremely limited in how they can communicate with their loading AS3 container. They can only communicate with one another over LocalConnection. LocalConnection is neccessary for communication because AS3 runs in a new and different ActionScript Virtual Machine (AVM) than the legacy AS. The ActionScript 3.0 Overview a talks a little more about how the 8.5 player runs legacy ActionScript with the AVM1 and ActionScript 3.0 with the AVM2.

In this post, I’m only going to discuss the first of these two ways: compile time inclusion with the [Embed] meta tag.

The [Embed] meta tag provides a way to include external resources like images, sounds, and fonts into a finished project by compiling them into the SWF. It can be used to import JPEG, GIF, PNG, SVG, SWF, TTF, and MP3 files. It can also be used to add fonts. The Developing Flex Applications chapter Embedding Resources does a good job of explaining the details of [Embed]. If you’re interested in this topic, you should read it. To demonstrate how [Embed] works, we’ll add a “Remember” button to the AutoComplete project we created in the ActionScript Projects in Flex Builder 2.0 post.

The first step to adding a “Remember” button is to create three PNGs for the button states up, over, and down with the graphics program of your choice. Create them (or download them from here) and place them in a folder called “img” inside you AutoComplete project folder at the same level as AutoComplete.as. Name the PNGs “Remember_up.png”, “Remember_over.png”, and “Remember_down.png”.

The second step is to write the code to embed and use the button art. This code will be added to AutoCompleteRunner.as. At the end of ActionScript Projects in Flex Builder 2.0 AutoCompleteRunner.as looked like:

Read more...

Tags  | 2 comments

ActionScript Projects in Flex Builder 2.0

Posted by: Ali Mills Mon, 10 Apr 2006 05:25:00 GMT

I learned a lot about writing ActionScript 3.0 and using Flex Builder 2.0 while working on code examples for the ActionScript 3.0 Language Reference. Finally, I feel like we’re at a place where I can talk openly about most of what I learned. I mean, search the web, Mike, Sho, Darron, and Jesse are all actively blogging about things AS3/Flex related. I like AS3. I’m excited to see the language succeed. Hopefully, sharing what I know will help. This first AS3 post is about creating and working with ActionScript projects in Flex Builder 2.0.

A few days ago Sho posted source for the Flex auto complete text input control v0.6 on his blog. Today, in a comment on that blog, bokonn asked if the component could be reused in ActionScript, and Sho replied that it wouldn’t work in an ActionScript only project.

Well, since we’re going to need a context to create and work with our Flex Builder 2.0 ActionScript project, I’ve written an AS3 AutoComplete class that works in ActionScript only projects. We’ll use this class as our context. Before proceeding, I feel the need to point out that Sho’s component and my class are two very different creations with one of the main differences being that his is very feature rich and mine is not.

OK, here we go. Say you’re one of those developers who’s not interested in using either the Flex Framework or MXML, is Flex Builder 2.0 still for you? It sure is. For you, dear developer, Flex Builder 2.0 offers the ActionScript project. To create one, follow these steps:

Read more...

Tags  | 5 comments

Older Posts

Older posts: 1 2 3