SnakeJoystickInput.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. var o0 = require('o0');
  2. var o0CC = require('o0CC');
  3. var o0Game = require('o0Game');
  4. cc.Class({
  5. extends: require('SnakeDirectionInput'),
  6. properties: {
  7. touchLocation: null,
  8. moveLocation: null,
  9. speedingButton:null,
  10. },
  11. // use this for initialization
  12. onLoad: function () {
  13. this._super();
  14. var self = this;
  15. this.speedingButton = o0CC.addScriptNode(this.control.snake.node,'o0CCButton',10);
  16. o0CC.setGroup(this.speedingButton,o0Game.GroupIndex.UI);
  17. this.speedingButton.node.x = this.control.snake.gameScene.canvas.node.width/2-150;
  18. this.speedingButton.node.y = 0;
  19. this.speedingButton.node.width = 80;
  20. this.speedingButton.node.height = 80;/** */
  21. this.speedingButton.name = 'Boost';
  22. this.speedingButton.node.on(cc.Node.EventType.TOUCH_START, function (event) {
  23. self.setTargetSpeeding(true);
  24. }, this);
  25. this.speedingButton.node.on(cc.Node.EventType.TOUCH_END, function (event) {
  26. self.setTargetSpeeding(false);
  27. }, this);/** */
  28. var touchGraphic = o0CC.addScriptNode(this.control.snake.node,cc.Graphics,9);
  29. o0CC.setGroup(touchGraphic,o0Game.GroupIndex.UI);
  30. //touchGraphic.groupIndex = o0Game.GroupIndex.UI;
  31. touchGraphic.clear();
  32. touchGraphic.circle(0, 0, 25);
  33. touchGraphic.fillColor = new cc.Color(255,255,255,200);
  34. touchGraphic.fill();
  35. touchGraphic.node.x = -this.control.snake.gameScene.canvas.node.width/2+200;
  36. touchGraphic.node.y = -this.control.snake.gameScene.canvas.node.height/2+200;
  37. var moveGraphic = o0CC.addScriptNode(this.control.snake.node,cc.Graphics,10);
  38. o0CC.setGroup(moveGraphic,o0Game.GroupIndex.UI);
  39. //moveGraphic.groupIndex = o0Game.GroupIndex.UI;
  40. moveGraphic.clear();
  41. moveGraphic.circle(0, 0, 20);
  42. moveGraphic.fillColor = new cc.Color(255,255,255,150);
  43. moveGraphic.fill();
  44. moveGraphic.node.x = -this.control.snake.gameScene.canvas.node.width/2+200;
  45. moveGraphic.node.y = -this.control.snake.gameScene.canvas.node.height/2+200;
  46. this.touchLocation = new o0.Vector2(1,0);
  47. //TOUCH_ONE_BY_ONE
  48. //TOUCH_ALL_AT_ONCE
  49. var listener = {
  50. event: cc.EventListener.TOUCH_ONE_BY_ONE,
  51. onTouchBegan: function (touch, event) {
  52. if(self.control == null){
  53. return false;
  54. }
  55. self.touchLocation = touch.getLocation();
  56. var localTouchLocation = self.control.snake.node.parent.convertToNodeSpaceAR(self.touchLocation);
  57. touchGraphic.node.active = true;
  58. touchGraphic.node.x = localTouchLocation.x;
  59. touchGraphic.node.y = localTouchLocation.y;
  60. return true;
  61. },
  62. onTouchMoved: function (touch, event) {
  63. if(self.control == null){
  64. return false;
  65. }
  66. self.moveLocation = touch.getLocation();
  67. var localTouchLocation = self.control.snake.node.parent.convertToNodeSpaceAR(self.touchLocation);
  68. var localMoveLocation = self.control.snake.node.parent.convertToNodeSpaceAR(self.moveLocation);
  69. var vector = new o0.Vector2(localMoveLocation).minus(localTouchLocation);
  70. if(vector.length > 35){
  71. vector = vector.toLength(35);
  72. }
  73. moveGraphic.node.active = true;
  74. moveGraphic.node.x = touchGraphic.node.x + vector.x;
  75. moveGraphic.node.y = touchGraphic.node.y + vector.y;
  76. return true;
  77. },
  78. onTouchEnded: function (touch, event) {
  79. self.touchLocation = null;
  80. if(touchGraphic.node!=null){
  81. touchGraphic.node.active = false;
  82. }
  83. if(moveGraphic.node!=null){
  84. moveGraphic.node.active = false;
  85. }
  86. return true;
  87. },
  88. }
  89. cc.eventManager.addListener(listener, this.node);
  90. },
  91. start: function (dt) {
  92. },
  93. update: function (dt) {
  94. this._super();
  95. if(this.touchLocation == null || this.moveLocation == null){
  96. return;
  97. }
  98. var localTouchLocation = this.node.parent.convertToNodeSpaceAR(this.touchLocation);
  99. var localMoveLocation = this.node.parent.convertToNodeSpaceAR(this.moveLocation);
  100. this.setTargetVector(new o0.Vector2(localMoveLocation).minus(localTouchLocation).mod);
  101. },/*
  102. test:function(){
  103. cc.log('mouse snake');
  104. },/** */
  105. });