[FMX] Easy SetInterval example

Hi,

I try to improve my AS skills, and i’m looking for a simple example of the set interval event. Where ever I look I see the most complicated Tuts and examples with stocks etc. Is there somebody who can help me with just a basic simple example. This is what I want to get familiar with the whole thing.

I have one button (btn_test) and one square(mc_square) on the stage. mc_square has the following AS attached:


onClipEvent (load) 
{
speed  = 3;
alpha =  0;
}
onClipEvent (enterFrame) 
{
a = alpha - _alpha;
_alpha += a/speed
}

What I want is, that when I click the button, the square is going from alpha 0 to alpha 100, but there has to be a delay of (lets say) 3 seconds before this happens. Please help!!!

Have you tried the tutorial about setInnterval by senocular?

http://www.kirupa.com/developer/actionscript/setinterval.htm

Okay, here goes. I’m not sure how advanced you are with AS, so it might sound a bit childish. If it does, I’m sorry.
Put the following code on the first frame of the main timeline:

startFade = 0;

function waitforabit(){

startFade++;

if (startFade == 2){
_root.mc_square.play();
}

else if (startFade > 2){
clearInterval(_root.waitforit);
}

}

Now give btn_test the following action (just select it and paste this in the as window):

on(release){

_root.waitforit = setInterval(_root.waitforabit, 1500);

}

With this code, when you press the button, the function waitforabit() is set to be called every 1500 milliseconds, or 1.5 seconds. The function adds 1 to the value of startFade each time and waits for it to turn to a value of 2 to start the fade of the square. When startFade has turned bigger than 2 (the next time the function is called) it clears the interval and the function is no longer called.

Now for the final touch: the easiest way to achieve the fade is with animation. Just make a fade starting from frame 1 (I’m assuming you know how to do this) and make sure to give this frame a stop(); action. Now you’re all set.

I’ve included a flash file which is exacly what I just explained.
I hope this helps. Good luck and just holler if you need more help or if anything is unclear.

Hi Gelfling,

This is exactly where I was looking for. This is great. Your explination is very clear! Some of these tutorial writers think that there are only Einsteines :m: visiting sites like this. When you ask them for a simple example the don’t want to or don’t know how to give it! In all the examples they give they use the trace function. Impossible for starters to understand. :-/ I’m not a stuppid guy but I try to learn but it seems that none of the big guys understand that there are also people who just start and right now a simple example is much clearer then two pages of examples I don’t understand!

So the only thing I can say is bravo. You have to upgrade yourself from Junior Member to Whatever there is:)

dboers, I must disagree with you when it comes to the tutorials here on kirupa.com. They are often very well written and comes with good explinations. And the writers are often (always?) moderators of the forum, which means that they are (not required to, but still are!) very helpful in the forums - both with small, simple questions and big, complex ones.

Hi Kalliban,

I don’t say there not helpful. What I say is that most of the Tutorials, here and everywhere, are not ment for people with none or a little knowledge of AS. When I ask for a simple example everybody directs me to all kinds of tutorial I don’t understand. Whilei’m asking for a simple example. I am sure the tuts here and on other sides are food, but in case of this set Interval case. When I read a Tutorial how to set up a setInterval, I also expect also a part about how to contol the thing. remember, Not everybody is busy with Flash for years.

*Originally posted by dboers *
…Some of these tutorial writers think that there are only Einsteines :m: visiting sites like this. When you ask them for a simple example the don’t want to or don’t know how to give it! In all the examples they give they use the trace function. Impossible for starters to understand. :-/ I’m not a stuppid guy but I try to learn but it seems that none of the big guys understand that there are also people who just start and right now a simple example is much clearer then two pages of examples I don’t understand!
I completely disagree with you and its terrible that you can’t appreciate the help that was given. If you had searched the forums, you would have found thousands of examples.
I myself posted a simple code about setInterval a couple of days ago.

I’m sorry that you diagree, but wat I don’t understant is that when you placed a simple example yourself a couple of days ago, you still directed me to a Tutorial of senocular. For whom I have great respect by the way.

And it is not true that I don’t appreciate any help as you can see in my thanks to Gelfling

P.s. Look at the answer Gelfling gave me together with the zip. This was exactly where I asked fore. Nothing more, nothing less.

*Originally posted by dboers *
In all the examples they give they use the trace function. Impossible for starters to understand. :-/

Trace() is your friend. learn trace, love trace.

trace simply takes the variable that is passed to it and prints it to the output screen. It doesn’t effect the look of your movie, and is a great way to make sure all your variables are doing what they should be doing. If you are having problems with a file, after you have double checked your code, the first thing you should do is add trace statements.
If you haven’t gotten in the habit of using it yet, or don’t see a need for it, that’s fine, but when you start making flash files that are more involved and heavy on the coding, you will probably find a need for it when something goes wrong.

*Originally posted by dboers *
**I’m sorry that you diagree, but wat I don’t understant is that when you placed a simple example yourself a couple of days ago, you still directed me to a Tutorial of senocular. **
Its not my job to spend my time searching the forums for you.
You clearly stated that you are trying to improve your AS skills, that was the reason i pointed you to an in depth explanation.
Enough said.

One think I’ve noticed with this forum as opposed to others, is that when you ask a question, you will usually get a reply very fast. The code you get in the reply is usually the most current method of achieving the goal. And the code is written, clean, consise, and usually trimmed of all the fat, sometimes leaning to a prototype/OOP style.
If that’s not what you want, or you don’t understand that stuff yet, that’s cool, just ask again, guide us a little. But understand that they/us are not giving this code to confuse you. They are giving it to you because it is probably the best way to do it, and if you take a second and look at the actual code you are bound to learn a thing or two.
Don’t fret about it too much, this is probably the nicest, most helpful, and fast paced flash forums on the net. If you stick around long enough you will learn much.