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

8 years ago by Wake

Hi there guys! i`m new with impact but i´m doing right now a tiny game for LD 33 (i know, maybe someone will ask me few days after LD but still helps me to learn! haha).

So, i have a Player that spawn a bullet with bunciness pressing "Z". Also, i have another type of bullet spawned by clicking the mouse. My problem is:

The "pressing Z" bullet is a high damage bullet, so i want to limit it to 3 bullets on screen. If there are 3 bullets on screen, the player can´t fire more, even if he press the "Z" button. If you have 2 High damage bullet on screen, you can fire one more, etc.

This is my Player code with my bullets (bullet and Granada). Sorry for the spanish notes, i´m Chilean! if you need more info, just tell me.

PS: I know that my code sucks sometimes, this is my second week with impact. I'm a 3D artist so you know, coding is new for me haha





	EntityPlayer = ig.Entity.extend({

		//Importar animacion y fisicas del personaje (tambien salud y colisiones)
		animSheet: new ig.AnimationSheet ('media/graphics/player.png', 16, 16),
		size: {x:16, y:16},
		offset:{x:2, y:0},
		//_wmScalable: true,
		maxVel: {x:100, y:140},
		friction: {x:600, y:0},
		accelGround: 400,
		accelAir: 300,
		jump: 300,
		gravity: 1,

		type: ig.Entity.TYPE.A,
		checkAgainst: ig.Entity.TYPE.NONE,
		collides: ig.Entity.COLLIDES.PASSIVE,
		health: 100,
		startPosition: null,
		//bounciness: 0.7,

		//Esto es para la inmunidad luego del respawn
		invincible: true,
		invincibleDelay: true,
		invincibleTimer: null,
		weapon: 0, 
		totalWeapons: 2,
		activeWeapon: 'EntityBullet',
		_wmDrawBox: true,
		_wmBoxColor: 'rgba(255, 0, 0, 0.7)',


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

			this.addAnim ('idle', 1, [5] );
			this.addAnim ('run', 0.12, [0,1,2] );
			this.addAnim ('shoot',1, [3] );
			this.addAnim ('jump', 1, [4] );
			this.addAnim ('falling', 1, [6]);
			this.addAnim ('shoting', 1, [3], true);
			this.addAnim( 'pain', 0.3, [6], true );

			// Propiedades mouse

			this.invincibleTimer = new ig.Timer();

			//posicion. Se indica la posicion del personaje.
			this.startPosition = {x:x, y:y};


		update : function(){

			// Esto flipea la animacion
			this.currentAnim.flip.x = this.flip;

			// Moverse de izquierda a derecha
			var accel = this.standing ? this.accelGround : this.accelAir;

				this.accel.x = -accel;
				this.flip = true;
			else if (ig.input.state('right')){
				this.accel.x = accel;
				this.flip = false;
				this.accel.x = 0;

			// Saltar
			if ( this.standing && ig.input.pressed('jump')){
				this.vel.y = -this.jump;

			//Nuevo metodo para ejecutar las animaciones, diferente al if pressed.state.released. Se usa con calculo de velocidad en vez de verificar tecla apretada WAY BETTER!!!

			if(this.vel.y < 0 ){
				this.currentAnim = this.anims.jump;
			else if( this.vel.y > 0) {
				this.currentAnim = this.anims.falling;
			else if( this.vel.x != 0){
				this.currentAnim =;
			else {
				this.currentAnim = this.anims.idle;

			//disparooo! no funciona el flip de la bala eso si.

			if( ig.input.pressed('fire') ) {
 			var entity = ( //esto spawnea la bala y la flipea
 			 EntityGranada, this.pos.x+10, this.pos.y, {flip:this.flip});
			entity.currentAnim.flip.x = this.flip;
			//this.currentAnim = this.anims.shoting;

			if (> this.invincibleDelay){
				this.invincible = false;
				this.currentAnim.alpha =1;
				this.weapon ++;
				if(this.weapon >= this.totalWeapons)
					this.weapon = 0;
				switch (this.weapon){
			this.activeWeapon = 'EntityBullet';
			this.activeWeapon = 'EntityGranada';



			if( ig.input.pressed('click') ) { //Basic shoot command
     var mx = (ig.input.mouse.x +; //Figures out the x coord of the mouse in the entire world
     var my = (ig.input.mouse.y +; //Figures out the y coord of the mouse in the entire world

     var r = Math.atan2(my-this.pos.y, mx-this.pos.x); //Gives angle in radians from player's location to the mouse location, assuming directly right is 0
     Honestly, the above should probably take into account offsets of where your gun is located, 
     but that greatly overcomplicates this snippet since most of you are going to have different exit points for the barrel of your weapons

     Furthermore, each weapon might even have different exit points, so you might want to move the angle calculation into the init method of
     each bullet
     */ EntityBullet, this.pos.x, this.pos.y, {flip:this.flip, angle:r} ); //Nothing to special here, just make sure you pass the angle we calculated in

// Aca termina el MOUSEEEEEEE


		//Esto es para llamar al respawn. El player aparecera en el incio (donde la entidad estaba originalmente)
		kill: function(){
		var x = this.startPosition.x;
		var y = this.startPosition.y;, this.pos.x, this.pos.y,
		{callBack:function(){ EntityPlayer, x, y)}} );
		makeInvincible: function(){
			this.invincible = true;
			offset = {x:2, y:0};

		receiveDamage: function (amount, from){
		if (this.invincible)
		this.parent(amount, from);
		draw: function (){
			if (this.invincible)
				this.currentAnim.alpha = * 1;

	EntityBullet = ig.Entity.extend({
	size: {x:8, y:5},
	offset: {x:5, y:6},
	animSheet: new ig.AnimationSheet ('media/graphics/items.png', 16, 16),
	maxVel: {x:300, y:300},
	type: ig.Entity.TYPE.NONE,
	flip: false,
	checkAgainst: ig.Entity.TYPE.B,
	collides: ig.Entity.COLLIDES.PASSIVE,
		check: function( other ) {
other.receiveDamage( 10, this );

	init: function (x, y, settings) {
		this.parent (x + (settings.flip ? -4 : 8) , y+8, settings );
		this.vel.x = this.accel.x = (settings.flip ? -this.maxVel.x : this.maxVel.x);
		this.addAnim('shoot', 0.1, [11]);


		var vely = Math.sin(this.angle) * this.maxVel.x; //.desiredVel is just the velocity I would want if we were going in a straight line directly out of the right of the player. I just put it as a property of the entity since I refer to it in multiple locations
     var velx =  Math.cos(this.angle) * this.maxVel.x;

     I'm a fan of fullspeed projectiles with no acceleration so we set the velocity, max velocity and for good measure acceleration too.
     You might want to start with a bit of velocity and some sort of acceleration so your projectile starts off slower and speeds up for something like a rocket 
     If that's the case, you'll want to do something like the following
     this.maxVel.x = whatever max you want;
     this.accel.x = Math.sin(this.angle)  * desiredAcceleration;
     this.accel.y = Math.cos(this.angle)  * desiredAcceleration;
     this.vel.x = Math.sin(this.angle)  * desiredStartingVelocity;
     this.vel.y = Math.cos(this.angle)  * desiredStartingVelocity;
     this.maxVel.x = this.vel.x = this.accel.x = velx;
     this.maxVel.y = this.vel.y = this.accel.y = vely;
     //this.activeWeapon = {flip:this.flip});



	handleMovementTrace: function( res ) {
this.parent( res );
if( res.collision.x || res.collision.y ){

check: function( other ) {
other.receiveDamage( 3, this );


EntityGranada = ig.Entity.extend({
	size: {x:8, y:5},
	offset: {x:5, y:5},
	animSheet: new ig.AnimationSheet ('media/graphics/items.png', 16,16),
	type:  ig.Entity.TYPE.NONE,
	checkAgainst: ig.Entity.TYPE.B,
	collides: ig.Entity.COLLIDES.PASSIVE,
	maxVel: {x:130, y:200},
	bounciness: 1,
	gravityFactor: 1,
	gravity: 1,
	angle: 45,
	bounceCounter: 2,

	init: function(x,y, settings){
		this.parent (x + (settings.flip ? -4 : 7), y, settings);
		this.vel.x = (settings.flip ? -this.maxVel.x : this.maxVel.x);
		this.vel.y = -(10 + (Math.random()*100));
		this.addAnim('granada', 0.1, [1,2]);



	handleMovementTrace: function (res){
		this.parent (res);
		if (res.collision.x || res.collision.y){
			//esto es pa que rebote 3 veces! se puede cambiar a gusto la cantidad de rebotes 
			if(this.bounceCounter > 4){

	check: function (other){
		other.receiveDamage (10, this);

EntityDeathExplosion = ig.Entity.extend({
lifetime: 1,
callBack: null,
particles: 10,
init: function( x, y, settings ) {
this.parent( x, y, settings );
for(var i = 0; i < this.particles; i++), x, y, {colorOffset:
settings.colorOffset ? settings.colorOffset : 2});
this.idleTimer = new ig.Timer();
update: function() {
if( > this.lifetime ) {

EntityDeathExplosionParticlep = ig.Entity.extend({
size: {x: 2, y: 2},
maxVel: {x: 160, y: 200},
lifetime: 2,
fadetime: 1,
bounciness: 0,
vel: {x: 100, y: 30},
friction: {x:100, y: 0},
collides: ig.Entity.COLLIDES.LITE,
colorOffset: 0,
totalColors: 9,
animSheet: new ig.AnimationSheet( 'media/particles.png', 2, 2 ),

init: function( x, y, settings ) {
this.parent( x, y, settings );
var frameID = Math.round(Math.random()*this.totalColors) + (this.colorOffset
* (this.totalColors+1));
this.addAnim( 'idle', 0.2, [0,1,2,3,4,5,6,7] );
this.vel.x = (Math.random() * 2 - 1) * this.vel.x;
this.vel.y = (Math.random() * 2 - 1) * this.vel.y;
this.idleTimer = new ig.Timer();


update: function() {
if( > this.lifetime ) {
this.currentAnim.alpha =
this.lifetime - this.fadetime, this.lifetime,
1, 0

8 years ago by Wake

Oh, and also, i take a lot of help from some post here in the forum. There are some code with other users notes. I did´t delete them because help me in the process. If one of you guys recognize your notes or code here, THANK YOU <3!

8 years ago by ijed

Hi Wake, see you Monday...

8 years ago by Dejan

I will just assume that if( ig.input.pressed('click') ) is for shooting with your mouse and if( ig.input.pressed('fire') ) for shooting with z.

So if you would change
 if( ig.input.pressed('fire') ) {

 if( ig.input.pressed('fire')  &&<3) {

your if statement will only run if you are pressing your fire button AND if there are less than 3 instances of the granada entity active.

8 years ago by Wake

Hi Ijed, i think i know who you are! aahah I have a horrible fever now and maybe i see you tomorrow, but maybe not (if i sill fell like ass).


That works! thanks a lot! i´m a total noob so thanks for help me with that kind of questions! This is the game.

I need one thing more that i can´t do, if someone can help me with that i finish the game!

Actually, i need a "game Over" screen when the Entity Treasure health is 0. If Treasure dies, then go to the Game Over Screen. I Try few methods but nothing work. Now, when the treasure dies, game stucks.

Thanks again!

8 years ago by stahlmanDesign

Another way to do this would be to keep track of how many bullets have been shot.

In the player code, each time you spawn a bullet do this: ++;

in EntityBullet, use the kill function to reduce the bullet count whenever a bullet is destroyed:
kill: function(){ --;

Now when you press Z, you check to make sure there are 3 bullets or less:
if( ig.input.pressed('fire')  && <=3){
// spawn bullet
// too many bullets, can't spawn until one is destroyed

8 years ago by Wake

Oh! i will try that stahlmanDesign, thanks for that! if it works, i will post my code.


8 years ago by stahlmanDesign

Make sure in main.js you define bullets before the init function:

bullets: 0,
init: function(){
Page 1 of 1
« first « previous next › last »