Game Development Reference
In-Depth Information
There are drawbacks to using notNaN in production code, however. First, it doesn't work in IE8 or older.
Second, it makes your code slow. Luckily, you can simply disable it for use in production.
var DEBUG = true;
...
if(DEBUG) {
notNaN(Vec2.prototype, 'x');
notNaN(Vec2.prototype, 'y');
}
Random values
To create varying particle effects, we are going to need random numbers. JavaScript only provides a very
simple way to get random numbers, namely Math.random() , which returns a value between 0 and 1. For
our particle engine, we will need random numbers in the form of 63 +/- 9.
function fuzzy(range, base){
return (base||0) + (Math.random()-0.5)*range*2
}
// a random value from -10 to 10
fuzzy(10);
// a random value of 63 +/- 9.
fuzzy(9, 63);
We will also need to randomly choose an item from a list; for example, a texture. The following is a very
simple function that will help us do just that:
function choice(array) {
return array[Math.floor(Math.random()*array.length)];
}
Components
Our particle system consists of a few different components that work together to create many different
effects. The design focuses on efficiency and simplicity. It makes heavy use of JavaScript features like
first-class functions and closures. We start with a very simple, but flexible, base and add functionality as
needed.
 
Search WWH ::




Custom Search