This forum is read only and just serves as an archive. If you have any questions, please post them on

1 decade ago by ansimuz


Is there an elegant to limit the number of bullets allowed to shoot. For example allow to spawn only 3 bullets entities at a time?


Right now i am using this conditional to limit the number of shots to 3:

if('EntityArrow').length < 3){;, this.pos.x, this.pos.y,{flip:this.flip});

1 decade ago by city41

That's not a bad way to do it. You'll have to figure out how many arrows are still alive at some point, so I think the call to getEntitiesByType is unavoidable.

Another way is when an arrow dies you could have it fire an event, and keep track of how many live arrows there are by the number that have been shot versus the number that are still alive.

1 decade ago by bitmapshades

I use a firing mode so the player doesn't use up all their ammo when holding down the fire button, a timer() function in the bullet entity and a property bullets keeping track of the number of bullets fired in the unit() function. If it's less than max bullets then apply direction and velocity otherwise use kill() to remove the bullet entity.

1 decade ago by vincentpiel

First, to reply to your question : you know when your player shoot, so use a counter, test against 4 this counter to decide to shoot or not, and increase this counter if you shoot. Then, in your Bullet constructor, provide the player object (stored in a bulletOwner property or like), and when the bullet dies, have the bullet decrease the counter. This might be done by overloading kill. kill would decrease the counter then call its parent.
But if i may ask, why limit this number ? This is not 'natural'. The best way to limit the number of bullets you shoot is to control the rate of the shooting. So create a ig.Timer in the hero constructor, allow shooting only if this timer is off, and set it to a given value when you shoot. This value will depends on the gun (Bow vs AK47 :-) ). Every 30 bullets or so, set the timer to a much longer period, a make a special noise : the gun is reloading.

1 decade ago by bitmapshades

@vincentpiel you're right I didn't mention that when using kill on a bullet entity it's important to decrease the player's bullet count when using the bullet kill() function as well as to set a firing rate for the active weapon. My preference is to check if ammo is > 0 before handling the other firing conditions.

1 decade ago by ansimuz

Thanks for all you replies.

All of them work perfectly. The reason i want to limit the number of fires is because i want to avoid the player to be shooting like he has a machinegun, i want him to shoot as he were megaman or something like that.

1 decade ago by vincentpiel

i wouldn't use to store those informations, but anyway this code is closer
to your goal :

  if   ( ( < 3) && 
                                 (>0) &&
                                           (>0) {;;
     var arrowPosx= this.pos.x + (this.flip) ? this.size.x : 0;,arrowPosx, this.pos.y,{flip:this.flip});

in the constructor you define shootTimer as a new timer and set the reloadPeriod value (around 1.5s for instance).
This value might decrease with player level (skills++).
Increase when players find arrows within a level.
Rq : In fact, as i said earlier, if it was for me i wouldn't limit shotArrows ( < 3), and only limit by timer.

And the kill method of an arrow becomes :
   kill : function() {;

Rq : as it is designed now, it is hard to handle several weapons. You should really send more informations to the arrow constructor.
Page 1 of 1
« first « previous next › last »