question

Craig avatar image
Craig asked

Phaser Integration Error - cannot set property 'game'

Hi there,

Good to see a plugin for Phaser and PlayFab.

I followed the instructions in this article:

https://api.playfab.com/docs/tutorials/landing-players/phaser

But continue to get this error even after trying a few different syntax permutations.

phaser.min.js:3

Uncaught TypeError: Cannot set property 'game' of undefined

at i.PluginManager.add (phaser.min.js:3)

at Object.create (main.js:29)

at i.StateManager.loadComplete (phaser.min.js:3)

Is there a known issue with the syntax in the article?

Cheers

Craig

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

marcowilliamspf avatar image
marcowilliamspf answered

Hi Craig,

I was able to get to the bottom of your issue. The problem is that you are using the CDN and somehow it has not gotten the update from the repository. This is an automated process so we are looking into why the CDN did not get updated. Meanwhile, you can download the latest Javascript SDK client file from Github and it has the correct code needed for the phaser plugin to work.

6 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Craig avatar image Craig commented ·

Thanks Marco - that worked.

However the next step in getting started is throwing an error.

Please check out the screenshots attached for the code in use and the error seen.

Cheers, Craig

playfab-phaser.jpg

0 Likes 0 ·
playfab-phaser.jpg (229.1 KiB)
brendan avatar image brendan Craig commented ·

Can you try changing that to LoginWithCustomID? That looks like the case-sensitivity is the issue.

0 Likes 0 ·
marcowilliamspf avatar image marcowilliamspf brendan commented ·

Sorry about that, there was a typo in our documentation. It is supposed to be game.PlayFab.ClientApi.LoginWithCustomID

1 Like 1 ·
Show more comments
Craig avatar image Craig commented ·

@Brendan Repo of the the minimal worked example code here:

https://github.com/craigmeltzer/phaser-playfab-sample

Same error as before.

Cheers

0 Likes 0 ·
brendan avatar image
brendan answered

Can you check that this is the first line in your main.js:

var game =newPhaser.Game(800,600, Phaser.AUTO,'',{ preload: preload, create: create, update: update });

It sounds like create is being called before game has been defined in your project.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Craig avatar image
Craig answered

Hey Brendan,

Thanks for response. Yes that Phaser new Game line is at the top of main.js

I've attached a Phaser example of Space Invaders from their samples. It works fine but with the PlayFab plugin add command from the tutorial it fails with "Uncaught TypeError: Cannot set property 'game' of undefined"

Shown below is the bottom of the Phaser Create method where game is already defined and working ok for the rest of the code.

//  And some controls to play the game with
    cursors = game.input.keyboard.createCursorKeys();
    fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
    

// Start PlayFab Integration
   game.PlayFab = game.plugins.add(Phaser.Plugin.PlayFab);

Check out the attached files which should run on any web server and show working Space Invaders but PlayFab error for plugin add.

Once working I don't mind cleaning up and simplifying this example game for download with the tutorial to show scores being saved in PlayFab etc.

Cheers

Craig

phaser-playfab-space-invaders.zip


10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Craig avatar image
Craig answered

Thanks the updated code in the tutorial is now working ok:

https://api.playfab.com/docs/tutorials/landing-players/phaser

Cheers

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

warrenfelsh avatar image
warrenfelsh answered

In JavaScript almost everything is an object, null and undefined are exception. When you try to access an undefined variable it always returns undefined and we cannot get or set any property of undefined. In that case, an application will throw Uncaught TypeError cannot set property of undefined.

In most cases, you are missing the initialization . So, you need to initialize the variable first. Moreover, if you are not sure a variable that will always have some value, the best practice is to check the value of variables for null or undefined before using them. If you just want to check whether there's any value, you can do:

if( value ) {

//do something

}

Or, if you do not know whether a variable exists (that means, if it was declared) you should check with the typeof operator .

if ( typeof(some_variable) !== "undefined" && some_variable !== null ) {

//deal with value

}

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.