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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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