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 Nico

Hey guys, so I'm playing around with one of my longterm game projects today and I'm trying to make an ammo crate which seems like it should be pretty simple.

I setup grenades and bullets as global variables in main.js and have it setup so that the player can't shoot anything unless his ammo count is greater than 0. I have setup the ammo box to kill itself when it collides with the player as well as log the new ammo count to the console.

It's logging correctly and the game continues to run fine when I pick up the ammo but then when I try to shoot the game crashes with this error:

TypeError: 'true' is not a constructor (evaluating 'new (entityClass)( x, y, settings || {} )') game.js line: 131


I would greatly appreciate if anyone could shed some light on this for me. Happy to post any code that would help.

Thanks
-N

1 decade ago by Joncom

How about posting line 131, and perhaps a couple lines surrounding it? :)

1 decade ago by Nico

	spawnEntity: function( type, x, y, settings ) {
		var entityClass = typeof(type) === 'string'
			? ig.global[type]
			: type;
			
		if( !entityClass ) {
			throw("Can't spawn entity of type " + type);
		}
		var ent = new (entityClass)( x, y, settings || {} );
		this.entities.push( ent );
		if( ent.name ) {
			this.namedEntities[ent.name] = ent;
		}
		return ent;
	},

That block starts at line 123 and ends at 137.

1 decade ago by Joncom

It would appear you are trying to spawn an entity when you get this error (a bullet?). May I see your call to ig.game.spawnEntity?

EDIT I suspect there&039;s a problem with the first parameter #type and it&039;s being passed in as #true.

1 decade ago by Nico

Here's the call to spawnEntity. It worked just fine before I threw in those incrementors which I commented as new lines:

// shoot
if( ig.input.pressed('click') ) {
    if( this.activeWeapon = "EntityBullet" && ig.game.bullets > 0) {
        var mx = (ig.input.mouse.x + ig.game.screen.x); //Figures out the x coord of mouse in world
        var my = (ig.input.mouse.y + ig.game.screen.y); //Figures out the y coord of mouse in world
        var r = Math.atan2(my-this.pos.y, mx-this.pos.x); //Gives angle of mouse position
	    ig.game.spawnEntity( this.activeWeapon, this.pos.x, this.pos.y, {flip:this.flip, angle:r} );
        ig.game.bullets --; //New line
        this.shootSFX.play();
    }
    if( this.activeWeapon = "EntityGrenade" && ig.game.grenades > 0) {
        var mx = (ig.input.mouse.x + ig.game.screen.x); //Figures out the x coord of mouse in world
        var my = (ig.input.mouse.y + ig.game.screen.y); //Figures out the y coord of mouse in world
        var r = Math.atan2(my-this.pos.y, mx-this.pos.x); //Gives angle of mouse position
        ig.game.spawnEntity( this.activeWeapon, this.pos.x, this.pos.y, {flip:this.flip, angle:r} );
        ig.game.grenades --;//New line
        this.shootSFX.play();
    }
}

Here is the ammo box kill function:

kill: function(){
    this.parent();
    ig.game.grenades = 3;
    ig.game.bullets = 50;
    console.log(ig.game.grenades);
    console.log(ig.game.bullets);
}

When the player runs over the box it executes correctly and logs the value of grenades and bullets as 3 and 50 respectively.

The game will run fine if I just pick up the box, but when I trigger spawnEntity after having collected it the game freezes.

EDIT: I feel like a fool but I fixed it...

Had to change this:
 if( this.activeWeapon = "EntityBullet" && ig.game.bullets > 0) 

To this:

if( this.activeWeapon == "EntityBullet" && ig.game.bullets > 0)

Could have sworn I had already tried that as a fix but it didn't work but clearly I was wrong hah.

I really appreciate the help Joncom :)


SECOND EDIT: I think it's also worth noting that I changed:

ig.game.grenades = 3;
ig.game.bullets = 50;

To:

ig.game.grenades = ig.game.grenades + 3;
ig.game.bullets = ig.game.bullets + 50;

Now it correctly adds ammo to the current amount.

1 decade ago by Joncom

Glad you're all sorted out ^^
Page 1 of 1
« first « previous next › last »