Bolko Posted March 27, 2015 Share Posted March 27, 2015 Hi, i´m trying to extend PIXI.DisplayObjectContainer, copied it from PIXI.SpriteStartScreen = function(testSprite){ PIXI.DisplayObjectContainer.call( this );}// constructorStartScreen.prototype = Object.create( PIXI.DisplayObjectContainer.prototype );StartScreen.prototype.constructor = StartScreen;But it does not workvar startScreen = new StartScreen(); // _cont.addChild(startScreen); //throws an error "undefined is not a function" in DOP.js:123 console.log(startScreen); // looks like a DOP console.log(startScreen instanceof StartScreen); //true console.log(startScreen instanceof PIXI.DisplayObjectContainer); // false -> should be trueWhat did i get wrong? Quote Link to comment Share on other sites More sharing options...
alex_h Posted March 27, 2015 Share Posted March 27, 2015 StartScreen.prototype.constructor = StartScreen;should beStartScreen.constructor = StartScreen; Quote Link to comment Share on other sites More sharing options...
Sebi Posted March 27, 2015 Share Posted March 27, 2015 @alex_h It's StartScreen.prototype.constructor = StartScreen; @Bolko The code looks fine. I doubt that you are getting that error. We would need to see more of your code. Maybe the issue lies somewhere else. Maybe PIXI is out of scope and that's why the second instanceof test fails. Maybe there is an issue with your _cont object. Quote Link to comment Share on other sites More sharing options...
alex_h Posted March 28, 2015 Share Posted March 28, 2015 Fuck,you are right! I think I must have been on drugs or something when I wrote that sorry Quote Link to comment Share on other sites More sharing options...
xerver Posted March 29, 2015 Share Posted March 29, 2015 Aside from globally promoting your StartScreen function, this code is fine. You are likely reassigning prototype somewhere else. Are you doing something like this:StartScreen = function(testSprite){ PIXI.DisplayObjectContainer.call( this );}// constructorStartScreen.prototype = Object.create( PIXI.DisplayObjectContainer.prototype );StartScreen.prototype.constructor = StartScreen;StartScreen.prototype = { /// ... methods and such};Because that will not work, for hopefully obvious reasons. Quote Link to comment Share on other sites More sharing options...
Bolko Posted March 30, 2015 Author Share Posted March 30, 2015 Thanks guys, the global scope seemed to be the problem.I´m coming from AS3 and the 37 possible approaches to use OOP in JS are driving me mad. I just copied the "PIXI way" here and now it works.So is this the way to go or not really?(function(){ var root = this; StartScreen = function(testSprite) { PIXI.DisplayObjectContainer.call( this ); this.addChild(testSprite); } // constructor StartScreen.prototype = Object.create( PIXI.DisplayObjectContainer.prototype ); StartScreen.prototype.constructor = StartScreen; root.StartScreen = StartScreen; }).call(this); Quote Link to comment Share on other sites More sharing options...
xerver Posted March 30, 2015 Share Posted March 30, 2015 The export to global scope is not going to mess with your prototype. That isn't the problem. You should organize your code however you want. The first and second snippet you posts are functionally equivalent, you didn't change anything by putting it into a closure like that. But you should *definitely* stop doing implicit global promotion (which happening because you have no var statement btw, so it assumes you mean window.StartScreen). function StartScreen(testSprite) { PIXI.DisplayObjectContainer.call(this); this.addChild(testSprite);}StartScreen.prototype = Object.create(PIXI.DisplayObjectContainer.prototype);StartScreen.prototype.constructor = StartScreen;Is totally fine, like I mentioned before if that doesn't work something else in your program is coming along and blowing away the prototype. Quote Link to comment Share on other sites More sharing options...
Bolko Posted March 30, 2015 Author Share Posted March 30, 2015 You should organize your code however you want. The first and second snippet you posts are functionally equivalent, you didn't change anything by putting it into a closure like that.Strange, I don´t think i changed anything else, well, now it works But you should *definitely* stop doing implicit global promotion (which happening because you have no var statement btw, so it assumes you mean window.StartScreen).Not sure if I got that right.Do you mean thatStartScreen = function() {}is bad, butvar StartScreen = function() {}andfunction StartScreen() {}are fine? Quote Link to comment Share on other sites More sharing options...
xerver Posted March 31, 2015 Share Posted March 31, 2015 The first snippet implicitly promotes to global, so everytime you use that variable you are really just doing window.StartScreen. Read up on hoisting, global promotion, and function declaration vs function definitions. Quote Link to comment Share on other sites More sharing options...
Bolko Posted March 31, 2015 Author Share Posted March 31, 2015 Yep, i know these are general JS questions and I´ve read lots of articles only to become more confused I´d love to see an example of how a pixi project can be built up with several screens or scenes.This one is nice but I´d love to see one not built in Typescript, but Javascript: http://ezelia.com/2013/pixi-tutorial There are loads of examples, but all of them are only one-screeners, I´d like to learn more about a good practice for building a more complex app. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.