A Flash Developer Resource Site

Results 1 to 20 of 20

Thread: Math random code not working in Flash CS4?

  1. #1
    Member
    Join Date
    Mar 2004
    Posts
    88

    Math random code not working in Flash CS4?

    Hey guys

    I took an old flash file that I made back when I had Flash 8 and used the same code in a new project for the exact same case but different animation clip on Flash CS4. The old Flash 8 project works but the CS4 one, it's just not working.

    This code is supposed to continually choose a random frame out of 7 - there's an animation/movie clip on each frame to play. Once the movie is done playing it's supposed to again choose another random frame and another and so forth. There's also a timer on it for the delay between each time it will choose and play the next random frame. The code is placed in an actions layer above each frame of the 7 that have the movie clips on them.

    Code:
    function ran() {
    	ran = math.round(random(7)+1);
    	gotoAndStop(ran);
    	clearInterval(startMyMovie);
    }
    startMyMovie = setInterval(ran,10000);
    stop();
    I chose Actionscript 2.0 for the new CS4 document just like in Flash 8 document. But instead of choosing random frame, it loads the first frame and once the first movie clip is done playing, it stops there.

    I also put a
    Code:
    stop();
    command at the end of each movie clip like I did before in the Flash 8 version. Everything seems to be the same when it comes to the coding part but the new one is just not working

    Any ideas?
    Last edited by Jessica M; 12-24-2011 at 08:27 AM.

  2. #2
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    There are a few mistakes with your code:

    Actionscript Code:
    function ran() {
        ran = math.round(random(7)+1);

    the function name is ran, and so is the variable ran. Giving them both the same name can confuse Flash, so things may not go as expected, so change one of the names.

    Second error lies here:

    Actionscript Code:
    ran = math.round(random(7)+1);

    math.round is incorrect, the m should be a capital M - also, random() is old and I think deprecated in CS4, so you should use Math.random()

    Since you're clearing the interval after it is executed once, why not just use setTimeout? It's the same, only that it is executed once and then auto-deletes itself

    Corrected Code:

    Actionscript Code:
    function ran() {
        ranNum = Math.round(Math.random()*6)+1;
        gotoAndStop(ranNum);
    }
    setTimeout(ran, 10000);
    stop();

    Hope this helps
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  3. #3
    Member
    Join Date
    Mar 2004
    Posts
    88
    It works like a charm!!

    Thank you my friend!!

    Just the way I want it!!

    Edit: It keeps working normally but after like a minute or so it just stops? I want it to continue working non-stop

    Edit2: Ok! I tried replacing:
    Code:
    setTimeout(ran, 10000);
    with:
    Code:
    setInterval(ran,10000);
    and see how it goes.

    Edit3: Nope, can't figure it out... It works normally and after a minute or 2 it just stops there

    Edit4: Ok, I tried using the set Interval with 15000 milliseconds instead of 10000. Tested it and so far it works all the way through the time I tested it So from my conclusions, it seems the problem was that I didn't give it enough time for some of the animations to finish so after some point it would mess it up. Gonna also try with the setTimeout part and see if it works the same

    Edit5: Nope, I get it now, the setInterval part keeps updating the function continually at the indicated milliseconds (which is what I want for this case). While the setTimeout and clearInterval part is supposed to interrupt the updating, which means that I don't need them in this case. Right?

    The problem is that apparently, not clearing the interval command seems to be harmful for the computer/browser and does something to make it slow or not responsive after a few minutes.... Which means I'm supposed to do something about it while at the same time allowing the random frames to be loaded and the animations to continue playing. This is getting more complicated than I thought... lol

    Edit6: Alright, this seems to be the best one yet:

    Code:
    clearInterval(startMyMovie);
    function ran() {
    	ranNum = Math.round(Math.random()*3)+1;
    	gotoAndStop(ranNum);
    }
    startMyMovie = setInterval(ran, 20000);
    stop();
    Clear the interval upon entering the frame, (which means the counting thingy, if it was active, it will stop and it won't be pilling up and messing around things - cause the previous code, after a couple of minutes got me an error that the script was slowing down my browser and computer). Then do the Math Random function to choose the random frame again. And finally set a new interval upon 20000 milliseconds (or 20 seconds), to count and then load the next random frame which will do the exact same thing again and again!

    Well I let it run for a few minutes now and so far, it keeps working fine and no errors whatsoever!
    Last edited by Jessica M; 12-24-2011 at 10:51 PM.

  4. #4
    Member
    Join Date
    Mar 2004
    Posts
    88
    Nig, looks like I'm gonna need help again

    I finished my website and everything is working correctly except that now I added a Preloader and it seems to be having trouble with my Math random script.

    From what I understood, when the Math random script loads the next random frame, it reloads everything from the beginning and the Preloader shows up every few seconds to load the site from the beginning again.

    Here's the script from the Preloader: on Frame1 In Layer called Actions:
    Code:
    totalBytes = Math.round(getBytesTotal()/1024);
    loadedBytes = Math.round(getBytesLoaded()/1024);
    percentDone = Math.round((loadedBytes/totalBytes)*100);
    if (_root._framesloaded>=_root._totalframes) {
    	gotoAndPlay("start");
    }
    On Frame2 in the same layer:
    Code:
    gotoAndPlay(1);
    In Labels Layer on Frame3 it has the label "start" (mentioned above in frame1)

    After that frame with start label, in another layer I put the frame that has my main site.

    Here's the final code for my Math Random Wizard (4 frames for 4 animations):

    Code:
    clearInterval(startMyMovie);
    function ran() {
    	ranNum = Math.round(Math.random()*3)+1;
    	gotoAndStop(ranNum);
    }
    startMyMovie = setInterval(ran, 15000);
    stop();
    Last edited by Jessica M; 01-07-2012 at 07:53 PM.

  5. #5
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    I think it's because you're using setInterval, and not deleting it. You see, after it goes to that random frame, the interval doesn't stop, it's kept executing after the specified amount, so every 15th second, it will go to a random frame, regardless of which frame you are on. To solve this, simply delete the setInterval from inside the function, or as I stated earlier, use setTimeout, which executes the function only once and deletes itself!

    SOLUTION ONE:

    Actionscript Code:
    clearInterval(startMyMovie);
    function ran() {
        ranNum = Math.round(Math.random()*3)+1;
        gotoAndStop(ranNum);
        clearInterval(startMyMovie);
    }
    startMyMovie = setInterval(ran, 15000);
    stop();

    SOLUTION TWO:

    Actionscript Code:
    clearInterval(startMyMovie);
    function ran() {
        ranNum = Math.round(Math.random()*3)+1;
        gotoAndStop(ranNum);
    }
    startMyMovie = setTimeout(ran, 15000);
    stop();

    Hope it works
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  6. #6
    Member
    Join Date
    Mar 2004
    Posts
    88
    Nope, it's still doing it Tested both codes

    Maybe it's the Preloader. Do you have any spare I can use?

  7. #7
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Could you post the FLA file?
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  8. #8
    Member
    Join Date
    Mar 2004
    Posts
    88
    Yup, here's the Preloader.
    Attached Files Attached Files

  9. #9
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    I can't see any other code than for the loading??!? If the preloader is a seperate file and you're loading it into your website, I strongly suggest you implement the preloader in the website itself!
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  10. #10
    Member
    Join Date
    Mar 2004
    Posts
    88
    Yup, like I said, the Preloader was placed inside the website. Maybe I should use an external preloader?

  11. #11
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Can you post the FULL FLA file? I can't conclude anything wrong with just the preloader >.<
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  12. #12
    Member
    Join Date
    Mar 2004
    Posts
    88
    If you mean the entire site, sorry I can't. Besides, it's too big in size.

  13. #13
    Member
    Join Date
    Mar 2004
    Posts
    88
    Ok, I tried using another preloader. And I get the same reaction. So apparently the problem is that the Math.random actionscript is trying to transfer the site back to frame 1 to 4, where the preloader is located.

    Is there a way to somehow change the Math.random script and have it look for frame labels instead of frame numbers? That way, it won't be effecting the whole site and it will just use the labels inside the Wizard movie clip like it should.

  14. #14
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Where do you have the Math.random code? Inside the movieclip you are targeting, or outside it? If outside, then you should give your movieclip an instance name and target that as well!

    Also, can you at least post the link to your website?
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  15. #15
    Member
    Join Date
    Mar 2004
    Posts
    88
    I will post the link when it's finished

    I'm trying to figure out how to use tell Target with this code, but I can't seem to manage it. How do I do it please?

    Code:
    tellTarget ("_root.MainSite.Leather_Page.Wizard") {
    function ran() {
    	ranNum = Math.round(Math.random()*3)+1;
    	gotoAndStop(ranNum);
    		clearInterval(startMyMovie);
    		startMyMovie = setInterval(ran, 15000);
    }
    }
    stop();

  16. #16
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    tellTarget? Now that is old, AS1, and I think deprecated :P

    I think that's where the problem lies. Either you can use with, tellTarget's replacement or dot notation. Using with won't fix the situation, since you are using tellTarget OUTSIDE a function, which I think, will have no effect on the function itself. If you want to use it, you'll have to use it INSIDE the function, 'cause it's the function that's going to be executed after 15 seconds, but if the tellTarget code is OUTSIDE the function, it won't be a part of it, and then target the current Timeline instead, which I presume is the Main Stage (_root), and that's why it's going to a Random frame on your actual timeline, rather than that of a Movieclip!

    METHOD 1:

    Actionscript Code:
    function ran() {
        with(_root.MainSite.Leather_Page.Wizard) {
            ranNum = Math.round(Math.random()*3)+1;
            gotoAndStop(ranNum);
        }
        clearInterval(startMyMovie);
    }

    startMyMovie = setInterval(ran, 15000);
    stop();

    METHOD 2:

    Actionscript Code:
    function ran() {
        ranNum = Math.round(Math.random()*3)+1;
        _root.MainSite.Leather_Page.Wizard.gotoAndStop(ranNum);
        clearInterval(startMyMovie);
    }

    startMyMovie = setInterval(ran, 15000);
    stop();

    Hope this works ^^
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  17. #17
    Member
    Join Date
    Mar 2004
    Posts
    88
    Tried Method 2. Problem solved, Nig!

    Thank you very much!

    Gonna post the link to the site once I upload it

    I included Flashkit and your name in my site credits even before the problem with the preloader showed up

  18. #18
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Oh, great
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  19. #19
    Member
    Join Date
    Mar 2004
    Posts
    88
    Here it is!

    http://www.questformoreglory.com/

    Uploaded a few minutes ago!

  20. #20
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    WOW, now that is pretty cool. Did you make those 3D characters yourself? If so, then AMAZING O_O
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

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