Jump to content

A Phaser.State should have its own field called 'key'


spinnerbox
 Share

Recommended Posts

This is not a bug, this is a suggestion for the new version of Phaser. Not sure if you are going to agree with me but I think this could be a useful feature.

 

I was searching the API of Phaser 2.2.2, specifically Phaser.State and found that there is no field called 'key' of type string in which I can store my State name and have it there for later use.

 

We can add states like this:

game.state.add('StateName', StateObject);

This works well, but I have to remember and match all those letters in the name. So my suggestion would be, this add function should save the state name in the StateObject.key, or maybe it would be better if I could store my state name in front, like this:

StateObject.key = 'StateName' 

and then use it to add state:

game.state.add(StateObject.key, StateObject);

I already did this in my code, each state have its own inner variable called key and I get it with a KEY() function. 

 

Let me know your thoughts.

 

 

 

 

Link to comment
Share on other sites

Plus a good idea would be to have some object where we will all keep out asset keys. 

Example:

var MY_IMAGE_KEY = 'myImageKey',      MY_SPRITESHEET_KEY = 'mySpriteSheetKey',      MY_SOUND_KEY = 'mySoundKey'; assets: {     MY_IMAGE_KEY: function () {           return MY_IMAGE_KEY;     },     MY_SPRITESHEET_KEY: function () {           return MY_SPRITESHEET_KEY;     },     MY_SOUND_KEY: function () {           return MY_SOUND_KEY;     }}

and then

game.add.image(assets.MY_IMAGE_KEY(),...);

I am using functions to represent immutable constants. Just a thought. 

 

I will add both ideas in Github issues. It is in Phaser Github somewhere?

Link to comment
Share on other sites

You could also use this ECMAScript 5 functionality called Object.defineProperty().

 

So let just say I have MyStateObject and I want it to have a KEY = 'MyStateKeyName'

 

I will do:

addStateKey: function (StateObject, keyName) {   Object.defineProperty(StateObject || {}, 'KEY', {        configurable: false,        enumberable: true,        value:  keyName,        writable: false   });}

and call

addStateKey(MyStateObject, 'MyStateKeyName');

then MyStateObject.KEY = 'MyStateKeyName' will be immutable from outside but still accessible.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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