Jump to content

Extend sprite class on external js


Arlefreak
 Share

Recommended Posts

I'm trying to extend an object to the sprite class in my game.js I know this is achived with:
 

MonsterBunny.prototype = Object.create(Phaser.Sprite.prototype);MonsterBunny.prototype.constructor = MonsterBunny;

But in the structure that is in the examples I'm not sure where to put it, I have it like this:

BasicGame.Game.prototype = {	MonsterBunny : function(game, rotateSpeed) {			/*some code*/	},	Box : function(game, rotateSpeed) {		/*some code*/	},    create: function () {    },    update: function () {	}};		/* I don't know where do I need to put this */	/*===========================================================*/		MonsterBunny.prototype = Object.create(Phaser.Sprite.prototype);	MonsterBunny.prototype.constructor = MonsterBunny;	MonsterBunny.prototype.update = function() {		//  Automatically called by World.update		this.angle += this.rotateSpeed;	};	/*===========================================================*/

I know this is more a JS (noob) question, but i couldn't find a solution, so help would be appreciated

Link to comment
Share on other sites

the short answer is "wherever you want". but the typical pattern is:

// define the "class" constructorMyModule.MyClass = function( some_args ) {    // set some properties    this.foo = 'bar';};// set-up the "class" to inherit from 'SomeBaseClass'MyModule.MyClass.prototype = Object.create( SomeBaseClass.prototype );// re-set the constructor (so it's not just an alias for 'SomeBaseClass')MyModule.MyClass.prototype.constructor = MyModule.MyClass;

you typically keep that code all in the same place, both for readability and to keep bugs out

Link to comment
Share on other sites

  • 3 weeks later...

I think I am having a similar issue and would highly appresiate anyone that can shed some light on it.

 

Is it possible to add "update" to the prototype chain of myObject() ?

 

I have tryed all sorts of different incantations with no luck. I am able to extend Sprite like in the examples and access update, but I want control over it in my inheritance.

 

As I am just starting out with js, I may not be frasing things correctly. But I am serious about learning more.

 

Here is what I would like to do. Hope it makes sence:

function EmitObj(){}EmitObj.prototype.emit = function(){};EmitObj.prototype.emX = function(cndX){};EmitObj.prototype.emY = function(cndY){};function SubEmit(){  EmitObj.call(this);}SubEmit.prototype = Object.create(EmitObj.prototype);SubEmit.prototype.constructor = SubEmit;SubEmit.prototype.subUp = function(){};// wrong implementation...any thoughts?SubEmit.prototype.update = function(){    //console.log(this.xem);}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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