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 quidmonkey

I'm trying to implement a particle system for my Breakout-style game similar to the one used in Biolab. I copied and added particle.js, and created several Entity particle classes named EntityParticleBlock + Color (where Color is the corresponding color of the block i.e. EntityParticleBlockRed, EntityParticleBlockBlue, etc.). These child classes are sub-classed from this parent:


ig.module(
	'game.entities.particle-block'
)
.requires(
	'game.entities.particle'
)
.defines(function(){
    
EntityParticleBlock = EntityParticle.extend({
	lifetime: 2,
	fadetime: 1,
	bounciness: 0.6,
	vel: {x: 60, y: 20},
	size: {x: 4, y: 8},
	
	animSheet: new ig.AnimationSheet( 'media/particle-red.png', 4, 8 ),
		
	init: function( x, y, settings ) {
                this.parent( x, y, settings );
		this.addAnim( 'idle', 1, [0] );	
	}
});

});


I embedded the particle logic in the parent Block class using the logic from Biolab's debris.js:


	update: function(){
		
		if (this.state == 'dead') {
			
			if( this.durationTimer.delta() < 0 && this.nextEmit.delta() >= 0) {
				this.nextEmit.set( this.duration / this.count );
				
				var x = Math.random().map( 0, 1, this.pos.x, this.pos.x + this.size.x );
				var y = Math.random().map( 0, 1, this.pos.y, this.pos.y + this.size.y );

				var particle = 'EntityParticleBlock' + this.color;
				ig.game.spawnEntity(particle, x, y );
			}
			else {
				this.kill();
			}
		}
		
		this.parent();
	},


When I run this, I'm getting the following error in the console:

Uncaught TypeError: Cannot read property 'flip' of null

I'm guessing spawnEntity() is unable to initialize the object, although I don't know why. Any ideas? Maybe I did to change to restructure my code?

1 decade ago by MikeL

Looks like you are calling spawnEntity incorrectly. The first paramter is the actual object not a string. The following should spawn the entity:
ig.game.spawnEntity(EntityParticleBlock, x, y );

Not exactly sure where color comes into play in your program though.

1 decade ago by quidmonkey

Impact's documentation has this to say about spawnEntity():
Create a new entity of the specified type (string or class object) and add it to the game world at x, y.

1 decade ago by MikeL

I see, never used that feature. In that case the problem may be where you add this.color to your string. That is unless you have an entity with that sort of extended object name, e.g. 'EntityParticleBlockBlue'.

1 decade ago by dominic

The way you use spawnEntity() is correct and works just fine (i.e. the entity is created), otherwise you&039;d get the #Can't spawn entity of type X error message.

The problem is that the init() method of the EntityParticle expects that the entity already has an animation - which in your case it doesn&039;t. To fix this, just add the animation first, then call #this.parent():

init: function( x, y, settings ) {
	this.addAnim( 'idle', 1, [0] );
	this.parent( x, y, settings );
}

I really should've documented this a bit better...

1 decade ago by quidmonkey

Thx. That did it.

1 decade ago by stephen7

The first parameter IS a string 'EntityYourName', not an object.
Page 1 of 1
« first « previous next › last »