Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. 'use strict';
  2.  
  3. define(['phaser'], function(Phaser) {
  4. function Gesture(game) {
  5. this.game = game;
  6.  
  7. this.swipeDispatched = false;
  8. this.holdDispatched = false;
  9.  
  10. this.isTouching = false;
  11. this.isHolding = false;
  12.  
  13. this.onSwipe = new Phaser.Signal();
  14. this.onTap = new Phaser.Signal();
  15. this.onHold = new Phaser.Signal();
  16.  
  17. }
  18.  
  19. Gesture.prototype.update = function() {
  20. var distance = Phaser.Point.distance(this.game.input.activePointer.position, this.game.input.activePointer.positionDown);
  21. var duration = this.game.input.activePointer.duration;
  22.  
  23. this.updateSwipe(distance, duration);
  24. this.updateTouch(distance, duration);
  25. };
  26.  
  27. Gesture.prototype.updateSwipe = function(distance, duration) {
  28. if (duration === -1) {
  29. this.swipeDispatched = false;
  30. } else if (!this.swipeDispatched && distance > 150 && duration > 100 && duration < Gesture.TIMES.SWIPE) {
  31. var positionDown = this.game.input.activePointer.positionDown;
  32. this.onSwipe.dispatch(this, positionDown);
  33.  
  34. this.swipeDispatched = true;
  35. }
  36. };
  37.  
  38. Gesture.prototype.updateTouch = function(distance, duration) {
  39. var positionDown = this.game.input.activePointer.positionDown;
  40.  
  41. if (duration === -1) {
  42. if (this.isTouching) {
  43. this.onTap.dispatch(this, positionDown);
  44. }
  45.  
  46. this.isTouching = false;
  47. this.isHolding = false;
  48. this.holdDispatched = false;
  49.  
  50. } else if (distance < 10) {
  51. if (duration < Gesture.TIMES.HOLD) {
  52. this.isTouching = true;
  53. } else {
  54. this.isTouching = false;
  55. this.isHolding = true;
  56.  
  57. if (!this.holdDispatched) {
  58. this.holdDispatched = true;
  59.  
  60. this.onHold.dispatch(this, positionDown);
  61. }
  62. }
  63. } else {
  64. this.isTouching = false;
  65. this.isHolding = false;
  66. }
  67. };
  68.  
  69. Gesture.SWIPE = 0;
  70. Gesture.TAP = 1;
  71. Gesture.HOLD = 2;
  72.  
  73. Gesture.TIMES = {
  74. HOLD: 150,
  75. SWIPE: 250
  76. };
  77.  
  78. return Gesture;
  79. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement