Gustavgb Posted November 7, 2013 Share Posted November 7, 2013 Hey guys,I need to know how I automatically spawn multiple objects, example in a shooting game, where the weapon should fire every time the user clicks. Quote Link to comment Share on other sites More sharing options...
Fricken Hamster Posted November 7, 2013 Share Posted November 7, 2013 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. Quote Link to comment Share on other sites More sharing options...
Gustavgb Posted November 7, 2013 Author Share Posted November 7, 2013 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? Quote Link to comment Share on other sites More sharing options...
alex_h Posted November 8, 2013 Share Posted November 8, 2013 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. Quote Link to comment Share on other sites More sharing options...
ooflorent Posted November 8, 2013 Share Posted November 8, 2013 // 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.