Flash Momentum Tutorialby: Matt CarpenterstartDrag Woes As you probably know, Flash comes with a great predefined function startDrag. This makes dragging things around the screen very easy. However, once you call the function stopDrag, the object stops dead in its tracks. If you wanted the object to maintain its motion once stopDrag is called, you will need to add some extra code to your objects.
Get the motion The first step in maintaining an object’s motion is figuring out what that motion is and saving it. To do this we simply need to save the objects location from the last frame, and then subtract that from the current frame’s location. This gives us the object’s velocity. As an example of what I mean, I’ve written the following handlers for a ball Sidenote: onClipEvent(load) {
Throw-able Ball Damping and Limiting As you can see, the ball can get moving pretty fast and shows no signs of slowing down ever. Perhaps this is how you want the ball to behave. For most applications, however, you will probably want to limit the object’s speed to within reason. You may also want the object to slow down over time. The rest of this tutorial will address these two features. To limit the speed of the ball, we use a little trig. Don’t be afraid of the math, I’ve already worked it out here for you. Those of you who like math can probably figure out what’s going on pretty easily. Those of you who hate it can just copy the code. // limit the velocity to MAX_SPEED And finally we’ll slow the object down over time. To slow the object down, we simply multiply the velocity by a number less than zero each frame. You could get the same effect by dividing the velocity by a number greater than zero. As you can see in the code, it doesn’t take much to slow the ball down. DAMPING_FACTOR = 0.98; Throw-able Ball with Limiting and Damping The End And there you have it. Your very own throw-able Flash object. To make things easier, I’ve placed all of the code into the © 2008 NetVisits, Inc. All rights reserved. |