AddThis Social Bookmark Button
UpdateAfterEvent with setInterval

updateAfterEvent can be called in conjunction with the setInterval method, let's see what the Adobe documentation tells us about setInterval first.

setInterval function
Calls a function or a method of an object at periodic intervals while a SWF file plays. You can use setInterval() to execute any function repetitively over time.

Let's put it into practise, have a look at the 2 animations below :

As you can see the animation on the right is a lot smoother. You can compare both code and you will see again that the only difference is that we have added updateAfterEvent in the second movie.

// ---------------- code movie 1 ---------------

// The function moveSphere is going to be called every 10ms

setInterval(moveSphere, 10);

// this is our main function that will animate the ball
function moveSphere() {
if (greenSphere._x>220) {
sphereDirection = "left";
}
if (greenSphere._x<5) {
sphereDirection = "right";
}
if (sphereDirection == "right") {
greenSphere._x += 2;
} else {
greenSphere._x -= 2;
}
}

 

// ---------------- code movie 2 ---------------

// The function moveSphere is going to be called every 10ms

setInterval(moveSphere, 10);

// this is our main function that will animate the ball
function moveSphere() {
if (greenSphere._x>220) {
sphereDirection = "left";
}
if (greenSphere._x<5) {
sphereDirection = "right";
}
if (sphereDirection == "right") {
greenSphere._x += 2;
} else {
greenSphere._x -= 2;
}
// here we refresh the screen each time the function is called, in our case every 10ms
updateAfterEvent();
}


Tips regarding the use of setInterval:

- you can clear an interval set by setInterval by using the method clearInterval. In order to do it you need to assign an ID to a variable, for example :
myInterval = setInterval(myFunction,2000);

function myFunction(){
trace(
"I will show that message in 2 seconds and just once because after me the interval is cleared");
clearInterval(myInterval);
}

- you can pass parameters to the function you call by using the following syntax :

myInterval = setInterval(myFunction,2000,"Tom",32);

function myFunction(myName,myAge){
trace(
"my name is "+myName+" and I am"+myAge);
clearInterval(myInterval);
}

// Displays "my name is Tome and I am 32"


I hope this little tutorial will help to understand a bit more why and when to use updateAfterEvent.

Good luck :)



AddThis Social Bookmark Button
If you think this page is providing useful information, don't hesitate to leave a comment.
flashvalley
 
Copyright ©2006-2008 flashvalley.com - All rights reserved