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 harryfeng

Hi:

So I have one kind of monster, they behave exactly the same way, but they only look different. (sprite different). can I create one monster and extend that one with different sprite sheet?

Thanks,

H

1 decade ago by Joncom

Yes, you can make your monster entity once and then make another monster entity which basically extends your first. Your second entity would have very little code in it.

1 decade ago by MartinGr

Sure can!

Let's say you have an entity "enemy" you want to extend. Creating an entity "Enemy2" which inherits everything from base class and just replaces the spritesheet can be achieved like this.

ig.module(
  'game.entities.enemy2'
).requires(
  'game.entities.enemy'
).defines(function() {
	
	EntityEnemy2 = EntityEnemy.extend({
				
		animSheet: new ig.AnimationSheet( 'media/enemy2.png', 36, 36 ),
		
		init : function( x, y, settings ){
			
	    this.parent( x, y, settings );
	    
		}
		
	});
	
});

Just remember to require the base entity in the header part of your new module.
Also note, that when extending an entity you don't need to add ig.* in front of the base entity name like you would when creating regular or box2d entity.

1 decade ago by lazer

Would OP need to create Enemy2 at all? Why not just specify two different animSheets? I wrote about one potential way of doing this here: http://liza.io/swapping-out-animation-sheets-on-state-change-in-impactjs/

1 decade ago by MartinGr

True enough, he doesn't actually need to create Enemy2 to achieve what he described. He could simply spawn the entities with different animation sheet with

ig.game.spawnEntity( EntityEnemy, 260, 220, {
	animSheet: new ig.AnimationSheet( 'media/enemy2.png', 36, 36 ),
});

Or he could use the approach you described by defining multiple sprite sheets and switching between them with a parameter.

The downside of this is that Weltmeister will always display the default image making level design a pain. It will also be easier to manage. At some point you might want to give different entities a bit different behaviour (making Enemy3 a bit faster than the others for example). In that case the code will become complete mess unless you use different objects for entities.

On different subject, that's an awesome blog you've got. Good job!

1 decade ago by harryfeng

ig.game.spawnEntity( EntityEnemy, 260, 220, {
animSheet: new ig.AnimationSheet( 'media/enemy2.png', 36, 36 ),
});

thanks for this line of code. Didn't know we could take the 4th parameter in spawnEntity. (not in the reference).

Thank you!
Page 1 of 1
« first « previous next › last »