| |
|
|
|
|
|
|
|
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.
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.
setInterval(moveSphere, 10);
function moveSphere() {
if (greenSphere._x>220) {
sphereDirection = "left";
}
if (greenSphere._x<5) {
sphereDirection = "right";
}
if (sphereDirection == "right") {
greenSphere._x += 2;
} else {
greenSphere._x -= 2;
}
}
|
setInterval(moveSphere, 10);
function moveSphere() {
if (greenSphere._x>220) {
sphereDirection = "left";
}
if (greenSphere._x<5) {
sphereDirection = "right";
}
if (sphereDirection == "right") {
greenSphere._x += 2;
} else {
greenSphere._x -= 2;
}
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);
}
|
I hope this little tutorial will help to understand a bit more why and when to use updateAfterEvent.
Good luck :)
|
|
|
If you think this page is providing useful information, don't hesitate to leave a comment.
|
|
|
|
|
|
|
|
|
Copyright ©2006-2008 flashvalley.com - All rights reserved |
|