fariazz Posted April 9, 2015 Share Posted April 9, 2015 When creating States I've seen both people using Objects and Functions. I've noticed that in the Phaser Coding Tips all the examples are using Functions. Is there any advantage in using Functions over Objects in this case? I understand that using Functions + prototype is good for when you are creating classes and then instances of those classes, but in this case there is only 1 of each State in a game. Link to comment Share on other sites More sharing options...
brejep Posted April 9, 2015 Share Posted April 9, 2015 Both work, so it is mostly a matter of personal preference. I've used functions for states in the past because I've used the new operator and tried to keep a sort of OOP/classes style to my code. As everything else has used function and prototype, I didn't want to code states in a different way. Also, Phaser itself uses function/prototype for classes so following that style makes sense if your game relies on Phaser as a framework. Objects are a little bit lighter weight but if you are just creating one instance of them that won't have much of an effect on performance. I am, personally, trying to move all my code into a more object-based model - i.e. not using the new operator but Object.create instead. It is JavaScript so there are lots of ways of doing everything, most of which have good and bad aspects. ZoomBox and drhayes 2 Link to comment Share on other sites More sharing options...
fariazz Posted April 9, 2015 Author Share Posted April 9, 2015 Both work, so it is mostly a matter of personal preference. I've used functions for states in the past because I've used the new operator and tried to keep a sort of OOP/classes style to my code. As everything else has used function and prototype, I didn't want to code states in a different way. Also, Phaser itself uses function/prototype for classes so following that style makes sense if your game relies on Phaser as a framework. Objects are a little bit lighter weight but if you are just creating one instance of them that won't have much of an effect on performance. I am, personally, trying to move all my code into a more object-based model - i.e. not using the new operator but Object.create instead. It is JavaScript so there are lots of ways of doing everything, most of which have good and bad aspects. That makes sense thanks for the explanation! Link to comment Share on other sites More sharing options...
Recommended Posts