Timebased Movement The objective of timebased movement is
Time-based Movement • The objective of time-based movement is solely performance. • The basic idea of time-based movement is to move objects at a consistent rate, no matter how well or poor the performance of the environment.
Physics • Physics provides us with a set of kinematic equations that govern an object's motion. • The kinematic displacement equation can be used to describe an object's movement in AS 3. • The displacement of a moving object is directly proportional to both velocity and time.
Example • Move faster, go farther. • Move longer, go farther.
Kinematic Displacement Equation • Calculates how far an object should move based on how much time has passed. d v t a Displacement of the object. Velocity of the object Time the object is in motion. Acceleration of the object.
Zero Acceleration • When acceleration is zero, the equation of motion reverts to a constant velocity equation.
Practice Labs • Lab 4 deals with fixed velocity • Displacement will be directly proportional to time.
AS 3 and Time-based Animation • Time-based animation uses elapsed time to determine how much to update movement of a display object. • Time-based animation measures how much time has passed and calculates how far the object should move based on that time. • In AS 3, time-based movement relies on the elapsed time computed at every ENTER_FRAME event.
Example //TASK 1 : GET THE CURRENT TIME: // MILLISECONDS ELAPSED SINCE THE RUNTIME VIRTUAL MACHINE // FOR AS 3 WAS INITIALIZED. time = get. Timer(); //TASK 2 : REGISTER A LOOPING TRIGER EVENT TO CREATE MOVEMENT add. Event. Listener(Event. ENTER_FRAME, move. Car); • Elapsed time is calculated at every frame. • Each moving element will update a different number of times based on the time since the last frame.
• Before updating the location of a moving object, it must be determined how much time has passed since the last update. public function move. Car(event: Event) { //TASK : GET THE ELAPSED TIME var elapsed. Time: int = get. Timer() - time; //TASK: UPDATE TIME FOR THE NEXT ENTER_FRAME time += elapsed. Time; //TASK : UPDATE THE POSITION OF THE CAR car. x += car. velocity * elapsed. Time / 1000; } //TASK : UPDATE THE ROTATION OF THE CAR WHEELS car. right. Wheel. rotation += car. velocity * elapsed. Time / 1000; car. left. Wheel. rotation += car. velocity * elapsed. Time / 1000;
Final Notes • Elapsed time is calculated at every frame. • Each moving element will update a different number of times based on the time since the last frame. • We will need to keep track of the time passed since the last frame, • Smooth the animations rely on FPS - higher FPS means smoother animation. • The advantage of time-based animation is that animated elements will stay in sync no matter what the frame rate is.
- Slides: 10