Zampano

"Not an instance of Phaser.Game" Problems

Recommended Posts

Hello,

I just updated from Phaser 2.8.0 to 2.9.1 and I've got some complications that I don't know how to solve best.

I'm using states in different js files with the object BasicGame to hold the states. In BasicGame.Play I create various items of a certain sprite class I wrote. Until now, I did it like this:

var c = new x_collectible(this);

So "this" is an instance of BasicGame.Play. With the new update, I got an error along the lines of "The value passed as the `game` argument ([object Object]) is not an instance of Phaser.Game.". I went ahead and tried to change it to:

var c = new x_collectible(this.game);

It worked, but now I'm having trouble accessing functions or variables of my BasicGame.Play state from within the collectible class. Before I used "this.game.variable" or "this.game.function()". 

What would be a proper solution to this? Adding BasicGame to the arguments when creating a collectible? Adding an isntance BasicGame as an attribute to my Phaser.Game and then do "this.game.BasicGame.Play.variable"?

These don't really seem very practical to me...

Share this post


Link to post
Share on other sites
function x_collectible(game) {	
	
    Phaser.Sprite.call(this, game, 0, 0, "x_collectibles", 0);
		
    this.anchor.setTo(0.5);
		
	this.value = 0;
	
    this.game.physics.enable(this);
	this.body.setCircle(17,3,3);
	
	
	this.sfx = {};
	
	this.sfx["heart"] = this.game.sfx_collectibles["heart"];
	
	this.sfx["superheart"] = this.game.sfx_collectibles["superheart"];
		
	this.sfx["mine"] = this.game.sfx_collectibles["mine"];
		
	this.sfx["ice"] = this.game.sfx_collectibles["ice"];
	
	this.kill();
};

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

the "this.game." calls are those that I don't know how to fix after the change.

Share this post


Link to post
Share on other sites

If you just need to reach the current state, you can do

function x_collectible(game) {
  // …
  this.state = this.game.state.getCurrentState();
  // …
}

and thereafter, e.g., 

// in x_collectible
this.state.property

If you get confused, just do console.log(this.game, this.state) to see what's what.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.