AbdSab Posted November 30, 2014 Share Posted November 30, 2014 Hi all, so i'm new in phaser, i've started learning it by making a project, so i have a problem : I have more than one level for example 10 levels each one has it's own state, so the problem i don't want to make every time the same update function and other functions for every state. So is there any way to make one function for all the 10 state ? And thank's Link to comment Share on other sites More sharing options...
eguneys Posted November 30, 2014 Share Posted November 30, 2014 Use object inheritance using Object.create http://blog.etianen.com/blog/2013/05/26/javascript-inheritance/. or use mixins, decorators etc, Phaser has no built in solution for this, you need to roll your own. Also check out my post about structuring States http://www.sitepoint.com/javascript-game-programming-using-phaser/. AbdSab 1 Link to comment Share on other sites More sharing options...
AbdSab Posted November 30, 2014 Author Share Posted November 30, 2014 Thank you very much for your help Link to comment Share on other sites More sharing options...
ageibert Posted December 1, 2014 Share Posted December 1, 2014 I had the same problem and did also choose the inheritance method.Here's some code that could help: The main Level "class" all Levels inherit from: https://github.com/geibi/phaser/blob/master/js/Level.js There i got some "parentCreate" and "parentUpdate" etc. function which are called by the child Level classes e.g.https://github.com/geibi/phaser/blob/master/js/Levels/Level_2.js Here you see e.g. in line 21BasicGame.Level_2.prototype.update = function () { this.parentUpdate();and after the update, all other functions for this specific level are called hope this helps AbdSab 1 Link to comment Share on other sites More sharing options...
AbdSab Posted December 1, 2014 Author Share Posted December 1, 2014 Thank you it's very clear and easy to understand Link to comment Share on other sites More sharing options...
Recommended Posts