Jump to content

Touch callbacks and callbackContext story.


Recommended Posts

I *think* there may be  a problem, but I'm not sure, as all this back end and iOS stuff is really not my speciality.


Story time!


When testing touch controls on the iPad, my callbacks that I had assigned to game.input.touch did not appear to get called.

I assigned the callbacks to the Touch object like this:

game.input.touch.touchStartCallback = this.onTouchDown;game.input.touch.touchEndCallback = this.onTouchUp;

My functions looked like this:

onTouchDown: function(event){    this.game.input.recordPointerHistory = true;    this.doThings = true;},onTouchUp: function(event){        this.game.input.recordPointerHistory = false;    this.doThings = false;},

I had a debug line on the screen that would print out the state of the doThings variable, and it never changed.

I then added a test bool to the touch class, and set it to true/false right before the callback was supposed to be called.

Snippet from Touch.js:

onTouchStart: function (event) {    if (this.touchStartCallback)    {        this.down = true; //My test addition        this.touchStartCallback.call(this.callbackContext, event);    }

My debug line would now print out "true : false" when a touch started, telling me that the Touch.js function excecuted successfully, but my onTouchDown function did not.


I put a breakpoint in my onTouchDown function, and found out that is was being run. The problem was the "this" object.

The callbackContext that the function was being called on was something completely unexpected: an AudioContext object.


In Sound/SoundManager.js, Line 71, is this snippet where the touch callback and context are being set:

if (this.game.device.iOS || (window['PhaserGlobal'] && window['PhaserGlobal'].fakeiOSTouchLock)){    this.game.input.touch.callbackContext = this;    this.game.input.touch.touchStartCallback = this.unlock;    this.game.input.mouse.callbackContext = this;    this.game.input.mouse.mouseDownCallback = this.unlock;    this.touchLocked = true;}

These were never cleared before my functions were assigned to the callbacks, so the context at that point was the SoundManager.


I did notice that the unlock function in SoundManager.js contained this snippet:

if (this.game.device.webAudio == false || (window['PhaserGlobal'] && window['PhaserGlobal'].disableWebAudio == true)){    //  Create an Audio tag?    this.touchLocked = false;    this._unlockSource = null;    this.game.input.touch.callbackContext = null;    this.game.input.touch.touchStartCallback = null;    this.game.input.mouse.callbackContext = null;    this.game.input.mouse.mouseDownCallback = null;}

but this was never called, as evident by the callbackContext not being null when my callback was called.




To solve my issue, all I did was assign my object to the game.input.touch.callbackContext via 

game.input.touch.callbackContext = this;

All that being said, I guess that I needed to set the callbackContext from the beginning (which I didn't do), and I guess that the unlock function is supposed to be called but isn't?



Link to comment
Share on other sites


  • Recently Browsing   0 members

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