A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: pausing a loop

  1. #1
    ( x x ) ( x x ) ( x x ) xxblindchildxx's Avatar
    Join Date
    Aug 2003
    Location
    London
    Posts
    282

    pausing a loop

    hey guys,
    i have the following script which builds a grid in a movieclip to be used as a dynamic mask:
    code:

    function buildMask(mask) {
    var maskHeight = 15;
    var maskWidth = 8;
    for (var i = 0; i<maskWidth; ++i) {
    vertrev = function(){
    for (var j = 0; j<maskHeight; ++j) {
    duplicateMovieClip("diam", "diam_"+i+"_"+j, i*100+j*2);
    if (j/2 == int(j/2)) {
    setProperty("diam_"+i+"_"+j, _x, (i*50)+25);
    setProperty("diam_"+i+"_"+j, _y, j*25);
    } else {
    setProperty("diam_"+i+"_"+j, _x, i*50);
    setProperty("diam_"+i+"_"+j, _y, j*25);
    }
    }
    }
    }
    buildMask();
    stop();



    it looks ok, but i think it would look better if the grid wasnt created instantly. instead what i want is to pause the loop for a short amount of time, so that it creates a sort of swooping effect.
    basically...
    code:

    function buildMask(mask) {
    var maskHeight = 15;
    var maskWidth = 8;
    for (var i = 0; i<maskWidth; ++i) {
    vertrev = function(){
    for (var j = 0; j<maskHeight; ++j) {
    // I WANT TO PAUSE FOR 1000milliseconds HERE
    duplicateMovieClip("diam", "diam_"+i+"_"+j, i*100+j*2);
    if (j/2 == int(j/2)) {
    // EVEN
    setProperty("diam_"+i+"_"+j, _x, (i*50)+25);
    setProperty("diam_"+i+"_"+j, _y, j*25);
    } else {
    setProperty("diam_"+i+"_"+j, _x, i*50);
    setProperty("diam_"+i+"_"+j, _y, j*25);
    }
    }
    }
    }
    buildMask();
    stop();


    how can i create the "pause for 1000 milliseconds" part of the code? i read about setInterval, but im not sure how to include it in my script.
    if you know how to apply a setInterval to this script please let me know, it would really help me out a lot.
    thank you in advance :-)
    Ross
    Ross McDowell | WannabeDesigner |
    T: 020 7916 8269 | E: blindchild17@hotmail.com

  2. #2
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    you cannot pause a loop in the manner you require. You can recreate the loop effect though by incrementing variables manually within a setInterval function. See code below as an example.

    Alternatively you could use you existing code, but make all of the duplicate MC's not visible, and then use a similar setInterval function to make them reapear.

    Hope this is of some help.

    Code:
    // set init vars //
    var maskHeight = 15;
    var maskWidth = 8;
    i=0;
    j=0;
    
    /// set interval object ///
    buildObj = new Object();
    buildObj.interval = function(){
    	duplicateMovieClip("diam", "diam_"+i+"_"+j, i*100+j*2);
    	// if j%2 is true (is odd) then _x = i*50 else _x = (i*50)+25
    	_root["diam_"+i+"_"+j]._x = (j%2) ? i*50 : (i*50)+25;
    	_root["diam_"+i+"_"+j]._y = j*25;
    	// if j=maskHeight then make j=0 else add one to j
    	j = (j == maskHeight) ? 0 : j+1;
    	// if j=0 then add one to i (else i = i)
    	i = (j == 0) ? i+1 : i;
    	// if have placed all mc's then clear interval
    	if (i==maskWidth) { clearInterval(buildIt) };
    }
    
    /// start interval function
    buildIt = setInterval(buildObj,"interval",100);
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  3. #3
    ( x x ) ( x x ) ( x x ) xxblindchildxx's Avatar
    Join Date
    Aug 2003
    Location
    London
    Posts
    282

    getting warmer.

    thank you Lexicon for your reply.
    i tried to implement your script into my movie, but it didnt work the way i would like it to.
    maybe i wasnt clear about what i would like to learn how to do.
    ive attached a file this time, which i hope will demonstrate what i am trying to achieve.
    if you have the time it would help me a great deal if you could steer me in the right direction.
    thank you again.
    Ross
    Attached Files Attached Files
    Ross McDowell | WannabeDesigner |
    T: 020 7916 8269 | E: blindchild17@hotmail.com

  4. #4
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    just jumble around what we already have....

    Code:
    // set init vars //
    var maskHeight = 5;
    var maskWidth = 3;
    i=0;
    j=0;
    
    buildObj = new Object();
    buildObj.interval = function(){
    	duplicateMovieClip("diam", "diam_"+i+"_"+j, i*100+j*2);
    	_root["diam_"+i+"_"+j]._x = (j%2) ? i*50 : (i*50)+25;
    	_root["diam_"+i+"_"+j]._y = j*25;
    	i = (i == maskWidth) ? 0 : i+1;
    	j = (i == 0) ? j+1 : j;
    	if (j==maskHeight) { clearInterval(buildIt) };
    }
    buildIt = setInterval(buildObj,"interval",100);
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  5. #5
    ( x x ) ( x x ) ( x x ) xxblindchildxx's Avatar
    Join Date
    Aug 2003
    Location
    London
    Posts
    282

    its not working.....NO WAIT!...yes it is.

    im sorry to keep bothering you,
    ur script still doesnt seem to be working for me, i added the script to the movie and i cant figure out what im doing wrong.
    i traced the x and y values of the duplicated mcs, and theyre fine, but for some reason..........NO WAIT I KNOW!!
    WOOHOO!
    lol.
    noticed you you were positioning the duplicatedmc's with a "_root" in front. and then i thought to myself, dont they have to be part of the mask mc to be a mask?
    i changed ur script alittle:
    code:

    // set init vars //
    var maskHeight = 5;
    var maskWidth = 3;
    i = 0;
    j = 0;
    buildObj = new Object();
    buildObj.interval = function() {
    duplicateMovieClip("diam", "diam_"+i+"_"+j, i*100+j*2);
    setProperty("diam_"+i+"_"+j, _x, (j%2) ? (i*50+25) : (i*50));
    setProperty("diam_"+i+"_"+j, _y, j*25);
    i = (i == maskWidth) ? 0 : i+1;
    j = (i == 0) ? j+1 : j;
    if (j == maskHeight) {
    clearInterval(buildIt);
    }
    };
    buildIt = setInterval(buildObj, "interval", 100);


    its working now, looks pretty cool
    you are a genius! ur method for creating the grid is a lot more compact, which i like :-D and u introduced me to "?:" which will be handy in the future.
    thanx again :-D
    Ross
    ps. are masks like this heavy on the cpu?
    Ross McDowell | WannabeDesigner |
    T: 020 7916 8269 | E: blindchild17@hotmail.com

  6. #6
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    here is your example reworked to work with the setInterval, and instead of duplicateMovie, I use attachMovie. I also deleted mask1 in preference of creating a blank movie clip through actionscript.

    Code:
    _root.createEmptyMovieClip("mask1", 1);
    mask1._x = 51;
    mask1._y = 73;
    box.setMask(mask1);
    
    // set init vars //
    var maskHeight = 5;
    var maskWidth = 3;
    var i = 0;
    var j = 0;
    
    buildObj = new Object();
    buildObj.interval = function() {
    	mask1.attachMovie("diam", "diam_"+i+"_"+j, i*100+j*2);
    	mask1["diam_"+i+"_"+j]._x = (j%2) ? i*50 : (i*50)+25;
    	mask1["diam_"+i+"_"+j]._y = j*25;
    	i = (i == maskWidth) ? 0 : i+1;
    	j = (i == 0) ? j+1 : j;
    	if (j == maskHeight) {
    		clearInterval(buildIt);
    	}
    };
    buildIt = setInterval(buildObj, "interval", 180);

    Enjoy
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  7. #7
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    glad to be of some assistance

    *ps i don't know how cpu intensive it is, you should be ok with so few objects in the mask. I don't think it would be more intensive than fading the image in with a tween.
    Last edited by Lexicon; 07-12-2004 at 08:52 AM.
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  8. #8
    Member
    Join Date
    Jan 2003
    Posts
    97
    Hi Lexicon,

    Just used your code to do my own little masking transition for a picture.

    How can I use the same technique, so that the masking transitions between 3 other pictures, using the same masking?

    Thnaks....
    Thanx and God Bless

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