Game Development Reference
In-Depth Information
Depending on the framework, you can reduce the y value by this speed value. This will give the
illusion of a slide or flick upward with friction. The object will slide quickly at first, and then slow
down till it comes to a complete halt.
Note that this example uses a repeat loop. If you are working with display objects, you need to
have a nonblocking mechanism that allows for updates to be made. In other words, you need to
use something like the draw function, which is called every so often, or the enterFrame events, so
that the new position is set at each frame, and you can see the transition of the object. If you want
the transition to be super-smooth, try a maximum value of 0.99. This can be used to simulate flicks,
sliding an object across the screen with inertia, and so on.
Simulating a Jack-in-the-Box
When you open a jack-in-the-box, the spring-loaded “Jack” springs out with full force, and then
bobs more slowly over time, until it stops. We can simulate a jack-in-the-box with the help of a little
bit of trigonometry.
Note Since we aren't using a GUI-based framework, the code here is generic and will produce x and
y values that can be applied to display objects in a GUI to simulate the visual effect. For the purpose of
this function, however, we shall display the values to the console.
frequency = 5.0
amplitude = 35
decay = 1.0
time = 0
function jackInTheBox(time)
y = amplitude * math.cos(frequency * time * 2 * math.pi) / math.exp(decay * time)
return y
end
To use this function, we need to call the function and pass it a time value. If you were to use this in
a loop, you would increment the time variable in increments (e.g., of 0.01, or another value that suits
the delay of your calling function).
Using a Sine Scroller
The preceding function makes use of a cosine wave. Similarly, you can vary the parameters and
even use sine waves. These were most commonly used in the 1980s on sine scrollers and to
animate waves of attacking aliens (e.g., in games like R-Type, Zynaps, and Crosswise). In a similar
fashion, the following function shall calculate the x and y values, which can be used to set the x- and
y-coordinates of a display object to simulate the visual effect.
 
 
Search WWH ::




Custom Search