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 ArcadeHype

I was just wondering if there is a way to create a base class such as "EntityEnemySkeleton" and then spawn different types of enemies such as "EntityAlien" or "EntityRobot" which would extend the "EntityEnemySkeleton" class allowing my "enemies" to inherit all the same kill, check and receivedamage methods that are defined in the base "EntityEnemySkeleton" class.

I tried the following but doesn't seem to work:

ig.module(
	'game.entities.enemy'
).requires(
	'impact.entity'
)
.defines(function() {
	
	EntityEnemy = ig.Entity.extend({
		
		size: {x:35 , y:62},
		offset: {x:0, y:0},
		maxVel: {x:100, y:100},
		friction: {x:150, y:0},
		speed:100,
		flip: false,
		
		//collision properties
		collides: ig.Entity.COLLIDES.PASSIVE,
                type: ig.Entity.TYPE.B,
                checkAgainst: ig.Entity.TYPE.A,
		
		init: function(x,y,settings){
			this.parent(x,y,settings);	
		},
	
		update: function(){
			
			//near an edge? return!
			if(!ig.game.collisionMap.getTile( this.pos.x + (this.flip ? +4 : this.size.x -4), this.pos.y + this.size.y + 1 )){
				this.flip = !this.flip;
			}
			
			var xdir = this.flip ? -1 : 1;
			this.vel.x = this.speed * xdir;
			this.currentAnim.flip.x = this.flip;
			this.parent();
			
		},
		
		handleMovementTrace: function(res){
			this.parent(res);
			//collision with a wall? return!
			if(res.collision.x){
				this.flip = !this.flip;
			}
		},
		
		check: function( other ) {
			other.receiveDamage( 10, this );
		},
		
		kill: function(){
			this.parent();
			ig.game.spawnEntity(EntityExplosion, this.pos.x, this.pos.y, {particles:30, colorOffset:1});
		},
		
		receiveDamage: function(value){
			this.parent(value);
			if(this.health > 0){
				ig.game.spawnEntity(EntityExplosion, this.pos.x, this.pos.y, {particles:10, colorOffset: 1});
			}
		},
		
	});
	
}); //end of enemy skeleton class

ig.module(
	'game.entities.alien'
).requires(
	'impact.entity',
	'game.entities.enemy'
)
.defines(function() {
	
	EntityAlien = EntityEnemy.extend({
		
		animSheet: new ig.AnimationSheet('media/alien_one.png', 35,62),
		
		init: function(){
			
			this.addAnim('walk', .1, [0,1,2,3,4,5]);
			
			this.parent();
			
		},
		
	});
	
}); //end of alien class

My game loads up fine but my entity doesn't appear and it doesn't appear in weltmeister either. Is there a way to accomplish this task and can weltmeister load entities that are sub-classed?

1 decade ago by ArcadeHype

on a side note, which application can i use to paste my code in the proper format?

1 decade ago by taddeimania

Yes weltmeister can load entities that are subclassed. I'm not sure if it's a hard requirement but are your child entities in their own .js file? so you would have baseAlien.js and a blueAlien.js would include the basealien object and extend it?

If you are ever not seeing anything working the way you expect it to you should always pop over to the developer tools (firebug/chrome dev/god help you if you're on ie) and look in the console for any big red errors. That will typically help debug the problem you're having.

Here is an example of entities that are subclassed at multiple levels.

https://github.com/taddeimania/impactjs-sliderpuzzle/tree/master/lib/game/entities

the mainBlock is parent wth hBlock and vBlock inheriting from main and the 3's inheriting from the h and v.

I hope that helps.

1 decade ago by mimik

So you have this in a regular entity

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

EntityEnemy = ig.Entity.extend({


And in your subclass/child enemy who corelates to EntityEnemy:

ig.module(
'game.entities.newAwesomeEnemy'
).requires(
'impact.entityEnemy'
)
.defines(function() {

newAwesomeEnemy = ig.EntityEnemy.extend({

1 decade ago by ArcadeHype

Hey Guys,

Thanks for the help, i got it working properly now.

Cheers!

1 decade ago by taddeimania

What was the problem?

1 decade ago by ArcadeHype

everything was fine actually, i just had a mistype in my code...errrr!

1 decade ago by taddeimania

Ah it happens to everyone.
Page 1 of 1
« first « previous next › last »