Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to get an entity (a bullet, a grenade, and an explosive) from my player. Specifically, I want the shootingRate of my bullet (how frequently it can be fired).

How can I do this without having to call getEntityByType each time I fire this projectile? There has got to be a cleaner way from what I'm doing right now, which is:

// Shooting
var isShooting = ig.input.state('shoot');

if (isShooting && this.lastShootTimer.delta() > 0) {

switch (this.activeWeapon)
{
    case("EntityBullet"):
        ig.game.spawnEntity(this.activeWeapon, this.pos.x, this.pos.y - 10);
        var equipedWeap =  ig.game.getEntitiesByType(EntityBullet);
        this.lastShootTimer.set(equippedWeap.shootingRate);
        console.log(equipedWeap.shootingRate);
        break;
    case("EntityGrenade"):
        ig.game.spawnEntity(this.activeWeapon, this.pos.x, this.pos.y +5);
      var  equipedWeap =  ig.game.getEntitiesByType(EntityGrenade);
        this.lastShootTimer.set(equipedWeap.shootingRate);
        console.log(EquipedWeap.shootingRate);
        break;
    case("EntityExplosiveBomb"):
        ig.game.spawnEntity(this.activeWeapon, this.pos.x, this.pos.y +5 );
     var   equipedWeap =  ig.game.getEntitiesByType(EntityExplosiveBomb)[0];
        this.lastShootTimer.set(equipedWeap.shootingRate);
        console.log(equipedWeap.shootingRate);
        break;
   }
}
share|improve this question
add comment

2 Answers 2

Just have a look at the documentation on spawnEntity : it returns the newly created entity.

// Shooting
var isShooting = ig.input.state('shoot');

if (isShooting && this.lastShootTimer.delta() > 0) {
   var spawnWeap = ig.game.spawnEntity(this.activeWeapon, 
                                         this.pos.x, this.pos.y , 
                                             { gunnerflip : this.currentAnim.flip, 
                                                gunnerSize : this.size });
   this.lastShootTimer.set(spawnWeap.shootingRate);
 }

Rq 1 : You'll notice i didn't handle here the (x,y) shift linked to each weapon : i think you should handle it in the init() of each weapon, that's why i sent the flip and the size of the weapon user in the settings.

Rq 2 : you need to make a distinction between a weapon and its 'bullets'.

Rq 3 : Things might be simpler if you have, for each weapon, a canShoot function, that handles both the fire rate and the bullets remaining, and maybe some other conditions (overheat, ...). If you also implement a shoot() function that takes the shooting entity as an argument, your code becomes :

// Shooting
var isShooting = ig.input.state('shoot');

if (isShooting && this.activeWeapon.canShoot()) {
    this.activeWeapon.shoot(this);
 }
share|improve this answer
add comment

Just create a constant at the top level of your scope:

// constants
var PLAYER_SHOOTING_RATE = 2;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.