Game Development Reference
In-Depth Information
m_dx *= -1;
// Check the bounds for y
if ( m_sprite.y < 0 || m_sprite.y > HEIGHT )
m_dy *= -1;
// affect the change to the sprite
m_sprite.x += m_dx;
m_sprite.y += m_dy;
}
}
}
What you just saw was the listing for the entire program that makes a red ball
bounce off the walls, continuously.
You will notice that we define the HEIGHT and WIDTH simply to avoid magic
numbers all over the code. We also define the sprite as a class member or property
so that we can access it from different methods of the class and the timer object itself.
We also define the dx and dy, which determine the direction and speed of the ball. In
this example, we keep the speed constant to 1 and simply reverse the direction when
the ball reaches any of the borders in the moveSprite function.
In the constructor, we create a new timer, whose constructor takes two parameters
first being the interval in milliseconds and the second being the number of times to
fire the callback. Setting it to zero will cause the timer to fire infinite number of
times until the program is terminated.
In this example, the interval is defined to be 10, which means after every 10
milliseconds the callback will be fired for us to move the ball. You may experiment
with this interval to see how fast or slow the timer callbacks are fired.
Notice that the callback is set up to call the onTimer method that you define as a
member function of the class. The callback in this example simply calls the
moveSprite function.
The timer also supports a stop function and many other useful methods that the
reader is encouraged to fully explore and experiment.
Trace
Although Flash Builder provides powerful debugging controls, it is often useful to
print something to the console. Flash provides a simple method called trace that will
help you do just that.
 
Search WWH ::




Custom Search