Jump to content

"Abstract Classes"


LuckieLordie
 Share

Recommended Posts

Hi guys!

 

I've got more of a javascript question for you! If mods aren't happy about it being here then just let me know! 

 

I have in my game some collectables. Every collectable in my game does a few things. Creates itself when asked by the manager, Removes itself when asked by the manager and has the properties for doing that.

 

However, when I collide with a different object they need different behaviours. In OO I would use an abstract class and inherit all my different collectables from it. Here is my take at that idea in Javascript.

 

Base Class

Collectable = function(imageRef, game, posX, posY){	this.posX = posX;	this.posY = posY;	this.imageRef = imageRef;		this.spriteRef = null;	this.physicsShape = null;	this.alive = false;	this.collided = false;	this.game = game;	this.bounceSpeed = 2;	this.bounceOffset = 10;	};Collectable.prototype.constructor = Collectable;Collectable.prototype.AddCollectable = function(){	this.alive = true;	this.spriteRef = this.game.add.sprite(this.posX, this.game.world.height - this.posY, this.imageRef);	this.game.gameGroup.add(this.spriteRef);	this.AddShape();	this.spriteRef.anchor.setTo(0.5, 0.5);};Collectable.prototype.AddShape = function(){	var staticBody = this.game.game.space.staticBody;	this.boundingBox = new cp.BB(this.posX, 								 this.posY,								 this.posX + this.spriteRef.width, 								 this.posY + this.spriteRef.height);	//shape definition	this.physicsShape = this.game.game.space.addShape(new cp.BoxShape2(staticBody, this.boundingBox));	this.physicsShape.setSensor(true);	this.physicsShape.setCollisionType(2);	this.physicsShape.parentObject = this;	this.game.game.space.addCollisionHandler(2, 0, 		this.PlayerCollisionCallback, null, null, null);};Collectable.prototype.PlayerCollisionCallback = function(arb){	console.log("A Collectable hasn't implemented a collision function!");	};Collectable.prototype.RemoveCollectable = function(){	this.game.game.space.removeShape(this.physicsShape);	this.spriteRef.destroy();	this.spriteRef = null;	this.alive = false;};Collectable.prototype.Update = function(){ };

(I think I need to switch from adding and removing sprites to killing and reviving).

 

Here is one of my "Inherited" objects

CoinCollectable = function(imageRef, game, posX, posY){	Collectable.call(this, imageRef, game, posX, posY);};CoinCollectable.prototype.constructor = CoinCollectable;CoinCollectable.prototype = Object.create(Collectable.prototype);CoinCollectable.prototype.PlayerCollisionCallback = function(arb){	var shapes = arb.getShapes();	var collectable = shapes[0].parentObject;	var player = shapes[1].parentObject;	console.log("COIN COLLIDE");	player.coinsCollected++;	collectable.collided = true;};

Riight, that was a lot of setup.

 

My problem is that in my game I have two types of collectable at the moment. Fuel and Coin. Fuel is identical to Coin except the content of the "PlayerCollisionCallback" function. However they don't use the correct behaviour in game. I'm wondering if it's because of the way I've set up the "inherited classes".

 

Any help would be great!

Link to comment
Share on other sites

firstly, I think you've simply got the two lines

 

  1. CoinCollectable.prototype.constructor = CoinCollectable;
  2. CoinCollectable.prototype = Object.create(Collectable.prototype);

 

reversed. the second line undoes what the first line accomplishes and you end up with a Collectable, not a CoinCollectable

 

secondly, trying to emulate "OOP" features in javascript is not always fruitful. abstract base classes (interfaces) in particular don't really lend themselves well. javascript, being a dynamic language is much better suited to "duck typing" such as one might use in python or similar.

 

your example seems to be more one of an actual, non-abstract base-class however, so it seems you may not be looking for abstract classes at all... 

 

HTH anyway :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...