Previous: Web programming 8 Simple Animation.
Before we move on with the animation, we need to take a detour and learn about functions first.
You may know them from math: functions calculate stuff, like
x is the variable unknown and
f is the function. In math, all functions return something - in programming, they don't have to... r
So here's our first function - just animate the ball:
What we did was to create a function
move which takes a drawing
cand animates it to move to the left. Then we create a ball and call
move(ball) to move it.
So functions are just sections of program that take some objects and do stuff with them. Great! We can create functions out of nowhere and then call them to do their stuff.
We can even have functions in functions - this next example, when you run it, will popup a small warning that the ball has moved and you'll have to click "Ok" to continue:
Now, look at this one more carefully. We have a function
done inside the function
move, which will print an alert that the ball finished moving. But also, we pass this to the animation. What happens is that Raphael will now call this function when it is done moving the ball.
I think you now see how this is useful - let's move the ball back in the next lesson: Web programming 10 Reactive Animations.