Jump to content

Pass argument(s) to events


gelöscht
 Share

Recommended Posts

I can't seem to find a way to pass custom argument(s) to an event. Is this even possible?

Something like this:

sprite1.events.onInputDown.add( doSomethingWithNumber, this, 1); //pass the number 1sprite2.events.onInputDown.add( doSomethingWithNumber, this, 3); //pass the number 3function doSomethingWithNumber(inNumber){    //do something with inNumber here}

We really need this for a project we are currently working on. If this is not possible, how would you work around this issue?

 

Link to comment
Share on other sites

sprite1.events.onInputDown.add(() => doSomethingWithNumber(3), this);

Does this work? (that's Typescript code)

 

Or if you're in javascript I think it's

sprite1.events.onInputDown.add((function() {   doSomethingWithNumber(3);}), this);
Link to comment
Share on other sites

Thanks for your answers!
 

 

sprite1.events.onInputDown.add(() => doSomethingWithNumber(3), this);

Does this work? (that's Typescript code)

 

Or if you're in javascript I think it's

sprite1.events.onInputDown.add((function() {   doSomethingWithNumber(3);}), this);

I already tried that, but it didn't work. After your post i tried it again...and again it didn't work.
I then found out, that it works, if you pass a "real" number (or var which contains a number). The problem i was having is that i was setting the onInputDown event in a for loop and passed "i" as the number. And everytime the event was fired, "i" was always the same number.

So as a (VERY ugly) workaround i did this:
 

for(i=0; i<answers.length; ++i){	//create answer	this._answerText[i] = this.state.game.add.text(...);	//make it clickable	this._answerText[i].inputEnabled = true;		//f-ugly workaround for passing i to the event function	if(i == 0)		this._answerText[i].events.onInputDown.add( function(){this._chooseAnswer(0);} , this);	else if(i == 1)		this._answerText[i].events.onInputDown.add( function(){this._chooseAnswer(1);} , this);	else if(i == 2)		this._answerText[i].events.onInputDown.add( function(){this._chooseAnswer(2);} , this);	else if(i == 3)		this._answerText[i].events.onInputDown.add( function(){this._chooseAnswer(3);} , this);}

I tried different ways to pass the "current i" to the function, but so far "i" is always the same, once the event is being fired. Tried lookup tables, generating new vars and storing "i" in it, etc. Never worked.

While my original problem is solved and for our needs this workaround works fine, i am still interested in how you can correctly pass "i" to an event. Just so i know how to do it without that ugly workaround in the future. :)

Link to comment
Share on other sites

You should have posted, that your problem is a loop.

 

While running the loop the callback-functions that you create all catpure "i" by reference. So they all point to the one "i" variable, and by the time they get executed "i" has it's last value (the one from the end of the loop.)

 

 

You can fix this, by inserting one indirection (another function) that has it's own scope and captures the variable at its current state:

 

 

//this is the indirection (it captures a new "i" variable)function createFunction(i) {    return function() { test(i)}}//this is the callback function, we really want to callfunction test(value) {    alert(value);}//this is our loopfunctions = new Array();for(i=0; i< 5; ++i){            functions[i] = createFunction(i);}//here we trigger the callbacksfunctions[0]();functions[1]();functions[2]();
Link to comment
Share on other sites

The example in the post above is tested and works.

 

For you concrete example:

function createindirection(myValue, reference) {  return function() { function(){reference._chooseAnswer(myValue);}}//In your loop:this._answerText[i].events.onInputDown.add( createindirection(i, this)  , this);

it should work this way. (not tested)

Link to comment
Share on other sites

 

You should have posted, that your problem is a loop.

 

While running the loop the callback-functions that you create all catpure "i" by reference. So they all point to the one "i" variable, and by the time they get executed "i" has it's last value (the one from the end of the loop.)

 

 

You can fix this, by inserting one indirection (another function) that has it's own scope and captures the variable at its current state:

 

 

//this is the indirection (it captures a new "i" variable)function createFunction(i) {    return function() { test(i)}}//this is the callback function, we really want to callfunction test(value) {    alert(value);}//this is our loopfunctions = new Array();for(i=0; i< 5; ++i){            functions[i] = createFunction(i);}//here we trigger the callbacksfunctions[0]();functions[1]();functions[2]();

 

 

This works great. Thanks!

I'll make sure to bring some cake and/or beer when i visit Ulm next time.  ;)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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