public
Last active

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
// declare bullet pools
var activeBullets = [];
var bulletPool = [];
 
// construct some bullets ready for use
for (var i=0; i < 20; i++)
bulletPool.push( new Bullet() );
 
// a constructor/factory function
function getNewBullet()
{
var b = null;
 
// check to see if there is a spare one
if (bulletPool.length > 0)
b = bulletPool.pop();
else
// none left, construct a new one
b = new Bullet();
 
// move the new bullet to the active array
activeBullets.push(b);
return b;
}
 
function freeBullet(b)
{
// find the active bullet and remove it
// NOTE: Not using indexOf since it wont work in IE8 and below
for (var i=0, l=activeBullets.length; i < l; i++)
if (activeBullets[i] == b)
array.slice(i, 1);
// return the bullet back into the pool
bulletPool.push(b);
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.