News for hackers. Stuff that matters.
Re:Making Sprite-based Games with Canvas by angersock on Tuesday March 19, 2013 @06:18PM (#5403993)
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).

There are no comments on this comment yet.