CBall.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. function CBall(iXPos, iYPos, oSprite, iXDir, iYDir, iSpeed, bFire, iRadius) {
  2. var _oBall;
  3. var _oInfoData = {};
  4. var _vDirection;
  5. var _vUpVector;
  6. var _iRadius;
  7. var _iOffsetWallRight;
  8. var _iOffsetWallLeft;
  9. var _iOffsetWallUp;
  10. var _iOffsetWallDown;
  11. var _iOffsetWallDownDown;
  12. var _iOffsetFallDown;
  13. var _bDownWall;
  14. var _iSpeed;
  15. var _iSpeedRate;
  16. var _iRotation = 0;
  17. this._init = function () {
  18. if (!bFire) {
  19. _oBall = createBitmap(oSprite);
  20. } else
  21. {
  22. var oData = {
  23. images: [oSprite],
  24. // width, height & registration point of each sprite
  25. frames: {width: oSprite.width / 8, height: oSprite.height, regX: (oSprite.width / 2) / 8, regY: oSprite.height / 2},
  26. animations: {fire: [0, 7, "fire", 0.5]}
  27. };
  28. var oSpriteSheet = new createjs.SpriteSheet(oData);
  29. _oBall = createSprite(oSpriteSheet, "fire", (oSprite.width / 2) / 8, oSprite.height / 2, oSprite.width / 8, oSprite.height);
  30. }
  31. _oBall.x = iXPos;
  32. _oBall.y = iYPos;
  33. _iRadius = iRadius + OFFSET_RADIUS_BALL;
  34. _bDownWall = false;
  35. _vDirection = new CVector2(iXDir, iYDir);
  36. _vUpVector = new CVector2(0, -1);
  37. _oBall.regX = _iRadius;
  38. _oBall.regY = _iRadius;
  39. var iEdgeRightWidth = -OFFSETEDGERIGHT - _iRadius;
  40. var iEdgeLeftWidth = OFFSETEDGELEFT + _iRadius;
  41. var iEdgeDownWidth = -OFFSETEDGEDOWN - _iRadius;
  42. var iEdgeUpWidth = OFFSETEDGEUP + _iRadius;
  43. _iOffsetWallRight = CANVAS_WIDTH + iEdgeRightWidth;
  44. _iOffsetWallLeft = iEdgeLeftWidth;
  45. _iOffsetWallDown = CANVAS_HEIGHT + iEdgeDownWidth - _iRadius;
  46. _iOffsetWallUp = iEdgeUpWidth + _iRadius;
  47. _iOffsetFallDown = CANVAS_HEIGHT + _iRadius;
  48. _iOffsetWallDownDown = CANVAS_HEIGHT + iEdgeDownWidth - _iRadius + OFFSETPOWERUPFLOOR;
  49. _iSpeedRate = iSpeed;
  50. _iSpeed = BALL_SPEED * _iSpeedRate;
  51. s_oStage.addChild(_oBall);
  52. s_oStage.setChildIndex(_oBall,1);
  53. };
  54. this.changeDirection = function (iXDir, iYDir, bPadding, szHit) {
  55. if (bPadding === true) {
  56. _vDirection.set(iXDir, _vDirection.getY() * iYDir);
  57. }
  58. else
  59. {
  60. if (szHit === "base") {
  61. _vDirection.set(_vDirection.getX() * iXDir, iYDir);
  62. } else if (szHit === "side") {
  63. _vDirection.set(_vDirection.getX() * iXDir, _vDirection.getY() * iYDir);
  64. } else if (szHit === "launch") {
  65. _vDirection.set(iXDir, iYDir);
  66. }
  67. }
  68. };
  69. this.changeSprite = function (oSprite, szType) {
  70. var iXPos = _oBall.x;
  71. var iYPos = _oBall.y;
  72. if (szType === "fire") {
  73. s_oStage.removeChild(_oBall);
  74. _oBall = null;
  75. var oData = {
  76. images: [oSprite],
  77. // width, height & registration point of each sprite
  78. frames: {width: oSprite.width / 8, height: oSprite.height, regX: (oSprite.width / 2) / 8, regY: oSprite.height / 2},
  79. animations: {fire: [0, 7, "fire", 0.5]}
  80. };
  81. var oSpriteSheet = new createjs.SpriteSheet(oData);
  82. _oBall = createSprite(oSpriteSheet, "fire", (oSprite.width / 2) / 8, oSprite.height / 2, oSprite.width / 8, oSprite.height);
  83. _oBall.x = iXPos;
  84. _oBall.y = iYPos;
  85. s_oStage.addChild(_oBall);
  86. }
  87. else {
  88. s_oStage.removeChild(_oBall);
  89. _oBall = createBitmap(oSprite);
  90. _oBall.x = iXPos;
  91. _oBall.y = iYPos;
  92. _oBall.regX = _iRadius;
  93. _oBall.regY = _iRadius;
  94. s_oStage.addChild(_oBall);
  95. }
  96. this.changeRotation();
  97. s_oStage.setChildIndex(_oBall,1);
  98. };
  99. this.downFloor = function (bVal) {
  100. _bDownWall = bVal;
  101. };
  102. this.getDirectionY = function () {
  103. return _vDirection.getY();
  104. };
  105. this.getX = function () {
  106. return _oBall.x;
  107. };
  108. this.getY = function () {
  109. return _oBall.y;
  110. };
  111. this.setDirection = function (x, y) {
  112. _vDirection.normalize();
  113. _vDirection.set(x, y);
  114. this.changeRotation();
  115. };
  116. this.changeRotation = function () {
  117. _iRotation = Math.round(toDegree(_vDirection.angleBetweenVectors(_vUpVector)));
  118. // identify if high or low direction
  119. if (_vDirection.getY() > 0) {
  120. //DOWN
  121. _iRotation *= -1;
  122. if (_vDirection.getX() >= 0) {
  123. // R
  124. _iRotation *= -1;
  125. }
  126. } else {
  127. //UP
  128. if (_vDirection.getX() <= 0) {
  129. // L
  130. _iRotation *= -1;
  131. }
  132. }
  133. _oBall.rotation = _iRotation;
  134. };
  135. this.setPosition = function (iXPos, iYPos) {
  136. if (iXPos === null) {
  137. } else {
  138. _oBall.x = iXPos;
  139. }
  140. if (iYPos === null) {
  141. } else {
  142. _oBall.y = iYPos;
  143. }
  144. };
  145. this.setInfoData = function (szKey, oValue) {
  146. _oInfoData[szKey] = oValue;
  147. };
  148. this.getInfoData = function (szKey) {
  149. return _oInfoData[szKey];
  150. };
  151. this.resetTheIndex = function (iValue) {
  152. _oInfoData["ID"] = iValue;
  153. };
  154. this.unload = function () {
  155. s_oStage.removeChild(_oBall);
  156. s_oBall = null;
  157. };
  158. this.setVelocityRate = function (iValue) {
  159. _iSpeedRate = iValue;
  160. _iSpeed = BALL_SPEED * _iSpeedRate;
  161. };
  162. this.update = function () {
  163. _oBall.x += (_iSpeed * _vDirection.getX());
  164. _oBall.y += (_iSpeed * _vDirection.getY());
  165. if (_oBall.x >= _iOffsetWallRight) {
  166. _vDirection.set(_vDirection.getX() * -1, _vDirection.getY());
  167. this.changeRotation();
  168. _oBall.x -= 1;
  169. playSound("boing", 1, false);
  170. } else if (_oBall.x <= _iOffsetWallLeft) {
  171. _vDirection.set(_vDirection.getX() * -1, _vDirection.getY());
  172. this.changeRotation();
  173. _oBall.x += 1;
  174. playSound("boing", 1, false);
  175. } else if (_oBall.y >= _iOffsetWallDown && _bDownWall === true && _oBall.y < _iOffsetWallDownDown) {
  176. _vDirection.set(_vDirection.getX(), _vDirection.getY() * -1);
  177. this.changeRotation();
  178. playSound("boing", 1, false);
  179. } else if (_oBall.y <= _iOffsetWallUp) {
  180. _vDirection.set(_vDirection.getX(), _vDirection.getY() * -1);
  181. this.changeRotation();
  182. playSound("boing", 1, false);
  183. } else if (_oBall.y > _iOffsetFallDown) {
  184. s_oGame.loseBall(_oInfoData["ID"]);
  185. } else if (_oBall.x <= _iOffsetWallLeft - 1) {
  186. _oBall.x += 2;
  187. } else if (_oBall.x >= _iOffsetWallRight + 1) {
  188. _oBall.x -= 2;
  189. } else if (_oBall.y <= _iOffsetWallUp - 1) {
  190. _oBall.y += 2;
  191. }
  192. };
  193. s_oBall = this;
  194. this._init();
  195. }
  196. var s_oBall;