Jump to content

Getting p2 is not defined


brejep
 Share

Recommended Posts

I've just tried the Phaser 1.2 branch with a project that uses requirejs and I get a reference error for p2:

Uncaught ReferenceError: p2 is not defined

Do I have to do something special to get this version working with requirejs?

 

I think there were similar problems with SAT for a while in the 1.1.x version but that was fixed.

Link to comment
Share on other sites

It looks like even with the changes made to the 1.2 branch grunt tasks I get problems with p2 and PIXI not being defined. They aren't being put in global scope and so Phaser doesn't find them.

 

I can fix it in PIXI by using this UMD pattern: https://github.com/umdjs/umd/blob/master/amdWebGlobal.js but trying to apply the same thing to p2 seems difficult. The wrapper they use does not lend itself to being understood. 

 

In the end I just made p2 global by removing the define check ("function"==typeof define&&define.amd) but that isn't a real solution just a hack so I could try out p2 with my current project.

 

I expect this may be the prod that will move me to use browserify instead.

Link to comment
Share on other sites

Ok, I have come up with a solution that just about works for requirejs:

 

At the moment, all the libraries (p2, PIXI, Phaser) have a simple define(module) call, for example in the Pixi Outro.js file:

else if (typeof define !== 'undefined' && define.amd) {        define(PIXI);    }

Which I've changed to:

} else if (typeof define !== 'undefined' && define.amd) {        define('PIXI', (function() { return (root.PIXI = PIXI); })());    }

This gives us a global instance of PIXI and a named 'PIXI' module that require can recognise. That allows Phaser to find the global instance of PIXI and allows you to have several modules in one file.

 

Similar change to line 24 of the p2.js file:

 

From: 

"function"==typeof define&&define.amd?define(e)

To: 

"function"==typeof define&&define.amd?define('p2', (function() {return (this.p2 = e());})())

And in Phaser's Outro.js file, I've just named the module without creating a global instance, by changing line 13:

 

From :

define(Phaser);

To:

define('phaser', Phaser);

Then, in require I can do something like this:

requirejs.config({    baseUrl: 'script',    paths: {        phaser: 'vendor/phaser/build/phaser',        p2: 'vendor/phaser/build/phaser',        PIXI: 'vendor/phaser/build/phaser'    }});require(['phaser', 'p2', 'PIXI'], function(Phaser, p2, PIXI) {    console.log('phaser', Phaser);    console.log('p2', p2);    console.log('PIXI', PIXI);    var game = new Phaser.Game(400, 300, Phaser.AUTO);});

I tested with and without requirejs and it worked but not tried with anything that uses exports instead.

Link to comment
Share on other sites

  • 1 year later...

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...
 Share

  • Recently Browsing   0 members

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