Flash & Flex Intermingling

jungle_feverA reader (I have readers?) wrote the following:

You said you’ll be extending Donovan’s PageFlip class
http://blog.hydrotik.com/2007/08/15/pageflip-tweener-as3-part-1/#comment-478

I’m wondering how did you proceed with that? Any chance to see sources somewhere? :)

Thanks in advance!

And I realized my reply would be too long to fit in a comment box and decided that I could probably milk this for warrant another blog post.

While porting the pageflip class, I found that it was so heavily wedded to Flex, that porting such basic classes as the View Stacks and all the other hodge podge in the mx packages would quickly launch this into the over-budget area.

My next course of action would be to try to load the flex swf into flash, but this quickly became a very big problem with AS3’s strict typing. Trying to coerce Flex’s Canvas class to Flash’s Stage class became an unbearable task, so strike 2.

What eventually worked out very well was to create a singleton class that was imported into both the flex page flipping and the flash application, the singleton would then dispatch events all over the place, communication between the two worked out pretty well.

If you’re not familiar with singleton classes in AS3, they’re quite possibly the simplest design pattern to implement, and don’t take any architectural changes. It’s simply a class that can only be instantiated once. While this doesn’t seem to have any obvious benefits over using static vars and static functions of a class, keep in mind that you can put this singleton class on the stage, and it can have instance variables that can quickly and easily be accessed by any other class. While MVC is complete overkill for the scale of flash projects that take up 99% of our time, placing a singleton class as the “hub” of your application can lend some very quick and necessary organization to most projects.

Look-see here:I’ve gotten great mileage out of making any basic media containing class a singleton, there always seem to be cases where outside classes in the project need to access the mediaplayer to tell it to either play a certain video, pause, or just shut up.

[code lang="Actionscript"]

package com.hailmothra.video
{
import flash.display.Sprite;

public class Player extends Sprite
{

public static var allowInstantiation:Boolean;
public static var instance:Player;

public function Player( ) : void
{
if ( allowInstantiation ) {
//one time instantiation code goes here!

} else {
throw new Error( "too many players!" );
}

}

public static function getInstance() : Player
{
if ( instance == null ) {
allowInstantiation = true;
instance = new Player();
allowInstantiation = false;

}

return instance;
}

public function stopVideo() : void
{
media.stop();
var e:Event = new Event( Event.CANCEL );
this.dispatchEvent( e );
}

}

}

[/code]

Now any class that wants to access the player can do it with a simple call like this:

var player:Player = Player.getInstance();
player.stopVideo();

Any classes that are listening to events from this class will then act on them, regardless of their being Flash swfs or Flex swfs.


About this entry