CCKeyboardDelegate.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright (c) 2011 Zynga Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * you must extend the keyboardDelegate and
  24. * implement your own game logic in
  25. * keydown and keyup functions
  26. * @class
  27. * @extends cc.Class
  28. */
  29. cc.KeyboardDelegate = cc.Class.extend(/** @lends cc.KeyboardDelegate# */{
  30. /**
  31. * Call back when a key is pressed down
  32. * @param {Integer} keyCode
  33. * @example
  34. * // example
  35. * if(keyCode == cc.KEY.w){}
  36. */
  37. onKeyDown:function (keyCode) {
  38. },
  39. /**
  40. * Call back when a key is released
  41. * @param {Integer} keyCode
  42. * @example
  43. * // example
  44. * if(keyCode == cc.KEY.w){}
  45. */
  46. onKeyUp:function (keyCode) {
  47. }
  48. });
  49. /**
  50. * KeyboardHandler is an object that contains KeyboardDelegate
  51. * @class
  52. * @extends cc.Class
  53. */
  54. cc.KeyboardHandler = cc.Class.extend(/** @lends cc.KeyboardHandler# */{
  55. /**
  56. * returns the keyboard delegate
  57. * @return {cc.KeyboardDelegate}
  58. */
  59. getDelegate:function () {
  60. return this._delegate;
  61. },
  62. /**
  63. * set the keyboard delegate
  64. * @param {cc.KeyboardDelegate} delegate
  65. */
  66. setDelegate:function (delegate) {
  67. this._delegate = delegate;
  68. },
  69. /**
  70. * initializes a cc.KeyboardHandler with a delegate
  71. * @param {cc.KeyboardDelegate} delegate
  72. * @return {Boolean}
  73. */
  74. initWithDelegate:function (delegate) {
  75. if(!delegate)
  76. throw "cc.KeyboardHandler.initWithDelegate(): delegate must be non-null";
  77. this._delegate = delegate;
  78. return true;
  79. },
  80. _delegate:null
  81. });
  82. /**
  83. * Create a KeyboardHandler with KeyboardDelegate
  84. * @param delegate
  85. * @return {cc.KeyboardHandler}
  86. */
  87. cc.KeyboardHandler.create = function (delegate) {
  88. var handler = new cc.KeyboardHandler();
  89. handler.initWithDelegate(delegate);
  90. return handler;
  91. };