trinButton.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. function TrinButton(onClick, animationName, opensLink) {
  2. TrinButton.superclass.constructor.apply(this);
  3. if (opensLink === undefined) {
  4. opensLink = false;
  5. }
  6. this.onClick = onClick;
  7. this.addAnimationFromCache(animationName);
  8. this.animationSpeed = 0;
  9. this.opensLink = opensLink;
  10. this.onCanvasClick = null;
  11. if (this.opensLink) {
  12. this.onCanvasClick = function(event) {
  13. var button = arguments.callee.button;
  14. if (button.onClick !== undefined && button.onClick !== null && button.active &&
  15. button.bounds.intersects(_TrinGame.mouse.x, _TrinGame.mouse.y)) {
  16. button.onClick();
  17. }
  18. };
  19. this.onCanvasClick.button = this;
  20. if (_TrinGame.isMobile) {
  21. _TrinGame.canvas.addEventListener(this.touchEventName, this.onCanvasClick, false);
  22. } else {
  23. _TrinGame.canvas.addEventListener(this.clickEventName, this.onCanvasClick, false);
  24. }
  25. }
  26. }
  27. extend(TrinButton, TrinSprite);
  28. TrinButton.prototype.update = function() {
  29. TrinButton.superclass.update.apply(this);
  30. if (this.active) {
  31. if (this.bounds.intersects(_TrinGame.mouse.x, _TrinGame.mouse.y)) {
  32. if (this.currentAnimation.frames.length > 1) {
  33. this.currentFrame = 1;
  34. }
  35. if (_TrinGame.mouse.isReleased()) {
  36. if (this.currentAnimation.frames.length > 2) {
  37. this.currentFrame = 2;
  38. } else {
  39. this.currentFrame = 0;
  40. }
  41. if (!this.opensLink && this.onClick !== undefined && this.onClick !== null) {
  42. this.onClick();
  43. }
  44. }
  45. } else {
  46. this.currentFrame = 0;
  47. }
  48. this.updateSize();
  49. }
  50. };
  51. TrinButton.prototype.clickEventName = "click";
  52. TrinButton.prototype.touchEventName = "touchend";
  53. TrinButton.prototype.destroy = function() {
  54. TrinButton.superclass.destroy.apply(this);
  55. if (this.opensLink) {
  56. if (_TrinGame.isMobile) {
  57. _TrinGame.canvas.removeEventListener(this.touchEventName, this.onCanvasClick, false);
  58. } else {
  59. _TrinGame.canvas.removeEventListener(this.clickEventName, this.onCanvasClick, false);
  60. }
  61. }
  62. };
  63. TrinButton.prototype.isHovered = function() {
  64. return this.currentFrame > 0;
  65. };