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 ) {
this.shootAt( ig.game.player );
} else {
}
},
shootAt: function( other ) {
ig.game.spawnEntity( 'EntityBullet', this.pos.x, this.pos.y, {
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.vel.x = Math.cos( this.angle ) * this.speed;
this.vel.y = Math.sin( this.angle ) * this.speed;
},
checkAgainst: ig.Entity.TYPE.A,
check: function( other ) {
if( other === ig.game.player ) {
other.shotBy( this );
}
}
Player:
health: 10,
type: ig.Entity.TYPE.A,
shotBy: function( bullet ) {
bullet.kill( );
if ( --this.health <= 0 ) {
}
}
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 »