[MX] setInterval or getTimer?

I`m building a photo slide projector that calls a function using setInterval. But after the tenth call (about after 50 sec) the flash player stops working and crashes.
On the first frame of the main timeline I set the following code:

// comando para fullscreen
fscommand("fullscreen", "true");
fscommand("allowscale", "false");

// funcao que attacha mc_loadimg e carrega img
function loadimg () {
	foto1 = Math.floor(random(48));
	foto2 = Math.floor(random(48));
	foto3 = Math.floor(random(48));
	foto4 = Math.floor(random(48));
	loadMovie(foto4 + ".jpg", "_root.foto1");
	loadMovie(foto2 + ".jpg", "_root.foto2");
	loadMovie(foto2 + ".jpg", "_root.foto3");
	loadMovie(foto4 + ".jpg", "_root.foto4");
	setInterval(_root.loadimg, 5000);
	updateAfterEvent();
}

On a movieclip on the stage:

onClipEvent(load) {
	_root.loadimg ();
}

[color=blue]Would it be better if I use a getTimer and if statements?[/color]
Which one is more apropriate for this ocasion?

thanks

I like setInterval better than getTimer(), getTimer() is a pain in my … beeep…lol. Well that depends on the situation, in this case I think setInterval is better. I can’t see why your coding freezes, but try this anyway…

Remove the onClipEvent(load) from your movie clip on the stage and have this on the frame…

[AS]// comando para fullscreen
fscommand(“fullscreen”, “true”);
fscommand(“allowscale”, “false”);

// funcao que attacha mc_loadimg e carrega img
function loadimg () {
f2 = Math.floor(random(48));
f4 = Math.floor(random(48));
loadMovie(f4 + “.jpg”, “_root.foto1”);
loadMovie(f2 + “.jpg”, “_root.foto2”);
loadMovie(f2 + “.jpg”, “_root.foto3”);
loadMovie(f4 + “.jpg”, “_root.foto4”);
}
//run the function initially
loadimg();
//use setInterval to run it every 5 seconds after that
setInterval(loadimg, 5000);[/AS]

Set interval works every 5 seconds no matter what, you don’t need to reinitiate it every 5 seconds when the function is run, that isn’t very efficient, so what I did instead was moved the setInterval outside of the function you are calling. This of course would make the viewer have to wait 5 seconds to see the function in the first place, so right before initiating the setInterval I called the function to run it when that frame is hit. That way you get the movies loaded right off the bat AND every 5 seconds after that.

As for inside the function itself I removed the updateAfterEvent(), I saw no need for that in this script since you don’t need anything updated after an event, it will do it anyway. I also don’t think it’s wise to have the same variable names as instance names, it is confusing the coder and maybe to flash, so I changed those. And last put not least, I noticed your script you only used the foto2 and foto4 variables, so I removed foto1 and foto3, no need to run them if they aren’t getting used, you know how to put them back if you need them :wink:

i guess it crashes because everytime the function is called, a new interval is defined, so don’t set the interval in the function itself.

i think you should use something like this instead:

onClipEvent (load) {
imgInterval = setInterval(loadimg, 5000);
}

and by the way…

Math.floor(random(48));

returns the same value than just

random(48);

you’d only need to round the number if you use the Math.random method.

Math.floor(Math.random()*48));

=)

edit. oops… i should reload the page before posting. :stuck_out_tongue:
sorry, lost. :-\

Good catch on the random(48) thing kax, I saw Math.floor and my head automatically pictured Math.random() :stuck_out_tongue:

But at least we have the same theory on why it crashes, the setInterval defined in the function.

Thanks for that! :slight_smile:
It`s working better now.
But sometimes no image is loaded. What might be happening?
All the images have their number set from foto1.jpg to foto48.jpg.

I really want to learn how to make this code work in a different way.
Like this: the image is loaded in mc01 and then mc01 is triplicated into those positions and set rotation, alpha, color change. Is it possible to reflect the image as it is possible in photoshop (transform - reflect - horizontal or vertical)?
thanks again! :thumb:

that’s because random(48) returns a number between 0 and 47.
so does Math.floor(Math.random()*48).

random(48)+1;
// or
Math.floor(Math.random()*48)+1;

:wink:

trace random, should give you values from 0 to 47 :wink:

edit. oops… i should reload the page before posting.
sorry, kax :sigh: …same goes for me; lol

well you could flip the image by setting its _xscale/_yscale to -100 (there’s a recent thread about that).

but when you duplicate the movieclip, you must load the .jpg into each movieclip. :-\

edit. :stuck_out_tongue:
it’s ok, eyez. :wink:

But this way image number 01 will never be loaded!!
What about?

 random(49);

it will, 0+1 =1!
random 49 will still return 0 sometimes, do you have a 0.jpg?

Oh sure!
Yes I have a 0.jpg!

Hey Kax.
I didnt find the recent thread u told me about.
Could u post its url here?
thanks

hmmm… i thought there would be more info about it.
there are just a few snippets of code.

http://www.kirupaforum.com/forums/showthread.php?s=&threadid=25877