Jump to content

replacing a pixi constructor with my own version; problem with prototypical inheritance?


wayfinder
 Share

Recommended Posts

Hi!

 

In my (Phaser) project, I'm changing the fragmentSrc and vertexSrc of PIXI.StripShader. I would like to do this from outside the framework—I have a separate file into which I put stuff that I've rewritten, instead of going into the big file and having to carefully diff on every new release. This has never been a problem (I successfully put a bunch of changed pixi functions in there and it always worked), until this one, which is the first time I want to replace a constructor.

 

When I simply put the updated constructor function PIXI.StripShader into my add-on file, it doesn't work—it looks like that removes all the prototypical functions, and I get an error the first time anything tries to call the StripShader's non-existant init function.

 

When I give the updated constructor a new name and set PIXI.StripShader.prototype.constructor to that, it looks like that's simply being ignored.

 

When I duplicate the constructor and all its prototypical functions inside my add-on file, it doesn't work either—I don't get an error message, and the code is not being ignored, however the Strip does not get displayed at all.

 

The shader code I wrote works, but only if I make the changes to the fragmentSrc/vertexSrc in place, in the framework build, which is what I wanted to avoid in the first place.

 

 

Can you help? How would I best handle this?

Link to comment
Share on other sites

Oh my, it was my own stupidity at fault. The shader init function was already one of the functions I had replaced in order for my updated vertex shader and fragment shader to function correctly, so I was overwriting that when I duplicated the constructor and all its (unmodified) prototypical functions at a later point inside my add-on file.

 

So now I have duplicated the whole PIXI.StripShader including all its prototypical methods in my add-on file and made my changes to the code there, and it all works now. But: I'm still wondering whether there's a more elegant way that would allow me to do it without having to replicate all functions, even the ones I don't use, inside my file.

 

Do you know?

Link to comment
Share on other sites

If you did want to replace the constructor function still you can do something like

var oldStripShader = PIXI.StripShader;//you could even be tidy and put this somewhere rather than polluting the global namespace...PIXI.StripShader = function(){   //your custom stuff goes here};PIXI.StripShader.prototype = Object.create(oldStripShader.prototype);PIXI.StripShader.prototype.constructor = PIXI.StripShader;
Link to comment
Share on other sites

 

If you did want to replace the constructor function still you can do something like

var oldStripShader = PIXI.StripShader;//you could even be tidy and put this somewhere rather than polluting the global namespace...PIXI.StripShader = function(){   //your custom stuff goes here};PIXI.StripShader.prototype = Object.create(oldStripShader.prototype);PIXI.StripShader.prototype.constructor = PIXI.StripShader;

This looks like it's working, thanks! One trip-up to look out for was that in PIXI.Strip (for which I similarly edited the constructor), there was a non-prototypical addition of the draw modes, which still got wiped using this method, so they still needed to be duplicated, even though they weren't changed.

Link to comment
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...
 Share

  • Recently Browsing   0 members

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