Impact

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

1 decade ago by Dhoombaby

Hello,

In my game called dhoombaby, i have cops and player. I want logic for :
when player comes nearby to cop, cops should fire bullets on him....can anyone help me???

1 decade ago by jerev

An easy way would be to have a reference to the player, be it in your game main object, or somewhere else.

For example in player.js

init: function( x, y, options ) {
     //.....
     ig.game.player = this;
     //.....
}

Now in the update method of your cops, you can do the following:

update: function(  ) {
     //.....
     if ( this.distanceTo( ig.game.player ) < DISTANCE ) {
        // shooting animation active.
        this.shootAt( ig.game.player );
     } else {
        // shooting animation inactive.
     }
     //.....
},

shootAt: function( other ) {
    // particles flying in the direction of the player
    // Can use this: this.angleTo(other)
   ig.game.spawnEntity( 'EntityBullet', this.pos.x, this.pos.y, { // Do offset these positions depending on cop gun location
      angle: this.angleTo( other )
   } );
}

Particles(bullets) can have their check type set to EntityPlayer, and on check (overlap), you can hurt the player.


Bullet:
speed: 200,
maxVel: { x: 200, y:200 },

init: function( x, y, options ) {
    this.parent( x, y, options );

   //this.angle gets set through the options we set above. You could also use options.angle, which is the same thing.
   //this.parent( ); will merge all "options" with "this".

    this.vel.x = Math.cos( this.angle ) * this.speed;
    this.vel.y = Math.sin( this.angle ) * this.speed;
},

checkAgainst: ig.Entity.TYPE.A,  //or whatever type your player is

check: function( other ) {
      if( other === ig.game.player ) {
          other.shotBy( this );
      }
}

Player:
health: 10,
type: ig.Entity.TYPE.A,  //or whatever type

shotBy: function( bullet ) {
    bullet.kill( );

    if ( --this.health <= 0 ) {
      // player died
    }
}

1 decade ago by Dhoombaby

Thanks for reply but in my game
this.vel.x = Math.cos( this.angle ) * this.speed;
this.vel.y = Math.sin( this.angle ) * this.speed;

is not working...its also not showing any errors but the bullets are not fired...what could be the problem??
Page 1 of 1
« first « previous next › last »