dcsimg
A Flash Developer Resource Site

Page 1 of 4 1234 LastLast
Results 1 to 20 of 68

Thread: MX setInterval Explained

  1. #1
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361

    MX setInterval Explained

    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:

    http://www.kirupa.com/developer/acti...etinterval.htm


    _____________________________________________


    (last posted article on Listeners and ASBroadcaster)
    (next posted article on Depths)
    Last edited by senocular; 03-08-2004 at 10:43 AM.

  2. #2
    Moderator enpstudios's Avatar
    Join Date
    Jun 2001
    Location
    Tampa, Fl.
    Posts
    11,282
    Another great job, thanks senocular !!

  3. #3
    FK Newb Beater
    Join Date
    Dec 2002
    Location
    Seattle
    Posts
    676
    Oh man, you don't even know how far and wide I have searched for those answers!
    Now my Toolbox of knowledge just got bigger thanks to senocular!
    Just because you changed the code, doesn't mean it's yours

    Most Recent Work:
    Commercial tanning beds website

  4. #4
    Goooold Member!
    Join Date
    Oct 2002
    Posts
    259
    Thanks a lot man. setInterval is gonna make my life a lot easier .
    Last edited by Cyanide300; 02-18-2003 at 04:26 PM.

  5. #5
    Thanks Sen, I book marked it.

  6. #6
    Flash Initiate StuartW's Avatar
    Join Date
    Jul 2000
    Location
    The Metaverse
    Posts
    504
    Wicked... nice one!

  7. #7
    Senior Member fantasio's Avatar
    Join Date
    May 2000
    Location
    at the progress bar
    Posts
    409

    setInterval IS Performance dependant !!!!

    Hi sen,

    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.


    regards

    fantasio

  8. #8
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    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.

    Ill add that in, thanks

  9. #9
    Senior Member fantasio's Avatar
    Join Date
    May 2000
    Location
    at the progress bar
    Posts
    409

    Not Quite....

    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));
    };
    Attached Files Attached Files

  10. #10
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    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.

  11. #11
    Senior Member fantasio's Avatar
    Join Date
    May 2000
    Location
    at the progress bar
    Posts
    409

    ah, ok

    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

    later

  12. #12
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    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

  13. #13
    Member
    Join Date
    Mar 2000
    Location
    Madrid, Spain
    Posts
    51

    Timed buttons?

    Thanx for the complete info, senocular ..

    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
    ..|..|..|..|..|..|..|..|..|..|..|..|..|
    TiMuN .. reasonably crazy spaniard jack-of-all-trades

  14. #14
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    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
    }

  15. #15
    Member
    Join Date
    Mar 2000
    Location
    Madrid, Spain
    Posts
    51
    awesome!!

    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.

    Time is running out ...

    Thanx anyway!
    ..|..|..|..|..|..|..|..|..|..|..|..|..|
    TiMuN .. reasonably crazy spaniard jack-of-all-trades

  16. #16
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    just add
    Code:
    myButton.onRollOut = function(){
    	clearTimeout(rollID); // clear any previous 
    }

  17. #17
    Member
    Join Date
    Mar 2000
    Location
    Madrid, Spain
    Posts
    51
    I did already .. thanx for the quick answer thou.

    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 ..
    Last edited by TiMuN; 03-10-2003 at 07:16 AM.
    ..|..|..|..|..|..|..|..|..|..|..|..|..|
    TiMuN .. reasonably crazy spaniard jack-of-all-trades

  18. #18
    Member
    Join Date
    Mar 2000
    Location
    Madrid, Spain
    Posts
    51
    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!
    Attached Files Attached Files
    ..|..|..|..|..|..|..|..|..|..|..|..|..|
    TiMuN .. reasonably crazy spaniard jack-of-all-trades

  19. #19
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    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.

  20. #20
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    ok...
    yes, lose the rollID textfield.

    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.



    I think that should do it!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center