Jump to content

Starting a state on input callback


jevisan
 Share

Recommended Posts

Greetings, i'm having issues trying to initiate a state by clicking over an option text. I create my options inside a "LevelSelection" state with a function:

addMenuOption: function(text, callback) {

		var text = this.game.add.text(
			...
		);

		var onOver = function(target) {
			...
		};

		var onOut = function(target) {
			...
		}

		text.inputEnabled = true;
		text.events.onInputUp.add(callback);
		text.events.onInputOver.add(onOver);
		text.events.onInputOut.add(onOut);
	}

So i can construct levels with difficulty inside create() by just:

this.addMenuOption( "easy", function(target) {
	this.state.states['LevelLoad']._diff = "easy";
	this.state.start('LevelLoad');
});

this.addMenuOption( "hard", function(target) {
	this.state.states['LevelLoad']._diff = "hard";
	this.state.start('LevelLoad');
});

obviously the "levelLoad.js" and this "levelSelection.js" are included as states in my main.js and imported within my index.html but when i click over the "easy" text i create im getting the error: "Cannot read property 'start' of undefined". The same goes when i try with: this.game.state.start(...)

And after some experimentation i may think it is because of how the callback is assigned or maybe it is the scope?

I really dont know what to do

Any insights are appreciated

Link to comment
Share on other sites

I figured it out

I dont know why but it seems the adding events function requires the "this" parameter in order to work:

this.txt = this.game.add.text(
			30, 80,
			'text',
			{ font: '20px monospace', fill: '#fff', align: 'left' }
);
this.txt.inputEnabled = true;
this.txt.events.onInputUp.add( this.initiateState, this ); // <- heres the magic

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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