Do you have a collection of methods that you seem to use quite a bit from one project to another? You know, simple things like creating random numbers between a range of two values, or returning the characters in a string that follow a “.” - to get the extension, things like that.
Well I have. I try to keep it small because I use it on most projects for just a few methods here and there so I don’t want it to get bloated. And I gave it the ultra short class name of “Q”. ( If you understand why it’s named Q then kudos to you. )
The Q class ( download ) is full of static helper methods. That means all you have to do is import the class and then call the methods directly.
The import statement looks like this:
import com.polygeek.utils.Q;
You have to put the Q.as file in a folder in your global class path with folder structure of: com/polygeek/utils/Q.as.
Here’s a breakdown of what Q can do for you:
Just say it
This is great for tracing statements in a production environment. What it does is create a TextField on top of everything else in your design. Then it writes the String that you pass in to the TextField. If you add another message later it will just write it to the same TextField.
Plus you can click and drag the TextField around and get it out of the way. By default it will start in the upper-left corner of the SWF.
Here’s an example:
Q.say( "is this working?" );
Q.say( "here's another line.);
Q.say( "click and drag to move me around" );
A few Math tools
var rndNumber:Number = Q.rndRange( beginning:Number, end:Number );
If you need a random number between a set range then use this. For instance, if you needed a random number between 400 and 600 then you would write something like this:
var rndNumber:Number = Q.rndRange( 400, 600 );
Every time that code runs you’ll get a different value between 400 and 600.
You can do negative numbers as well. If you needed a random number between -2 and 2 then just do this:
var rndNumber:Number = Q.rndRange( -2, 2 );
You can use this tool below to see the values rndRange would output.
Q.pythagorean(x:Number, y:Number); // returns a number
Use this if you need the square root of the sum of two numbers squared - that would be the Pythagorean theorem.
Or basically the same thing if you need the distance between two MovieClips - by registration points.
Q.distance(a:MovieClip, b:MovieClip); // returns a number
Getting Physical
If you’ve read Kieth Peter’s Making Things Move book(s) then you’ll recognize these next two methods
Q.newton(a:MovieClip, b:MovieClip); // this applies the result to the two MCs and does NOT return a value
Here’s the nuts and bolts:
Q.newton(a:MovieClip, b:MovieClip):Void {
var dist:Number = Q.distance(a, b);
var force:Number = a.mass * b.mass / dist*dist;
var ax:Number = force * (b._x - a._x) / dist;
var ay:Number = force * (b._y - a._y) / dist;
//
a.vx += ax / a.mass;
a.vy += ay / a.mass;
b.vx -= ax / b.mass;
b.vy -= ay / b.mass;
}
And of course momentum plays a big roll in many of the MTM examples. So this might help:
Q.mo(mc0:MovieClip, mc1:MovieClip):Number {
return ((mc0.mass - mc1.mass) * mc0.vx + 2 * mc1.mass * mc1.vx) / (mc0.mass + mc1.mass);
}
Basic stuff
Draw a border around a MovieClip at runtime. If you don’t pass any arguments then it will draw a white border around the Stage.
Q.stageBorder(strokeWidth:Number, strokeColor:Number, strokeAlpha:Number, mc:MovieClip, fillColor:Number, fillAlpha:Number);
Remove unwanted carriage returns
New with CS3 is the added carriage return at the end of all authoring time TextFields - read read more about it.. That can cause big problems when you are reading the text value of a TextField. This little handy-dandy method will clean that up for you.
var cleanedUp:String = Q.chopLastChar( someTextField.text );
All that does is look at the string that you passed in. If there is a carriage return at the end it will remove it and pass the string back. Otherwise it just passed he string back to you unchanged.
Getting the Time
You pass in a number of seconds and this method will return a formated string of hours:minutes:seconds for you. Take it for a test drive if you want. ( Try inputing something like “3601″ which is 1 hour and 1 second.)
Download convertTime
Usage: var formatedTime:String = Q.timeOut( numberOfSeconds );
Note: I had to use the Q.chopLastChar in the SWF above to get rid of the carriage return.
Doing something to every element of an array
I don’t want to count the number of times I’ve written the code to iterate through every element of an array and perform some action on each element. It’s quick and easy to do but sometimes I get tired of writing:
var len:Number = myArray.length;
for( var i:Number = 0; i < len; ++i ) {
// do something here
}
So I wrote a little method to speed things up a bit.
Q.applyToArray( array:Array, fn:Function );
That little snippit of code will take the array that you passed and iterate through it. In the process it will pass the current element of the array and the current index to that function. So this won’t work for you in every case but probably in most of them.
Here’s the code for the method so you can see better what’s going on and how simple it is:
public static function applyToArray( a:Array, fn:Function ):Void {
var len:Number = a.length;
for( var i:Number = 0; i < len; i++ ) {
fn( a[i], i );
}
}
The downside is that it makes your code a tiny bit harder to read because we’re used to seeing for-loops and this removes them. But you’ll get used to it.
Making Reflections
If you would like to make a reflection of any MovieClip then just use this:
Q.createReflection( source:MovieClip, gradient:MovieClip );
This method takes two MovieClips, the source that you want to be reflected, and the gradient where you want the refection to be located.
The gradient is used to position the reflection and is applied to the reflection as a mask. If you create an alpha gradient then that will in turn fade out the reflection.
Because this is running an interval you can make a reflection of video, or anything that is animated and the reflection will continue to show the visual state of the source MovieClip.
Creating Copies
This method is almost exactly the same as createReflection. But this one obviously doesn’t flip the destination MC over or anything. It just copies it.
Q.createCopy( mc:MovieClip, dest:MovieClip );
Honestly, this and the createReflections shouldn’t really be here. I added them because they were useful at my previous job but I probably won’t use them much anymore, at least not in AS2. I just thought I’d leave them in here in case someone could use them.
Making it draggable
It’s very easy to make a MovieClip draggable with a mousePress/mouseRelease. And it doesn’t require all that much code. But, it could be easier. Try this:
Q.makeDraggable( mc:MovieClip );
Download Draggable Example
You just pass the MovieClip that you want to be draggable and you’re all done. You don’t have to pass in the callback functions unless you need them. In the example you can see that the larger MovieClip has callbacks because the _alpha changes when you press and release the mouse.
Suggestions
What about you. Do you have any suggestions for some useful methods that could be added here? Soon I’ll run through all these methods and convert them to AS3 and publish that class as well.