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
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
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:
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!
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:
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!
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
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.
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
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