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 FriendlyDuck

Hey all, I'm new to impact and am having trouble with spawning an entity in my game. The idea is that when spacebar is pressed, the entity is created. This works, however anywhere between 5 - 8 instances of it appear on the screen! I only want one.

Here's my code:

ig.module(
	'game.entities.player'
)
.requires(
	'impact.entity'
)
.defines(function(){

EntityPlayer = ig.Entity.extend({
    size: {x:80, y:80},
	maxVel: {x: 100, y: 150},
	friction: {x: 600, y: 0},
	gravityFactor:0,
	type: ig.Entity.TYPE.A,
	zIndex:99,
    
    animSheet: new ig.AnimationSheet( 'media/container.png', 80, 80),
    
    init: function( x, y, settings ) {
        this.addAnim('idle', 1, [0]); 
		this.addAnim('containerOpen', 1, [1]);        
        this.parent( x, y, settings );
    },
    
    update: function() {
		
		// move left or right
		if (ig.input.state('left')) { 
			this.vel.x = -100;
			this.currentAnim = this.anims.idle;
		}
		if (ig.input.state('right')) { 
			this.vel.x = +100;
			this.currentAnim = this.anims.idle;
		}
		if (ig.input.state('drop')) {
			this.currentAnim = this.anims.containerOpen;
			ig.game.spawnEntity( EntityBrick, this.pos.x, this.pos.y, {zIndex:1} );
			ig.game.sortEntitiesDeferred();
			//for( var i = 0; i < 5; i++ ) {
			//	var e = ig.game.spawnEntity(EntityBrick, this.pos.x, this.pos.y);
			//}
		}    
        this.parent();
    }    
});

// Brick Entity
EntityBrick = ig.Entity.extend({
	size: {x:11, y:7},
	type: ig.Entity.TYPE.B,
	checkAgainst: ig.Entity.TYPE.B,
	collides: ig.Entity.COLLIDES.ACTIVE,
	bounciness:0.5,
	zIndex:99,
	
	animSheet: new ig.AnimationSheet('media/brick.png', 11, 7),
    
    init: function(x, y, settings) {
        this.addAnim('idle', 0.5, [0]);        
        this.parent(x, y, settings);
    }
});

});

1 decade ago by blingum

In the case of the drop, you'll want to use

ig.input.pressed('drop')

instead of

ig.input.state('drop')

Pressed only returns true if the button was just pressed down and only once per press, where state returns true for as long as the button is pressed down. The update function is firing multiple times during each key press and state is returning true each time, so multiple entities are being generated.

Pressed is generally good for actions you just want to trigger once per key press, like some projectiles, while state is good for movements where you want to move an entity as long as a key is pressed down.

1 decade ago by FriendlyDuck

Awesome! Thanks for the info, it works perfectly now.
Page 1 of 1
« first « previous next › last »