Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extending Phaser.js classes

im working with Phaser framework , i want to create new class that hold all properties from sprite class in phaser so i tried doing this

var mario = function(){

    Phaser.Sprite.call(this); // inherit from sprite
};

but there was an error "Uncaught TypeError: undefined is not a function"

then i tried

var mario = function(){

   this.anything = "";

};

mario.prototype = new Phaser.Sprite();

OK it works but it called phaser constructor and i don't want to create sprite until i do var heroObj = new mario();

what should i do ?

like image 213
Sky Walker Avatar asked Dec 20 '22 05:12

Sky Walker


1 Answers

Try like this. I added a namespace to avoid global variables.

var YourGameNSpace = YourGameNSpace || {};

YourGameNSpace.Mario = function (game) {
    'use strict';

    Phaser.Sprite.call(this, game);
};

// add a new object Phaser.Sprite as prototype of Mario 
YourGameNSpace.Mario.prototype = Object.create(Phaser.Sprite.prototype);
// specify the constructor
YourGameNSpace.Mario.constructor = YourGameNSpace.Mario;

//add new method
YourGameNSpace.Mario.prototype.init = function () {
    'use strict';
    ...
};

And after you can instantiate it:

var mario = new YourGameNSpace.Mario(game);
like image 73
MamaWalter Avatar answered Dec 21 '22 17:12

MamaWalter