Sign in to follow this  

Trouble with creating "grand child" instance of Sprite

Recommended Posts

Hi people,

I'm extending the Sprite class with a class named "Being", which I further extend with two child classes, "Monster" and "Player". But it doesn't work. Here is first the code for my classes.

function Being(x,y,key){, game, x,y,key);
Being.prototype = Object.create(Phaser.Sprite.prototype);
Being.prototype.constructor = Being;

function Player(x,y,key){,y,key);
Player.prototype = Object.create(Being.prototype);
Player.prototype.constructor = Player;

function Monster(x,y,key){,y,key);
Monster.prototype = Object.create(Being.prototype);
Monster.prototype.constructor = Monster;

("game" is the Phaser.Game instance and is a global variable, I didn't forget to pass it as an argument.)
I have looked at the examples and forum topics, and it seems to me that it's the proper way to do. Now if I create a new instance of Being, which inherits from Sprite, this works like a charm.

    var B = new Being(300,300,'player');

But if I try to make a new instance of Player, which inherits from Being which itself inherits from Sprite, the it doesn't work. The code is:

    var P = new Player(200,200,'player');

And in the console I get the following error: "phaser.js:15246 Uncaught TypeError: this.onTextureUpdate is not a function". This error makes the entire game crash.

I'm really puzzled by the fact that it works for Being, but not for Player, which seems to indicate that all this code is mostly correct, except that I must be doing something wrong with the "second-level" inheritance. Any help would be greatly appreciated!

Share this post

Link to post
Share on other sites

More specifically, Sprites need the game object (which is usually 'this' inside a state) in order to initialise most of their subsystems. This means all of your constructors need an additional parameter of 'game', and your instantiation statements should then be something like:

var B = new Being(game,300,300,'player');

... or...

var B = new Being(this,300,300,'player');

And when extending Being, make sure that the game reference is being passed also.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.