I did this one tonight at the request of Jaffasoft
Im a little tired so I hope there arent TOO many mistakes heh. And I didnt include an Appendix as I did with ASBroadcaster, though I dont think theres really as much you can do to extend the use of setInterval. The applications section give some basic usage. Should be a pretty decent go through on the function though:
i appreciate that you explainded the setInterval here.
I have to add the following: I played with the setInterval command since MX is out and I found out that it's values are as dependant on cpu speed as the onEnterFrame version or as the getTimer command !
Don't count on those "milliseconds".
If you have a complex function, that you are checking, 1 ms could mean 10 or more.
There is a limitation to how fast the Flash player can display or calculate information, so in that, in any kind of situation where flash cant handle the payload, you will get a delay and calculations wont match up with the time set to process them. Flash will not skip calculations to adhere to timing so that will drag your interval back from where you set it to a rate which Flash cannot match.
Here is a little example for a metronome.
(see attached file)
The only thing that Flash has to do during the Interval is to play a sound.
In this case the sound is even uncompressed, so not much CPU Power is needed.
Now, if you type in for example 100 BPM in the first Inputfield and 130 BPM in the second, guess what, you don't here a difference...
And normally 100 vs 130 BPM is a BIG difference ( think seventies disco vs techno music)...
If you go lower, the a difference is there, but ist is far from the tempo it should have compared to a real metronome.
I don't know if this is a bug or just the way it is, but I think MM shouln't really use "milliseconds"(Forget using this as a means to build for example a halfway precise timer).
Just my two cents...
Ah, and maybe I did something wrong ...
So here's the code:
Code:
//create Sound Variables
rim = new Sound ();
rim.attachSound ("RIM");
clave = new Sound ();
clave.attachSound ("CL");
//the play functions
function playit ()
{
rim.start ();
}
function playitTwo ()
{
clave.start ();
}
//the functions that contains the Intervals
function Metronome ()
{
tempo = Math.round (15000 / (bpm));
clearInterval (inta);
inta = setInterval (playit, tempo);
}
function MetronomeTwo ()
{
tempoTwo = Math.round (15000 / (bpmTwo));
clearInterval (intaTwo);
intaTwo = setInterval (playitTwo, tempoTwo);
}
//the play buttons
buttonOne.onRelease = Metronome;
buttonTwo.onRelease = MetronomeTwo;
buttonBoth.onRelease = function ()
{
MetronomeTwo ();
Metronome ();
};
//the stop buttons
stopbuttonOne.onrelease = function ()
{
clearInterval (inta);
};
stopbuttonTwo.onrelease = function ()
{
clearInterval (intaTwo);
};
stopbuttonBoth.onrelease = function ()
{
clearInterval (inta);
clearInterval (intaTwo);
};
//updating the Interval Value on Input change
input.onChanged = function ()
{
tempo = Math.round (15000 / (bpm));
};
inputTwo.onChanged = function ()
{
tempoTwo = Math.round (15000 / (bpmTwo));
};
playing sounds isnt exactly an easy chore. This coming from flash where it has a hard time maintaining a consistent framerate even when theres nothing on the screen sometimes. When dealing with the initialization of sounds over and over again in tight succession, Flash will have trouble - as you have seen. So yeah, its not too reliable there.
I didn't really think it would be so hard to just play a sound, but then, hey, flash started as a vector based web animation tool and not as a director successor...
Do you know if shockwave would be better then this, or would it have the same problems.
A client is thinking about an advanced sound sequencer engine, and I was a bit too optimistic on some features I guess
I would assume director might be better... though I have had issues with Director performance in the past as well. However, most of my director experience comes out of basic run of the mill educational CDs and nothing quite as audio-elaborate as a sequencer of the such. If you have access to director, it wouldnt hurt to run a few tests and see how well it does
for that, in director, you might want to use timeout
ex:
Code:
on prepareMovie
timeout("m1").new(1000, #m1tick)
timeout("m2").new(1100, #m2tick)
end
on m1tick
sound(1).play(member("m1sound"))
end
on m2tick
sound(2).play(member("m2sound"))
end
I was searching trough the forums to find something for my project, where i want timed buttons to trigger some functions. But since our creative director whats them to act on rollOver .. how would i make them wait for, say, 3 seconds, and the trigger rollOver actions?
What if i move over to another button before completing, or if i pass the mouse over a couple of them? Do i need a clearInterval in every mouseOut?
This would put an end to my everlasting headache! !
Thanx in advance
youd probably want to use the setTimeout example at the bottom of the page. Its an interval that clears itself after one call. Then you just set up a function to be run and act as the rollover delay actions.
With basic usage though, with the setTimeout functionality, you can rollover multiple times and each one of those rollovers will register as an action to be performed after the time allotted, even if done before the time ends. However, if you want the action timing to be reset on each rollover so it doesnt compound the actions, you would want to be sure to clearTimeout for each rollover so that the actions are reset and you wont have them happen in mass.
ex:
Code:
// define setTimeout variant of setInterval which clears
// itself after one call
_global.setTimeout = function(a,b,c, args){
// for a basic function call:
if (typeof arguments[0] == "function"){
args = arguments.slice(2);
var ID, func = function(){
a.apply(null, args);
clearInterval(ID);
}
ID = setInterval(func, b, args);
// for an object method call:
}else{
args = arguments.slice(3);
var ID, func = function(){
a[b].apply(a, args);
clearInterval(ID);
}
ID = setInterval(func, c, args);
}
return ID;
}
_global.clearTimeout = clearInterval;
// define delayed action to occur after the button is rolled over
DelayAction = function(){ trace("Executed") }
// define rollover action for button
myButton.onRollOver = function(){
clearTimeout(rollID); // clear any previous rollover delay to prevent multiples
rollID = setTimeout(DelayAction, 1000); // set new delay action for 1 second
}
But it wasn't really what i wanted to achieve.
I want my buttons to trigger an action only when the user puts the mouse pointer over them for at least 1 second, and only then, trigger any clearInterval and launch actions.
I have set dinamic text fields linked to the rollID variable, and i can see rollID growing each time i roll over one of my buttons. Maybe that's why it doesn't clear previous intervals ..
Here is a fla file, if you'd like to take a look at it.
What i desperately want is it to behave almost like windows menu items, that only trigger actions whenever you are over them a certain amount of time (say half a second, or a second).
Senocular, if you have some time to take a look at it, i would be really thankful!
well first off I can say that make sure you arent using a var in your textfield as the ID of the interval. That makes the interval a string when referenced and not the actual interval which may screw things up (if you are doing so). Ill take a look at the fla when I have some time.
2 other things:
1) dont have the "off" label on the stop frame in your menu animations. If your animation isnt fully rolled out, playing that frame will call the stop command and your menus wont actually rollout. Instead, you can make a new keyframe AFTER the stop frame and lable that "off" (be sure to remove the first "off" label).
2) another thing is that since you are delaying the menus, its possible to rollover the button and not have the menu appear. However, in rolling off the button you are still calling the off frame to close the menu.. but if the menu isnt open, then it creates an instant non existant menu to pop in and close even though it was never open. This can be fixed by changing funcCierra ... and easily at that. Just change
cual.gotoAndPlay("off");
to
if (cual._currentframe > 1) cual.gotoAndPlay("off");
so that it first checks if the menu has surpassed the opening point and is open or atleast on its way to being opened.