Skip to content

Commit 4a91d97

Browse files
committed
Input.addMoveCallback used to return the index of the callback entry in the internal moveCallbacks array. However as callbacks were removed the indexes became invalid, potentially causing a future Input.deleteMoveCallback to remove the wrong callback entirely or error. Input.deleteMoveCallback now takes the original callback and context as its parameters to ensure deletion safety.
1 parent 9ce76fc commit 4a91d97

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

src/input/Input.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -441,30 +441,37 @@ Phaser.Input.prototype = {
441441
*
442442
* It will be called every time the activePointer moves, which in a multi-touch game can be a lot of times, so this is best
443443
* to only use if you've limited input to a single pointer (i.e. mouse or touch).
444+
*
444445
* The callback is added to the Phaser.Input.moveCallbacks array and should be removed with Phaser.Input.deleteMoveCallback.
445446
*
446447
* @method Phaser.Input#addMoveCallback
447448
* @param {function} callback - The callback that will be called each time the activePointer receives a DOM move event.
448449
* @param {object} context - The context in which the callback will be called.
449-
* @return {number} The index of the callback entry. Use this index when calling Input.deleteMoveCallback.
450450
*/
451451
addMoveCallback: function (callback, context) {
452452

453-
return this.moveCallbacks.push({ callback: callback, context: context }) - 1;
453+
this.moveCallbacks.push({ callback: callback, context: context });
454454

455455
},
456456

457457
/**
458-
* Removes the callback at the defined index from the Phaser.Input.moveCallbacks array
458+
* Removes the callback from the Phaser.Input.moveCallbacks array.
459459
*
460460
* @method Phaser.Input#deleteMoveCallback
461-
* @param {number} index - The index of the callback to remove.
461+
* @param {function} callback - The callback to be removed.
462+
* @param {object} context - The context in which the callback exists.
462463
*/
463-
deleteMoveCallback: function (index) {
464+
deleteMoveCallback: function (callback, context) {
465+
466+
var i = this.moveCallbacks.length;
464467

465-
if (this.moveCallbacks[index])
468+
while (i--)
466469
{
467-
this.moveCallbacks.splice(index, 1);
470+
if (this.moveCallbacks[i].callback === callback && this.moveCallbacks[i].context === context)
471+
{
472+
this.moveCallbacks.splice(i, 1);
473+
return;
474+
}
468475
}
469476

470477
},

0 commit comments

Comments
 (0)