espace Posted September 5, 2017 Share Posted September 5, 2017 hi, i want reduce my function inside my state with a reuse function.Is it possible ? How ? thanks for your advice . //NORMAL USAGE var level = { create :function(){ } update :function(){ }, render:function(){ }, } game= new Phaser.Game(1280,1920,Phaser.CANVAS,'game') game.state.add('boot',bootstate ) game.state.add ('level',level ) game.state.start('level',level) ///////////////////////////////////////////////////////// ///////////////////////////////////////////////////////// ///////////////////////////////////////////////////////// //WHAT I WANT TO DO ///////////////////////////////////////////////////////// init_level= ()=>{ create :function(){ } update :function(){ }, render:function(){ }, } var level = { init_level() } game= new Phaser.Game(1280,1920,Phaser.CANVAS,'game') game.state.add('boot',bootstate ) game.state.add ('level',level ) game.state.start('level',level) Link to comment Share on other sites More sharing options...
Taz Posted September 5, 2017 Share Posted September 5, 2017 You could do it like this, where init_level() creates and returns the object: function init_level () { return { create: function () { }, update: function () { }, render: function () { } }; } var level = init_level(); Link to comment Share on other sites More sharing options...
mattstyles Posted September 5, 2017 Share Posted September 5, 2017 The syntax you used for the anonymous function (lambda/arrow function) is invalid, I think you want an implicit return, but as { } delineates both function boundaries and an object you have to use ( ) to allow returning an object like this. The bit you're actually missing is to invoke the function to return something you want, magig answer above addresses this, the following is the same: const init = opts => ({ create: () => { console.log('Creating state', opts.id) }, update: () => {...}, render: () => {...} }) game.state.add('boot', init({id: 'boot'}) game.state.add('level', init({id: 'level'}) Link to comment Share on other sites More sharing options...
espace Posted September 6, 2017 Author Share Posted September 6, 2017 hi, thanks both for your response. simply an interrogation @mattstyles what is id and opts in your function ? Link to comment Share on other sites More sharing options...
mattstyles Posted September 6, 2017 Share Posted September 6, 2017 Ah, I've just replied on the other topic related to this that probably explains it. The `opts` parameter is an object, I've then used `opts.id` as an example of how you might use the initialisation object to create different instances, it is very akin to using classes and new but without all the overhead of class-based (or JS's version of it) OOP. The assumption here is that you want your levels wrapped in a function because you need to create different instances of what is basically the same thing (or provide defaults etc etc), if you don't need to pass anything in to the initialisation function then you may as well just explicitly create your object instances and pass them around (as most tutorials do for Phaser states). There might be some benefits of variable privatisation by wrapping things in a closure (the function that returns the actual level object) but I'm not sure I can think of any other benefits if you don't pass variables in to initialise the level object. Link to comment Share on other sites More sharing options...
Recommended Posts