Jump to content

Issue with inheritance


Nepoxx
 Share

Recommended Posts

 
Hi everyone,
 
I'm trying to create a clean HUD class using Phaser 2.0.7 but I am getting a weird error:
"Uncaught TypeError: Cannot read property 'x' of undefined "
 

I created a separate hud.js file with the following code: 

function HeadUpDisplay(game, parent, name, useStage){    Phaser.Group.call(this, game);    var fpsCounter = new Phaser.Text(0,0, "test", {        font: "16px Arial",        fill: "#ff0044"    });    fpsCounter.fixedToCamera = true;    this.add(fpsCounter);    this.update = function() {        fpsCounter.setText(game.time.fps);    };};HeadUpDisplay.prototype = Object.create(Phaser.Group.prototype);HeadUpDisplay.prototype.constructor = HeadUpDisplay; 

So basically my "class" HeadUpDisplay is extending Phaser.io's group. Any idea what is wrong? (and/or what could be improved)

 

Thanks! :)

Link to comment
Share on other sites

1. you must set game.time.advancedTiming = true; in order to use game.time.fps (regardless to what you asked)

2. I have a gut feeling that this error is not related to this code since I don't see any 'x' property in here.

Link to comment
Share on other sites

I'm new to javascript classes and prototypes, but isn't the first line..
function HeadUpDisplay(game, parent, name, useStage){

supposed to be like this?  :huh: Or is that semantically the same thing?

var HeadUpDisplay = function(game, parent, name, useStage){
Link to comment
Share on other sites

You've missed the first argument to the Phaser.Text constructor, which should be a reference to the game object.

 

Your code:

var fpsCounter = new Phaser.Text(0,0, "test", {        font: "16px Arial",        fill: "#ff0044"    });

should be

var fpsCounter = new Phaser.Text(game, 0,0, "test", {        font: "16px Arial",        fill: "#ff0044"    });
Link to comment
Share on other sites

Thanks!

 

Although, then, what's the difference between

var fpsCounter = new Phaser.Text(game, 0,0, "test", {        font: "16px Arial",        fill: "#ff0044"});

and

var fpsCounter = new game.add.text(0,0, "test", {        font: "16px Arial",        fill: "#ff0044"    });
Link to comment
Share on other sites

 

I'm new to javascript classes and prototypes, but isn't the first line..
function HeadUpDisplay(game, parent, name, useStage){

supposed to be like this?  :huh: Or is that semantically the same thing?

var HeadUpDisplay = function(game, parent, name, useStage){

 

I would suggest reading this: http://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/ . They definitely explain it better than I could :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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