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 ?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With