Example object pooling code written for the Script-8
function fireProjectiles(input,player, projectiles) { | |
if(input.startPressed) { | |
const availableProjectiles = projectiles.filter( projectile => projectile.visible === false); | |
const nextProjectile = (availableProjectiles !== null && availableProjectiles.length > 0) ? availableProjectiles[0] : null | |
if(nextProjectile !== null) { | |
nextProjectile.visible = true | |
nextProjectile.x = player.x + 3 | |
nextProjectile.y = player.y | |
nextProjectile.dx = PROJECTILESPEED | |
} | |
} | |
} | |
function updateProjectiles(projectiles) { | |
projectiles.forEach(projectile => { | |
projectile.x += projectile.dx | |
}) | |
} | |
function poolPlayerProjectiles(projectiles) { | |
projectiles.forEach(projectile => { | |
if(projectile.x > 128) { | |
projectile.visible = false | |
projectile.dx = 0 | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment