Showing posts with label Flex. Show all posts
Showing posts with label Flex. Show all posts

Sunday, April 13, 2008

Reflection in ActionScript 3

I'm not a big fan of the old eval() function in earlier versions of ActionScript. As of ActionScript 3, it is no longer supported. For the most part, that is a very good thing. My buddy, Jason, is migrating from AS2 to AS3 and he found himself looking for the eval() function. He came to me and asked me about it, I replied "you mean the evil() function?".

The problem with eval() is that you were executing code that was being created at runtime, meaning there's no chance of the complier helping you catch errors. He showed me the code he was working on and --usually-- you can solve the lack of eval() through a better architecture. As I looked at his program, I realized he had a pretty elegant solution. Re-architecting would take a good deal of time and not necessarily yield as elegant result. Java has quite a few classes for dealing with reflection, so I figured (given how similar AS3 is to Java) there must be some similar classes. As it turns out, the pickings are pretty minimal... but there were enough to come up with a nice, dynamic solution to his problem.

In the flash.utils package, there are a number of public functions. The one we were looking for (before we even knew it) was getDefinitionByName(). This function takes a string that is the name of a class, an returns the class object for it. With the class object, you can then create an instance of the class. Here's a simple example you could run in the Flash IDE:
var myClass:Object = getDefinitionByName("Symbol1");
addChild( new myClass() );
It assumes you have a Symbol named "Symbol1" that is exported for ActionScript. If you are doing this in Flex or inside an external class file, remember to import the flash.utils package. The danger of runtime errors still exists, but it is more limited now in that you can create classes and not just execute and arbitrary chunk code on the fly like you could with eval().

Audio Display List?

The Flash 9 player using ActionScript 3 (for Flex or Flash), has an internal structure called a display list. Items in the display list, will be shown, items not in it will not be shown. So if you load an external SWF, it will not be visible until you add it to the display list. Once it is in the display list, it'll receive events (like MouseEvents, KeyboardEvents, ENTER_FRAME, and so on). That's nice, because you can load it and display it at will.

But what about a SWF that also contains audio? As it turns out, there is no audio equivalent to the display list. Once a SWF containing audio is loaded, it will begin to play and you'll hear the audio, whether or not you're displaying it. To stop the audio, listen for Event.INIT on the loaderInfo in the Loader object. Event.INIT fires when the first frame is loaded so you can call stop() it to prevent the playback head from moving forward in the timeline.

Monday, March 10, 2008

FlexBuilder vs FlashDevelop

I've been using both FlexBuilder and FlashDevelop at work lately. I'm a long time FlashDevelop user, so I've a little bias towards it. Lately, I've been trying to look at FlexBuilder with regards to its strengths. I've found that if I am doing a lot of MXML coding, FlexBuilder is a better tool. You have a class view of the MXML and it is good about hinting for MXML attributes. If you need to do absolute positioning, FlexBuilder also wins because of it's WYSIWYG editor.

But when it comes to ActionScript 3 coding, I'd much rather be in FlashDevelop. FlashDevelop's code hinting is much more powerful, for example it does NOT require you to type exactly the right characters. It's a breeze to jump around to declarations of code as well as nice features for creating getters/setters, promoting local variable to member variables, and creating event handlers. The look and feel of FlashDevelop is also nice, it launches and runs quickly. It has good defaults, a user-friendly UI, and good choices for fonts (I guess that could fall under "good defaults"). FlashDevelop is also free.

Tuesday, October 09, 2007

Interacting with Flex in Flash

I was trying to bring a Flex 2 SWF into Flash CS3 and interact with it. I was able to load the Flex swf without a problem but when I tried to access a public property or function I'd get the following error:

ReferenceError: Error #1069: Property hello not found on
_Main_mx_managers_SystemManager and there is no default value.

So I did the natural thing and Googled it before spending time figuring it out :)
Unfortunately, my search came up empty :(

Looking into the docs class mx.managers.SystemManager gave me the clue I needed. To get at the actual application, I needed to get to the first child of the loaded content. The compiler was unhappy with calling getChildAt() on the content object, so I cast it into an Object along the lines:

var flexApp:Object = (loader.content as Object).getChildAt(0);
trace(flexApp.hello);

That did the trick!

Saturday, September 08, 2007

Code-behind and States in Flex 2

I've always liked the .NET way of developing web pages, where you have your code separated from your layout. Someone just referred me to this post on using Flex in a .NET code-behind style (thanks Steve!). It's very similar to .NET, you just extend the Application class and use it in your MXML file.

It is very cool but there's a minor error where the package name is left off the screenshot (though the instructions are correct). One of the comments on the post noted that in order to use states with this approach you needed to make a small change from "mx.states" to "app.states" where "app" is the namespace of your application.

I thought I'd post a version of this approach that included the states issue. I hilited where you need to change "mx" to "app" and where you need to add "app" as the namespace. The shots below are of code done in FlashDevelop 3 (beta 3).


UPDATE: September 14, 2007 1:07 PM I've had additional troubles with using the code-behind and states. I'm afraid for now it's best to hold off on using the approach.


Sunday, August 05, 2007

Flex 2 States

I've been learning a little about Flex 2 this weekend while working through O'Reilly's Programming Flex 2 by Chafic Kazoun and Joey Lott. There's always a big gap between working through a book and doing a real project when it comes to technology.

That said, one feature of Flex 2 that I think is implemented quite well is states. From my practice, it seems Adobe has implemented the concept in a straightforward manner. States relate to the view. You create different states for different modes your program is in. A program can only have one current state at a time. I'm impressed at how easy it is to set up a series of states and switch between them. When you do switch between states, the different controls on a state preserves its own state, i.e. a checked CheckBox remains checked whether it is part of the active state or not. I look forward to seeing whether I feel the same in actual practice.