Recommended Posts

Make an array to keep track of bullets.

When you create a bullet, add the bullet to the array.

Update each bullet in the array each step.

When the bullet dies, remove it from the array.

I have thought about that, but I have no idea how the code should look...


Anyone who has an example? 

Share this post

Link to post
Share on other sites

It would be a good idea to use an object pool for your bullets too, this ought to really help improve performance in your game. Try searching google for 'Bullet Object Pool' or something like that.

Share this post

Link to post
Share on other sites
// Game structures// ---------------var Bullet = {  damage: 1,  speed: 10  pos: {    x,    y  },  velocity: {    x: 0,    y: 0  },  setAngularSpeed: function(angle) {    // Compute velocity    // ...  }};var Player = {  pos: {    x: 0,    y: 0  },  velocity: {    x: 0,    y: 0  },  shoot: function(pos) {    var bullet = Object.create(Bullet);    bullet.setAngularSpeed(getAngle(this.pos, pos));    return bullet;  }};// Main// ----var player = Object.create(Player);var bullets = [];// Spawn a bulletbullets.push(player.shoot({x: 50, y: 20}));// Update all bulletsbullets.forEach(function(bullet) {  bullet.pos.x += bullet.velocity.x;  bullet.pos.y += bullet.velocity.y;  // Handle collisions  // ...});

You can try something like this.

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.