3 ms·
Minor nitpick. The bullet update code is a bit wordy: bullets.push({ pos: [x, y], dir: 'forward', sprite: new
by angersock 14y ago
Minor nitpick. The bullet update code is a bit wordy:
bullets.push({ pos: [x, y],
dir: 'forward',
sprite: new
Sprite('img/sprites.png', [0, 39], [18, 8]) });
and
var bullet = bullets[i];
switch(bullet.dir) {
case 'up': bullet.pos[1] -= bulletSpeed * dt; break;
case 'down': bullet.pos[1] += bulletSpeed * dt;
break;
default:
bullet.pos[0] += bulletSpeed * dt;
}
You could consider simply adding a velocity to the bullet at spawn time like so:
bullets.push({ pos: [x, y],
dir: [0,10],
sprite: new
Sprite('img/sprites.png', [0, 39], [18, 8]) });
and then in the update code replace the switch stuff with:
bullet.pos[0] += bullet.dir[0] * dt;
bullet.pos[1] += bullet.dir[1] * dt;
That should eliminate branching and make the interpreter much happier.
Additionally, by making the speed/velocity a member of the bullet instance, you can do things like have bullets that slow down or speed up if they have a think method or something, or that do homing (for this, look up dot-products and cross-products for steering).