View Full Version : Interval Manager, Dev Kit
MichaelxxOA
April 8th, 2006, 06:18 AM
Another little useful class I decided I'd give to you guys since it's going to be a bit before the kit is out.
This is used to manage intervals. It may not seem like a big deal, but it has honestly saved me so much time and grief. It gives you one location for setting and clearing intervals, and even allows you to set a timeout function.
Here is the class, IntervalManager.as
/**
* Flash's interval management is horrible. Here we provide a way
* of setting and clearing intervals, in one location.
*
* @author Michael Avila
* @version 1.0.0
*/
class IntervalManager
{
// stores the interval ID's in a dictionary, so that you can name them
private static var intervalIDs : Object = new Object();
/**
* Sets an interval, storing it's reference in this IntervalManager. If there is already an interval associated with the
* name that you provide (meaning an interval that was created with but not cleared with the IntervalManager), the IntervalManager
* will clear the interval and set the interval you provide in it's place.
*
* @param The name you are assigning the interval, note that this is how you will be deleting it
* @param The path to the function that will be called by the setInterval
* @param The name of the function as a string
* @param The interval that this function will be called on
* @param The args that will be passed to the interval. The arguments are passed as an array, so be sure
* to have your function compensate for that.
*/
public static function setInterval( name:String, path:Object, functionName:String, interval:Number, args:Array ) : Void
{
if ( IntervalManager.intervalIDs[name] != undefined)
{
IntervalManager.clearInterval( name );
}
IntervalManager.intervalIDs[name] = _global.setInterval(path, functionName, interval, args);
}
/**
* Sets a function to be called at "interval" seconds after the timeout is set. If there is already a timeout associated with
* the name that you provide (meaning an interval that was created with but not cleared with the IntervalManager), the IntervalManager
* will clear the old timeout and set the new timeout you provide in it's place.
*
* @param The name you are assigning the timeout
* @param The path to the function that will be called by the setTimeout
* @param The name of the function as a string
* @param The interval that this function will be called on
* @param The args that will be passed to the timeout. The arguments are passed as an array, so be sure
* to have your function compensate for that.
*/
public static function setTimeout( name:String, path:Object, functionName:String, interval:Number, args:Array ) : Void
{
if ( IntervalManager.intervalIDs[name] != undefined)
{
IntervalManager.clearInterval( name );
}
IntervalManager.intervalIDs[name] = _global.setTimeout(path, functionName, interval, args);
}
/**
* Clears the interval with the name you provide.
*
* @param The name of the interval you'd like to clear.
*/
public static function clearInterval( name:String ) : Void
{
_global.clearInterval( IntervalManager.intervalIDs[name] );
delete IntervalManager.intervalIDs[name];
}
}
The code is simple... no need to worry about it.
Here is the code in the fla:
IntervalManager.setInterval("hello", this, "sayHello", 1000, ["Michael"]);
IntervalManager.setTimeout("helloOnce", this, "sayHelloOnce", 1000);
function sayHello(args:Array) : Void
{
trace("Hello, " + args[0]);
}
function sayHelloOnce() : Void
{
trace( "Hello from the TimeOut" );
}
function onMouseDown()
{
IntervalManager.clearInterval( "hello" );
}
This class will be found in the package,
createage.managers
in the Dev Kit.
The documentation for the dev kit can be found here...
www.createage.com/CreateageLib
Take Care.
_Michael
28bit
April 8th, 2006, 04:13 PM
Yea, Flash's interval management really does suck. This is really useful. Thanks for sharing.
MichaelxxOA
April 8th, 2006, 05:15 PM
It's never a problem, I'm more than happy to share when possible. Take Care.
_Michael
MichaelxxOA
April 8th, 2006, 10:58 PM
Here's an update...
/**
* Flash's interval management is horrible. Here we provide a way
* of setting and clearing intervals, in one location.
*
* @author Michael Avila
* @version 1.0.5
*/
class createage.managers.IntervalManager
{
// stores the interval ID's in a dictionary, so that you can name them
private static var intervalIDs : Object = new Object();
/**
* Sets an interval, storing it's reference in this IntervalManager. If there is already an interval associated with the
* name that you provide (meaning an interval that was created with but not cleared with the IntervalManager), the IntervalManager
* will clear the interval and set the interval you provide in it's place.
*
* @param The name you are assigning the interval, note that this is how you will be deleting it
* @param The path to the function that will be called by the setInterval
* @param The name of the function as a string
* @param The interval that this function will be called on
* @param The args that will be passed to the interval. The arguments are passed as an array, so be sure
* to have your function compensate for that.
* @usage <code>
import createage.managers.IntervalManager;
IntervalManager.setInterval("hello", this, "sayHello", 1000, ["Michael"]);
function sayHello(args:Array) : Void
{
trace("Hello, " + args[0]);
}
function onMouseDown()
{
IntervalManager.clearInterval( "hello" );
}
</code>
*/
public static function setInterval( name:String, path:Object, functionName:String, interval:Number, args:Array ) : Void
{
if ( IntervalManager.intervalIDs[name] != undefined)
{
IntervalManager.clearInterval( name );
}
IntervalManager.intervalIDs[name] = _global.setInterval(path, functionName, interval, args);
}
/**
* Sets a function to be called at "interval" seconds after the timeout is set. If there is already a timeout associated with
* the name that you provide (meaning an interval that was created with but not cleared with the IntervalManager), the IntervalManager
* will clear the old timeout and set the new timeout you provide in it's place.
*
* @param The name you are assigning the timeout
* @param The path to the function that will be called by the setTimeout
* @param The name of the function as a string
* @param The interval that this function will be called on
* @param The args that will be passed to the timeout. The arguments are passed as an array, so be sure
* to have your function compensate for that.
*
* @usage <code>
import createage.managers.IntervalManager;
IntervalManager.setTimeout("helloOnce", this, "sayHelloOnce", 1000);
function sayHelloOnce() : Void
{
trace( "Hello from the TimeOut" );
}
</code>
*/
public static function setTimeout( name:String, path:Object, functionName:String, interval:Number, args:Array ) : Void
{
if ( IntervalManager.intervalIDs[name] != undefined)
{
IntervalManager.clearInterval( name );
}
IntervalManager.intervalIDs[name] = _global.setTimeout(path, functionName, interval, args);
}
/**
* Clears the interval with the name you provide.
*
* @param The name of the interval you'd like to clear.
*
* @usage <code>
import createage.managers.IntervalManager;
IntervalManager.setInterval("hello", this, "sayHello", 1000, ["Michael"]);
function sayHello(args:Array) : Void
{
trace("Hello, " + args[0]);
}
function onMouseDown()
{
IntervalManager.clearInterval( "hello" );
}
</code>
*/
public static function clearInterval( name:String ) : Void
{
_global.clearInterval( IntervalManager.intervalIDs[ name ] );
delete IntervalManager.intervalIDs[ name ];
}
/**
* Clears the timeout with the name you provide.
*
* @param The name of the timeout you'd like to clear.
*
*@usage <code>
import createage.managers.IntervalManager;
IntervalManager.setTimeout("hello", this, "sayHello", 2000, ["Michael"]);
function sayHello(args:Array) : Void
{
trace("Hello, " + args[0]);
}
function onMouseDown()
{
IntervalManager.clearTimeout( "hello" );
}
</code>
*
*/
public static function clearTimeout( name:String ) : Void
{
_global.clearTimeout( IntervalManager.intervalIDs[ name ] );
delete IntervalManager.intervalIDs[ name ];
}
}
Templarian
April 8th, 2006, 11:36 PM
This is really coming a long nicely. Your system works so nice.
MichaelxxOA
April 9th, 2006, 02:18 AM
Yeah, I have had this for a while, honestly I have at least 150 classes sitting in my classpath, most of which I use often. Right now I am going through them and deciding what is worthy to be put in the kit, and stuff like this which I find VERY useful and use it quite often, I decided I'd give them to you guys before the kit.
This manager has proved extremely useful in the past, and I hope that it proves useful to you guys. Take Care.
Thanks for the feedback Templarian.
_Michael
phorte
April 9th, 2006, 03:09 AM
Yet again, u never cease to amaze me. Awsome work. Any eta on the kit though, am really looking forward to being able to use it. :D
Keep up the good work. One day ill learn how to write classes, maybe tomorrow... hmm, who am i kidding :lol:
Thanks once again for all your awsome work.
MichaelxxOA
April 9th, 2006, 03:14 AM
The kit is coming out in versions, unfortunately I am a poor musician, I have to find a way to make money.
Their will be a free basic version.
Then a Dev Kit Pro...
The kit is under the name, CreateageLib
Although you have followed me through most of these classes, so PM me your email, or better yet an instant messenger (aim, yahoo or msn) and I will see what I can do.
Take Care.
_Michael
stringy
April 9th, 2006, 04:34 AM
There have been a few posts recently of people having difficulties with setInterval when reloading movies into clips etc, your class eliminates these problems:-Nice work
MichaelxxOA
April 9th, 2006, 05:36 AM
That was an issue forgot to mention, thanks stringy!
_Michael
booler
April 9th, 2006, 01:47 PM
THIS FREAKING ROCKS Oh this should be a built in class I swear!!! you rule
JoshuaJonah
April 9th, 2006, 02:08 PM
^I second that.^
Great work buddy!:kommie:
TheCanadian
April 9th, 2006, 02:31 PM
I hate to burst your bubble but you aren't going to make money selling AS2 classes. Not because they're bad or good or anything like that, it's just that you'd have to be a sucker to spend money on them when there is hundreds more on the web (or you could make it yourself) :)
bombsledder
April 9th, 2006, 03:46 PM
aww your releasing so many classes, maybe i should release my vector class, :O like for the people that know other programming languages
Edit: it is a nice class too, when i first started using flash i never used intervals because they were so hard, maybe you will give some nubs some hope
MichaelxxOA
April 9th, 2006, 03:49 PM
I don't plan on getting rich, and I'm not aiming at just the at home every day programmer. I know a few studios that are already interested, as well as companies that I do consulting work for. Companies don't want to waste time making something that has already been made, if they know it works and it works well, they will spend the money and get it, I know this first hand.
Plus it's not just AS 2.0 Classes, this is just the beginning and the focus. I have alot of Extensions and Components that I have written over the last 2 or 3 years, as well as server side scripts that compliment development and boost efficiency.
With that said, I don't plan on getting rich, if I could pay for hosting that would be great :). Take Care.
_Michael
TheCanadian
April 9th, 2006, 08:16 PM
My point is why pay for something when you can get it for free? But, not having much experience on marketing stuff, I'll take your word for it.
Lindquist
April 9th, 2006, 09:26 PM
You might be able to work out a book deal with friendsofed or o'reilly using a collection of classes along with how and why to implement them. Something like "Efficient Actionscript" or "Time-Saving Actionscript" might get their attention.
MichaelxxOA
April 9th, 2006, 10:44 PM
Friends of Ed would never do that for me :( lol
_Michael
Dauntless
April 10th, 2006, 12:20 PM
I made an IntervalManager class a while ago too.
You can find it here:
http://dauntless.be/blog/?p=11
MichaelxxOA
April 10th, 2006, 05:55 PM
@Dauntless - Nice...
So I've been looking into Lindquist said, and it turns out, it's not nearly as impossible as I was thinking it was going to be.
What are your guys' opinions?
_Michael
gvozden
April 10th, 2006, 06:46 PM
I vote for you to write a book
go 4 it !!!
respect for sharing stuff with community
Lindquist
April 10th, 2006, 09:46 PM
From what I've heard, there's nothing better for solidifying your own understanding of Actionscript than writing a book about it. Try e-mailing some other flash authors to get a real breakdown of what it requires. They're really usually approachable and willing to help.
I'd be glad to read over anything you write (I have a degree in English where I focused on technical writing) to help out with clarity, structure, etc.
P.S.- Don't forget to be funny.
MichaelxxOA
April 10th, 2006, 10:11 PM
I have spoken to a few people now, and it looks like this might actually happen. I'd really like to get your guys' feedback, Take Care.
_Michael
Jeff Wheeler
April 10th, 2006, 10:12 PM
Definitely write a book! That would be so sweet!
And, you're really, really good. :thumb:
MichaelxxOA
April 10th, 2006, 10:20 PM
Aw nokrev... you're going to make me tear up :) Lol, thanks this means alot, seeing as I have alot of support from various forums, I think I'm going to approach this, full force.
_Michael
phorte
April 11th, 2006, 01:30 AM
Book sounds awsome!
RevoGiant
April 11th, 2006, 05:46 AM
Sorry I am an idiot but I am curious as to when setting an interval comes in handy. Is it when you want certain things in a MC for instance to delay or accelerate or pause for different lengths?
MichaelxxOA
April 11th, 2006, 05:53 AM
setInterval allows you to call functions at a specific interval.
Let's say for instance you wanted to say hello overy 1 second ( 1000 milliseconds)
You create a function called sayHello...
function sayHello()
{
trace("Hello");
}
Then you want to create the interval. setInterval takes in a few parameters
The first is the path to the function. Since we are calling a function that is in the same places as the interval is set, the path is the this keyword. You then put the name of the function as a string. The third parameter is the number of milliseconds inbetween each call. And starting at the fourth parameter, all arguments are passed in order to the function you call.
var myInt = setInterval(this, "sayHello", 1000, "Michael");
function sayHello( name:String )
{
trace("Hello " + name);
}
You can then clear an interval that is running using the clearInterval method.
var myInt = setInterval(this, "sayHello", 1000, "Michael");
function sayHello( name:String )
{
trace("Hello " + name);
clearInterval( myInt );
}
In this example your function is only called once, because it is cleared when the function is called.
I hope this shed some light.
Take Care.
_Michael
bombsledder
April 11th, 2006, 09:15 AM
Ya sounds great write a book, I would write it on advanced actionscript, i'll read over it too :P
mprzybylski
April 11th, 2006, 10:59 AM
there is a lack of advanced AS books out there, most are geared towards beginners, so it may not be a bad idea actually. you seem to know a lot and like sharing the knowledge...
Scarybug
April 11th, 2006, 11:27 AM
A friend of mine wrote a chapter for "Gaming Hacks" published by O'Reilly. I don't think it made him rich, but it sure helped his resume.
MichaelxxOA
April 11th, 2006, 02:34 PM
Yeah, I definitly don't plan on getting rich, but I'm sure that writing a book would solidify your resume. I like the idea of Advanced Actionscript. I'm going to try and gather a few ideas, I will post in here as they come to me.
@bombsledder I will definitly take you up on that offer.
Take Care.
_Michael
MichaelxxOA
April 11th, 2006, 02:51 PM
If you guys would like, start posting topics you'd like to read about.
RevoGiant
April 11th, 2006, 03:06 PM
thanks! Thats cool. Why do you think Flash's Interval setting is primitive?
MichaelxxOA
April 11th, 2006, 03:50 PM
There are a number of reasons that I don't care for it.
The main thing is management. setInterval returns an integer that represents it's id. This id is what you pass to clearInterval when you'd like the interval removed. Now due to Flash's nature of allowing code to be on different timelines, and just little scope oddities, you are not given one central location to manage your intervals.
If you set an interval on one timeline and then try to clear it from another, you have to be sure that your pointing to the interval id correctly. With this manager, or managers similar, you are given one place. All of the interval id's are stored in an object under the name you provide. This lends itself alot to clarity, as you can identify a box fade interval as "boxFade".
Another thing that this manager offers is a safer way of managing intervals. With this manager when you set an interval it's id is automatically stored, and if you try to overwrite an interval that is currently going the old interval is removed first. This keeps you from allowing intervals to run wild, because once you lose the intervalID it's very difficult to clear that interval.
I hope this made sense, take care.
_Michael
RevoGiant
April 11th, 2006, 04:36 PM
wow thanks Michael!
MichaelxxOA
April 11th, 2006, 04:47 PM
Another thing that I forgot to mention is that this Manager will free the memory used to store the intervalID, something that does not automatically happen with clearInterval... Take Care.
_Michael
Jeff Wheeler
April 11th, 2006, 05:53 PM
Yeah, I definitly don't plan on getting rich, but I'm sure that writing a book would solidify your resume. I like the idea of Advanced Actionscript. I'm going to try and gather a few ideas, I will post in here as they come to me.
On the other hand… if you happen to… I'll take a few million. ;)
MichaelxxOA
April 11th, 2006, 05:57 PM
of course...
Jeff Wheeler
April 11th, 2006, 05:58 PM
Thanks ;)
MichaelxxOA
April 11th, 2006, 06:24 PM
No worries... just um... don't get your hopes up :).
_Michael
Jeff Wheeler
April 11th, 2006, 06:29 PM
:lol: You're so confident. ;P
JoshuaJonah
April 11th, 2006, 06:37 PM
Mike, i bought your book and it sucks..:D What was that 'pink elephant' section about? I feel it could have used more words, you know like more pages..
Sorry i'm tired, and it was supposed to be funny. :(
I'll buy your book when it comes out. I hope it has a pink binding:)
MichaelxxOA
April 11th, 2006, 06:39 PM
hahaha, it was funny.
I didn't think I'd have this much support, and right now it's just a thought.
Thank you guys, you can't imagine how much I truly appreciate it.
Take Care.
_Michael
MichaelxxOA
April 14th, 2006, 01:15 AM
I decided that I'm going to have a wiki for the progress of this book, anyone know of any good wiki software?
_Michael
Jeff Wheeler
April 14th, 2006, 01:33 AM
Wikibooks :thumb:
MichaelxxOA
April 14th, 2006, 02:05 AM
I considered that... I'm not sure, I'll have to figure out all of the legal issues. Thanks for all the support nokrev, it means the world.
TakeCare.
_Michael
Scarybug
April 14th, 2006, 09:53 AM
I kind of like MoinMoin.
MichaelxxOA
April 15th, 2006, 01:19 PM
Thank you very much Scarybug, that looks interesting.
TakeCare.
_Michael
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.