Archive for category ActionScript

MouseWheelManager Class

OVERVIEW
Flash allows you to capture the events from a mouse wheel. The event fires with two params (delta:Number, scrollTarget:MovieClip). In most cases, you only want one thing to respond to this event. For example, if you have two listboxes with scrollbars, you only want the one that your mouse is over to respond to the event. You could accomplish this by having each element listen for the wheel event and do it’s own check to determine whether it should handle it or not. However, if you did this everytime the event fired, all the handler checks would have to run. In the case of having 50 elements listening, that would be 50 if checks, which is inefficient.

COMPONENT TECHNIQUE
If you dig into the Flash component classes, you will find they implement the check rather intelligently. In mx.core.ScrollView, a single listener is implemented. The handler evaluates the second param (scrollTarget) to determine which component should handle the event. It does this by starting at the MovieClip target passed in and working up the parent chain until it finds a MovieClip that is a ScrollView instance. Once it finds that instance, it makes the instance dispatch a scroll event.

SIMPLE SOLUTION
I created a MouseWheelManager class that has the same intelligence as the ScrollView, but is a lot simpler to use . To utilize it with an Class or Object, instead of making that class subclass ScrollView (doesn’t rely on additional classes), the class just has to have a onMouseWheel handler and register itself and associated MovieClip with the manager.

/**
* Dreamsocket
*
*  @author : Kenny Bunch
*  @example:
*
*           import com.dreamsocket.managers.MouseWheelManager;
*
*            var o:Object = {};

*            o.onMouseWheel = function(p_delta:Number, p_startingTarget:MovieClip):Void
*           {
*                  trace("works");
*           }
*
*            MouseWheelManager.getInstance().register(foo_mc, o);
*/

class com.dreamsocket.managers.MouseWheelManager
{
        private static var k_instance:MouseWheelManager;

        private var m_targets:Object;

        private function MouseWheelManager()
        {
                this.m_targets = {};
                Mouse.addListener(this);
        }

        public static function getInstance():MouseWheelManager
        {
                if(MouseWheelManager.k_instance == null)
                {
                        MouseWheelManager.k_instance = new MouseWheelManager();
                }

                return MouseWheelManager.k_instance;
        }

        public function register(p_tgt:MovieClip, p_instance:Object):Void
        {
                this.m_targets[p_tgt] = p_instance;
        }

        public function unregister(p_tgt:MovieClip):Void
        {
                delete(this.m_targets[p_tgt]);
        }

        private function onMouseWheel(p_delta:Number, p_startingTarget:MovieClip) : Void
        {
                var testTarget:MovieClip = p_startingTarget;
                var target:MovieClip;

                while(testTarget != null)
                {
                        if (this.m_targets[testTarget] != null)
                        {       // target exists in list and is bottom of tree set as target and stop climbing parents
                                target = testTarget;
                                break;
                        }
                        //  child mc is not a target climb to parent and test
                        testTarget = testTarget._parent;
                }

                if (target != null)
                {       // target found dispatch event
                        this.m_targets[testTarget].onMouseWheel(p_delta, p_startingTarget);
                }
        }
}

5 Comments

AS3: Visualizing Video

In an attempt to start using AS3 and Flexbuilder, I created a simple module (based in part on Brendan Dawes “Cinema Redux” processing experiment), that visualizes a movie by taking snapshots of frames on a supplied sample rate. It uses those snapshots to create a visual mosaic image, which allows you to see the use of light, color, and transition throughout the movie.
Read the rest of this entry »

6 Comments

BrowserHistory AS1 Class and Files

I originally posted my AS1 BrowserHistory class and associated files in 2002 and it was distributed on various sites. In 2005, with my own site, I decided to finally give it a permanent home

What
Normally, pressing a browser’s forward or backward navigation buttons on a Flash page will take you out of the movie. The BrowserHistory class allows developers to create a history list that can trigger actions based on the use of these buttons.

Why
I have noticed a number of solutions out there that attempt to resolve the back button problem, most noteably Robert Penner’s work (www.robertpenner.com) and more recently Mike Chamber’s Pet Store back button. Robert introduced using MX’s LocalConnection object to get around all the browser issues, and I really like the new solutions that implemented this approach, however all of them seem to be missing something. Robert’s required the creation of a new swf and html page for every History object. Mike’s limited you to passing strings through the browser’s url and doing your own data conversions. The javascript approach also didn’t work in the newer Netscape and Mozilla browsers, because appending variables to a url is not treated as a new history object in the browser.

What I really wanted from a History object in Flash was something that:
1) would work on all platforms and browsers with the 6 player
2) would be as simple to use as adding History elements through script and defining a function to handle a history event
3) would only require including extra files and not manipulating them
4) would allow me to work with any predefined datatype

In my attempt, I tried to accomplish these goals by moving the persistant storage of the History elements into a SharedObject instance and using 4 predefined swfs and their accompanying html files (total of 8 files) to create a way of checking status. Since there are only those 8 files used in tracking status, no matter how many history elements you add, a user will only request a total of 8 additional files (this is nice if you think about server loads, the other methods request a file for every history element). Using this method of storage and status does not require the manipulation of extra files, instead all of the work is done “behind the scenes” by the BrowserHistory Class, the only requirements are that you include the proper files and know how to script a BrowserHistory instance.

I hope that this can be of use to someone.
ENJOY :)
Kenny Bunch

7 Comments

asfunction: Definition, Call Paths, and Scope

For anyone unfamiliar with asfunction I will explain the basics first. For those already comfortable with it, but want to understand how to resolve it’s call path and scope to a class instance, feel free to skip to the latter part of this article.
Read the rest of this entry »

5 Comments

Is mx.utils.Delegate.create inefficient?

I use mx.utils.Delegate.create constantly, however in looking at the source code, there are some memory inefficiencies. In the create method, there is a function literal (the var f). By creating this function literal, the create method is actually prolonging the scope of it’s arguments and it’s local variables in the scope of f for the lifetime of f. So what does all this mean? Well, it means that in the scope of f you have create’s argument’s obj and func still available. Yet, f reference’s these values by creating local variables from static properties attached to the function itself, instead of accessing them directly.

Read the rest of this entry »

7 Comments

Magic Strings vs Constants

In looking at pieces of the MM framework, and how a lot of developers write their own code, I’ve noticed a lot of magic string usage. What do I mean by a magic string, let’s examine the following piece of code for a definition:

myComponent.addEventListener("change", this);

In this example, “change” is the magic string. The reason that it is a magic string is that code is dependent on that string being correct, but yet there is no formal assertion that it is correct. For example, if in the example I mispelled “change” as “chnge”, my code would fail silently. Though this may not be catastrophic, you are left to your own debugging methods to figure out what went wrong. Fortunately, there are several ways to remedy this. The easiest and most common is to implement some sort of constant variable in your code to represent the string. For example:

// definition : mx.controls.ListBox.EVT_change = "change";
myComponent.addEventListener(mx.controls.ListBox.EVT_change, this);

In this example, the dispatching object actually has a static property EVT_change that is equal to the string and which is used to represent the string. If you use this method and type in a property that doesn’t exist, you will get an error. So with little effort you can have strict typing, better debugging, and more relaxation time.

4 Comments

IntervalManager

Early on in developing Central apps, I found that I needed something to manage all my Intervals in an application. It was important to have a central place that I could identify, create, and clear intervals, so I developed a singleton manager (IntervalManager) to handle this. In addition, I overloaded the setInterval method, so that one could create delayed calls like setTimeout in javascript, or create an interval that ran N number of times. I managed this by adding a prefixed param N to the setInterval method, which if used tells the interval to run N times, and if not defaults to normal setInterval behavior. Below is the class with usage definitions.
Read the rest of this entry »

13 Comments

File Transfer with Central and Flashcom

I had a theory I wanted to test this morning : Can I transfer files from point 2 point using Central and some sort of proxy. 5 minutes of thought, 5 minutes of code, and the answer was yes. Basically I just read in a binary file, sent the binary data across the wire, wrote the binary on receive, and opened it up. Below is the echo code. NOTE: the flashcom app does nothing but accepts the connection.
Read the rest of this entry »

8 Comments