Screenshot of SimpleAS3 Framework

An All-in-One Web Development Solution?

You may have heard about Nicolas Cannasse and his revolutionary MTASC Actionscript 2 compiler. For the developers in the Flash crowd, MTASC brings Actionscript to a completely new level of power and flexibility. Unfortunately, Nicolas announced recently that he doesn’t want to make a version of the compiler for AS3. He points out that MTASC provides a missing tool, but the Flex 2 compiler from Macromedia already provides that and promises to build on it. I’d love some strong open source AS3 tools, but I can understand where he’s coming from. He wants to innovate, not recreate.
Read the rest of this entry »

by Josh Tynjala | Start the Discussion

Connecting to AIM with ActionScript 3 Sockets

Update: I've added some more features to FlashAIM, such as text formatting.

The community has been posting some pretty cool things made with Flex2. Andre Michelle's blog has been filled with all sorts of interesting 3D and bitmap experiments. Darron Schall presented a preview of his VNC client which takes advantage of bitmap drawing and the new Socket api. Of course, I couldn't let everyone upstage me, so with a couple weekends of work, I put together some classes for connecting to and interacting with AIM via the TOC 2.0 protocol. I made the basic user interface below just so I'd have a screenshot of the code in action.

Thumbnail image of basic client in action

At the moment, the TocConnection class allows a user to log in, keep track of the status of buddies, send and receive messages, and set his or her own user info. I mainly wanted to get the most vital functions in place before releasing any code. I don't know if I'll expand it, but maybe I'll start a project on OS-Flash so that the community can contribute.

Here's an example of how to use TocConnection in your own code. In the example, event handlers simple trace the output to the console. We start with initialization of the connection and adding listeners to the events. Generally, when a server message is received from AOL, an event is dispatched so listeners can update their status.

After initialization, and once you receive the connect event, you can start sending and receiving messages, and accessing other data. Here you'll notice I set my user info right away, and sent a message to SOME_USER. Normally you wouldn't send automated messages like this, but you can start sending messages to other users after you receive the connection event. Finally, I loop through the list buddies for my account and trace them to the console.

Actionscript:
  1. package
  2. {
  3.     import net.zeusdesign.im.aoltoc.*;
  4.     import flash.display.Sprite;
  5.     import flash.util.trace;
  6.    
  7.     public class ActionTalkTest extends Sprite
  8.     {
  9.         private var _aol:TocConnection; //the main connection class
  10.        
  11.         public function ActionTalkTest()
  12.         {
  13.             //the constructor takes host and port as params, but there are defaults
  14.             this._aol = new TocConnection();
  15.             this._aol.addEventListener(TocEventType.CONNECT, onAolTocConnect);
  16.             this._aol.addEventListener(TocEventType.DISCONNECT, onAolTocDisconnect);
  17.             this._aol.addEventListener(TocEventType.ERROR, onAolTocError);
  18.             this._aol.addEventListener(TocEventType.RECEIVE_MESSAGE, onAolTocReceiveMessage);
  19.             this._aol.addEventListener(TocEventType.UPDATE_BUDDY, onAolTocUpdateBuddy);
  20.             this._aol.connect("YOUR_SCREEN_NAME", "YOUR_PASSWORD"); //sorry, can't have mine
  21.         }
  22.  
  23.         private function onAolTocConnect(event:TocEvent):Void
  24.         {
  25.             //set your user info
  26.             this._aol.setUserInfo("This user is connected with ActionTalk.");
  27.  
  28.             //sendMessage takes a screen name and the message to send to it
  29.             this._aol.sendMessage("SOME_USER", "Hi, I'm talking to you from Flash!");
  30.            
  31.             //the following adds all your buddies to a string and traces it out
  32.             var output:String = "";
  33.             for(var i:int = 0; i <this._aol.groups.length; i++)
  34.             {
  35.                 //a buddy group is basically an array specific to the Buddy class
  36.                 var group:BuddyGroup = this._aol.groups[i];
  37.                 for(var j:int = 0; j <group.length; j++)
  38.                     output += group.buddyAt(j).screenName + " ";
  39.             }
  40.             trace("buddies: " + output);
  41.         }
  42.        
  43.         //disconnect event handler
  44.         private function onAolTocDisconnect(event:TocEvent):Void
  45.         {
  46.             //raw event data is usually the preparsed string from AOL
  47.             trace("Disconnected. " + event.raw); //raw event data is usually the preparsed string from AOL
  48.         }

Other events include ERROR, RECEIVE_MESSAGE, and UPDATE_BUDDY. As far as I know, errors that you receive after sign-in aren't fatal. If you receive it beforehand, the server will drop the connection. When another user sends you a message, a specialized event tells you the screen name , his or her message, and if the message was automattcally sent (an away message). Another event is triggered whenever the state of one of your buddies changes. Typically, this happens one someone goes offline, becomes idle, or receives a warning.

Actionscript:
  1. //error event handler
  2. private function onAolTocError(event:TocErrorEvent):Void
  3. {
  4.     trace("Toc Error: " + event.errorID + " " + event.message + " [" + event.raw + "]");
  5. }
  6.  
  7. //receive message event handler
  8. private function onAolTocReceiveMessage(event:TocImEvent):Void
  9. {
  10.     var output:String = "";
  11.  
  12.     //auto variable in TocImEvent marks auto-replies such as away messages
  13.     if(event.auto) output += "Auto-Reply from ";
  14.  
  15.     //screenName refers to who sent the message and message is what they said
  16.     output += event.screenName + ": " + event.message;
  17.     trace(output);
  18. }
  19.  
  20. //update buddy event handler
  21. private function onAolTocUpdateBuddy(event:TocUpdateBuddyEvent):Void
  22. {
  23.     //generally updates online status, warning level, and idle time
  24.     trace("Update Buddy: " + event.screenName + " " + event.online + " " + event.warningLevel);
  25. }

That's it. The instant messenger source code is available. Get the free Flex 2 SDK from Adobe.

by Josh Tynjala | 24 comments

Impatiently Waiting for ActionScript 3

I spent the morning today reading up on the future of Flash over at Macromedia Labs. This new repository for pre-release alpha software is bursting with information on Flex 2, ActionScript 3, and the Flash 8.5 plugin. There are so many new features that will make the life of every AS developer so much easier. I almost wish they had waited on this. I'm still exploring Flash 8, and now I'll be waiting like a little kid for Christmas to actually put AS3 in production.

A lot of people have mentioned interest in regular expressions and E4X, but I'm looking forward to getting rid of all the hacks required to code AS. Firstly, MovieClips are now instantiated like regular variables. We won't be laughing at newbies when they try this little number anymore:

var mc:MovieClip = new MovieClip();

Not only that, but subclasses can be declared like that too. No more messing with prototypes to get a component on the Stage with only code. Likewise, we don't need to assimilate root with that method anymore anymore either because now it can be the class of our choice. MovieClips have become so flexible that suddenly there's a large inheritance chain of specialized display objects. It starts all the way down from a new EventDispatcher class, which acts as a base, and goes up though half a dozen levels that add functionality at each step. Macromedia highlighted a Sprite class which is a perfect lightweight version of MovieClip for components.

Unfortunately, just two days after I presented my new event model, Macromedia announces that they've redesigned events from the ground up. If a class has any sort of event in AS2, it uses the newer model in AS3. There goes my hatred for non-standard events. Moreover, Delegate is no longer needed to specify a listener function. It's built right into addEventListener. Finally, while listener functions still receive a single event object as an argument, there's a base Event class that we can subclass. I got my strong typing, even if I still have to use the object. They covered every point I made, so I intend to switch back to their version once AS3 officially comes out.

I noticed other interesting tidbits while I skimmed over the docs. There's a new protected keyword that effectively has the same functionality that private does now. This makes private stronger by hiding members from subclasses too. There's also a const keyword for making constants. Sealed classes make dynamic properties and methods impossible at runtime. As a language feature, this isn't necessarily good or bad (I can see arguments both ways), but if a class is sealed, there will be a performance increase, and less memory will be needed in the player. I saw a couple new primitive types--int and uint. They'll be very useful for when floating points aren't needed. Lastly, for those of us that will be migrating our code, we'll no doubt deal with the fact that the mx namespace has been reorganized. Macromedia added many new levels in there, and moved quite a few classes around.

In short, we ActionScript coders are going to be spoiled with so many new tools at our disposal. I'm sure I'll wish I could build a site in AS3 well before it's out in final form, but I'm glad we have a lot of time to experiment and learn about the new features. AS3 has matured well beyond it's years, and its core library definately got a great overhaul. It's a good time to be a Flash developer.

by Josh Tynjala | 2 comments

Going Beyond EventDispatcher

Macromedia's EventDispatcher class is used in almost every one of my Flash projects, and it's the foundation for my component framework. Obviously, I find it's functionality indispensable. However, it comes with limitations that I'd like to live without. For one, specifying a function name to call in a listener could be simpler. Delegate is useful, but having to use it for almost every event call makes it's functionality a good candidate for encapsulation. I've also never been a big fan of the event object the listener receives as an argument. It uses Flash's dynamic typing, which I admit can be useful at times. However, events tend to have uniform arguments, so I'd like it to be a little more type-safe. Ideally, I'd like the listener to receive these arguments like a regular function. With so many needs, I realized it was time to get down and dirty with events.

On a side note, I'd like events in other Macromedia classes to behave the same way. I usually have to check the documentation to remember which way to listen to a particular object. With some classes, you can only listen to all the events at once, and in others, there's a specific function in the object that you have to implement, which will then be in that object's scope. You'll then have to hack your way out to access variables and methods in another object. Of course, I can't replace the system used by some of these classes, they're hardcoded into the player, but it's not too hard to create a wrapper class that hides the clutter and utilizes my new event model to keep everything consistent.

This new event model involves two main classes: Event and EventManager. Event handles one specific event by keeping track of the listeners for that event and dispatching to them as requested. It works behind the scenes as part of EventManager. EventManager is the front end that acts as a repository for events. Every one of my components has one. It's simple to add an event, then pass on any objects that want to listen to a specific event. Here's a basic example of how a component sets up its EventManager:

Actionscript:
  1. import net.zeusdesign.events.EventManager;
  2.  
  3. class SomeComponent
  4. {
  5.     private var _eventManager:EventManager;
  6.     function SomeComponent()
  7.     {
  8.         //instantiate the event manager
  9.         this._eventManager = new EventManager();
  10.  
  11.         //the event is then added to the manager
  12.         this._eventManager.addEvent("onSomeEvent");
  13.     }
  14.  
  15.     public function get eventManager():EventManager
  16.     {
  17.         //It's easier for outside classes to listen to the EventManager
  18.         //directly, so we don't have to implement add and remove methods
  19.         //over and over.
  20.         return this._eventManager;
  21.     }
  22.    
  23.     public function someClassMethod(arg1, arg2):Void
  24.     {
  25.         //now, somewhere else in the class, I want to dispatch the event
  26.         this._eventManager.dispatchEvent("onSomeEvent", [arg1, arg2]);
  27.     }
  28. }

An object that wants to listen to an event, can do so easily. You'll notice I added a third argument to the standard addEventListener. I call this the routing function. This argument allows a listener to specify a specific function to call when the event is triggered. It can be very useful if you have many components that would normally have the same event name. Also, as you saw above, events are passed as an Array to EventManager, but when the event is triggered, the listener function receives arguments like a normal function instead of one event object like EventDispatcher.

Actionscript:
  1. var c:SomeComponent = new SomeComponent();
  2. c.eventManager.addEventListener("onSomeEvent", this, "onCustomEventName");
  3.  
  4. function onCustomEventName(arg1, arg2):Void
  5. {
  6.     //do something
  7. }

You can download the EventManager source code now. If you have any questions, comments, or improvements please drop me an email.

by Josh Tynjala | Start the Discussion

Pages: Prev 1 2 3 ...36 37 38 39 40 41 42 43