Jump to content

reduce state ...possible with a reuse function ?


espace
 Share

Recommended Posts

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

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

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

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

 Share

  • Recently Browsing   0 members

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