BallController.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. import { _decorator, Component, Node, Vec2, Vec3, UITransform, Collider2D, Contact2DType, IPhysics2DContact, RigidBody2D, Prefab, instantiate, find, CircleCollider2D } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('BallController')
  4. export class BallController extends Component {
  5. // 球的预制体
  6. @property({
  7. type: Prefab,
  8. tooltip: '拖拽Ball预制体到这里'
  9. })
  10. public ballPrefab: Prefab = null;
  11. // 球的移动速度
  12. @property
  13. public speed: number = 50;
  14. // 当前活动的球
  15. private activeBall: Node = null;
  16. // 球的方向向量
  17. private direction: Vec2 = new Vec2();
  18. // GameArea区域边界
  19. private gameBounds = {
  20. left: 0,
  21. right: 0,
  22. top: 0,
  23. bottom: 0
  24. };
  25. // 球的半径
  26. private radius: number = 0;
  27. // 是否已初始化
  28. private initialized: boolean = false;
  29. // 子弹预制体
  30. @property({
  31. type: Prefab,
  32. tooltip: '拖拽子弹预制体到这里'
  33. })
  34. public bulletPrefab: Prefab = null;
  35. start() {
  36. // 计算游戏边界
  37. this.calculateGameBounds();
  38. // 检查子弹预制体是否已设置
  39. if (this.bulletPrefab) {
  40. console.log('子弹预制体已设置:', this.bulletPrefab.name);
  41. } else {
  42. console.error('子弹预制体未设置!请在编辑器中设置bulletPrefab属性');
  43. }
  44. }
  45. // 计算游戏边界(使用GameArea节点)
  46. calculateGameBounds() {
  47. // 获取GameArea节点
  48. const gameArea = find('Canvas/GameLevelUI/GameArea');
  49. if (!gameArea) {
  50. console.error('找不到GameArea节点');
  51. return;
  52. }
  53. const gameAreaUI = gameArea.getComponent(UITransform);
  54. if (!gameAreaUI) {
  55. console.error('GameArea节点没有UITransform组件');
  56. return;
  57. }
  58. // 获取GameArea的尺寸
  59. const areaWidth = gameAreaUI.width;
  60. const areaHeight = gameAreaUI.height;
  61. // 获取GameArea的世界坐标位置
  62. const worldPos = gameArea.worldPosition;
  63. // 计算GameArea的世界坐标边界
  64. this.gameBounds.left = worldPos.x - areaWidth / 2;
  65. this.gameBounds.right = worldPos.x + areaWidth / 2;
  66. this.gameBounds.bottom = worldPos.y - areaHeight / 2;
  67. this.gameBounds.top = worldPos.y + areaHeight / 2;
  68. console.log('GameArea Bounds:', this.gameBounds);
  69. }
  70. // 创建小球
  71. createBall() {
  72. if (!this.ballPrefab) {
  73. console.error('未设置Ball预制体');
  74. return;
  75. }
  76. // 如果已经有活动的球,先销毁它
  77. if (this.activeBall && this.activeBall.isValid) {
  78. this.activeBall.destroy();
  79. }
  80. // 实例化小球
  81. this.activeBall = instantiate(this.ballPrefab);
  82. // 将小球添加到GameArea中
  83. const gameArea = find('Canvas/GameLevelUI/GameArea');
  84. if (gameArea) {
  85. gameArea.addChild(this.activeBall);
  86. } else {
  87. this.node.addChild(this.activeBall);
  88. }
  89. // 随机位置(在GameArea范围内)
  90. this.positionBallRandomly();
  91. // 设置球的半径
  92. const transform = this.activeBall.getComponent(UITransform);
  93. if (transform) {
  94. this.radius = transform.width / 2;
  95. console.log('小球半径设置为:', this.radius);
  96. } else {
  97. this.radius = 25; // 默认半径
  98. console.warn('无法获取小球UITransform组件,使用默认半径:', this.radius);
  99. }
  100. // 确保有碰撞组件
  101. this.setupCollider();
  102. // 初始化方向
  103. this.initializeDirection();
  104. this.initialized = true;
  105. console.log('小球创建完成:', {
  106. position: this.activeBall.position,
  107. radius: this.radius,
  108. initialized: this.initialized
  109. });
  110. }
  111. // 随机位置小球
  112. positionBallRandomly() {
  113. if (!this.activeBall) return;
  114. const transform = this.activeBall.getComponent(UITransform);
  115. const ballRadius = transform ? transform.width / 2 : 25;
  116. // 计算可生成的范围(考虑小球半径,避免生成在边缘)
  117. const minX = this.gameBounds.left + ballRadius + 20; // 额外偏移,避免生成在边缘
  118. const maxX = this.gameBounds.right - ballRadius - 20;
  119. const minY = this.gameBounds.bottom + ballRadius + 20;
  120. const maxY = this.gameBounds.top - ballRadius - 20;
  121. // 随机生成位置
  122. const randomX = Math.random() * (maxX - minX) + minX;
  123. const randomY = Math.random() * (maxY - minY) + minY;
  124. // 将世界坐标转换为相对于GameArea的本地坐标
  125. const gameArea = find('Canvas/GameLevelUI/GameArea');
  126. if (gameArea) {
  127. const localPos = gameArea.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(randomX, randomY, 0));
  128. this.activeBall.position = localPos;
  129. } else {
  130. // 直接设置位置(不太准确,但作为后备)
  131. this.activeBall.position = new Vec3(randomX - this.gameBounds.left, randomY - this.gameBounds.bottom, 0);
  132. }
  133. }
  134. // 设置碰撞组件
  135. setupCollider() {
  136. if (!this.activeBall) return;
  137. // 确保小球有刚体组件
  138. let rigidBody = this.activeBall.getComponent(RigidBody2D);
  139. if (!rigidBody) {
  140. rigidBody = this.activeBall.addComponent(RigidBody2D);
  141. rigidBody.type = 2; // Dynamic
  142. rigidBody.gravityScale = 0; // 不受重力影响
  143. rigidBody.enabledContactListener = true; // 启用碰撞监听
  144. rigidBody.fixedRotation = true; // 固定旋转
  145. rigidBody.allowSleep = false; // 不允许休眠
  146. rigidBody.linearDamping = 0; // 无阻尼
  147. rigidBody.angularDamping = 0; // 无角阻尼
  148. } else {
  149. // 确保已有的刚体组件设置正确
  150. rigidBody.enabledContactListener = true;
  151. rigidBody.gravityScale = 0;
  152. }
  153. // 确保小球有碰撞组件
  154. let collider = this.activeBall.getComponent(CircleCollider2D);
  155. if (!collider) {
  156. collider = this.activeBall.addComponent(CircleCollider2D);
  157. collider.radius = this.radius || 25; // 使用已计算的半径或默认值
  158. collider.tag = 1; // 小球标签
  159. collider.group = 1; // 碰撞组
  160. collider.sensor = false; // 非传感器(实际碰撞)
  161. collider.friction = 0; // 无摩擦
  162. collider.restitution = 1; // 完全弹性碰撞
  163. } else {
  164. // 确保已有的碰撞组件设置正确
  165. collider.sensor = false;
  166. collider.restitution = 1;
  167. }
  168. console.log('小球碰撞组件设置完成:', {
  169. hasRigidBody: !!rigidBody,
  170. hasCollider: !!collider,
  171. radius: collider ? collider.radius : 'unknown'
  172. });
  173. // 移除可能存在的事件监听
  174. this.activeBall.off(Contact2DType.BEGIN_CONTACT);
  175. this.activeBall.off(Contact2DType.END_CONTACT);
  176. this.activeBall.off(Contact2DType.PRE_SOLVE);
  177. this.activeBall.off(Contact2DType.POST_SOLVE);
  178. // 注册所有类型的碰撞事件
  179. this.activeBall.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
  180. this.activeBall.on(Contact2DType.END_CONTACT, this.onEndContact, this);
  181. this.activeBall.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
  182. this.activeBall.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
  183. console.log('已注册所有碰撞事件监听器');
  184. }
  185. // 碰撞回调中,只需要更新方向向量,物理引擎会处理实际的反弹
  186. onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
  187. // 获取碰撞点和法线
  188. if (!contact) {
  189. console.warn('碰撞事件没有contact对象');
  190. return;
  191. }
  192. // 记录碰撞对象信息
  193. console.log('碰撞发生:', {
  194. selfName: selfCollider.node.name,
  195. otherName: otherCollider.node.name,
  196. otherPath: this.getNodePath(otherCollider.node)
  197. });
  198. // Debug: 检查节点名称是否包含Block
  199. console.log(`检查节点名称: "${otherCollider.node.name}",是否包含'Block': ${otherCollider.node.name.includes('Block')}`);
  200. // 获取碰撞点的世界坐标
  201. const worldManifold = contact.getWorldManifold();
  202. if (!worldManifold) {
  203. console.warn('无法获取worldManifold');
  204. return;
  205. }
  206. // 获取碰撞法线
  207. const normal = worldManifold.normal;
  208. console.log('碰撞法线:', normal);
  209. // 检查是否碰到墙体
  210. const nodeName = otherCollider.node.name;
  211. const nodePath = this.getNodePath(otherCollider.node);
  212. const nodeParent = otherCollider.node.parent ? otherCollider.node.parent.name : 'unknown';
  213. console.log(`检查墙体碰撞: ${nodeName}, 路径: ${nodePath}, 父节点: ${nodeParent}`);
  214. // 使用多种方式检测墙体碰撞
  215. const isWall =
  216. nodeName === 'TopFence' ||
  217. nodeName === 'BottomFence' ||
  218. nodeName === 'JiguangL' ||
  219. nodeName === 'JiguangR' ||
  220. nodeName.includes('Fence') ||
  221. nodeName.includes('Jiguang') ||
  222. nodePath.includes('GameArea/TopFence') ||
  223. nodePath.includes('GameArea/BottomFence') ||
  224. nodePath.includes('GameArea/JiguangL') ||
  225. nodePath.includes('GameArea/JiguangR');
  226. if (isWall) {
  227. console.log(`球碰到了墙体 ${nodeName},进行反弹,路径: ${nodePath}`);
  228. // 更新方向向量
  229. this.direction = this.calculateReflection(this.direction, normal);
  230. // 确保球有刚体组件
  231. const rigidBody = this.activeBall.getComponent(RigidBody2D);
  232. if (rigidBody) {
  233. // 立即更新速度方向,确保反弹效果
  234. rigidBody.linearVelocity = new Vec2(
  235. this.direction.x * this.speed,
  236. this.direction.y * this.speed
  237. );
  238. console.log('更新后的速度:', rigidBody.linearVelocity);
  239. // 强制设置位置,避免卡在墙内
  240. const ballPos = this.activeBall.worldPosition;
  241. const ballRadius = this.radius;
  242. // 根据碰撞法线调整位置
  243. if (Math.abs(normal.y) > Math.abs(normal.x)) {
  244. // 上下墙体碰撞
  245. if (normal.y > 0) {
  246. // 下墙
  247. this.activeBall.setWorldPosition(new Vec3(
  248. ballPos.x,
  249. ballPos.y + ballRadius * 0.5,
  250. ballPos.z
  251. ));
  252. } else {
  253. // 上墙
  254. this.activeBall.setWorldPosition(new Vec3(
  255. ballPos.x,
  256. ballPos.y - ballRadius * 0.5,
  257. ballPos.z
  258. ));
  259. }
  260. } else {
  261. // 左右墙体碰撞
  262. if (normal.x > 0) {
  263. // 左墙
  264. this.activeBall.setWorldPosition(new Vec3(
  265. ballPos.x + ballRadius * 0.5,
  266. ballPos.y,
  267. ballPos.z
  268. ));
  269. } else {
  270. // 右墙
  271. this.activeBall.setWorldPosition(new Vec3(
  272. ballPos.x - ballRadius * 0.5,
  273. ballPos.y,
  274. ballPos.z
  275. ));
  276. }
  277. }
  278. }
  279. }
  280. // 如果碰撞的是方块,发射子弹
  281. // 修改检测逻辑,使用更宽松的条件
  282. const isBlock =
  283. nodeName.includes('Block') ||
  284. nodePath.includes('Block') ||
  285. (otherCollider.node.getChildByName('Weapon') !== null);
  286. if (isBlock) {
  287. console.log(`检测到方块碰撞: ${nodeName}, 路径: ${nodePath}`);
  288. this.fireBullet(otherCollider.node);
  289. }
  290. }
  291. // 碰撞结束事件
  292. onEndContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
  293. console.log('碰撞结束:', otherCollider.node.name);
  294. }
  295. // 碰撞预处理事件
  296. onPreSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
  297. console.log('碰撞预处理:', otherCollider.node.name);
  298. }
  299. // 碰撞后处理事件
  300. onPostSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
  301. console.log('碰撞后处理:', otherCollider.node.name);
  302. // 检查是否碰到墙体
  303. const nodeName = otherCollider.node.name;
  304. if (nodeName === 'TopFence' || nodeName === 'BottomFence' ||
  305. nodeName === 'JiguangL' || nodeName === 'JiguangR' ||
  306. nodeName.includes('Fence') || nodeName.includes('Jiguang')) {
  307. console.log(`球碰到了墙体 ${nodeName},处理反弹`);
  308. // 获取碰撞点和法线
  309. if (!contact) return;
  310. const worldManifold = contact.getWorldManifold();
  311. if (!worldManifold) return;
  312. // 获取碰撞法线
  313. const normal = worldManifold.normal;
  314. // 更新方向向量
  315. this.direction = this.calculateReflection(this.direction, normal);
  316. // 确保球有刚体组件
  317. const rigidBody = this.activeBall.getComponent(RigidBody2D);
  318. if (rigidBody) {
  319. // 立即更新速度方向,确保反弹效果
  320. rigidBody.linearVelocity = new Vec2(
  321. this.direction.x * this.speed,
  322. this.direction.y * this.speed
  323. );
  324. }
  325. }
  326. }
  327. // 计算反射向量
  328. calculateReflection(direction: Vec2, normal: Vec2): Vec2 {
  329. // 使用反射公式: R = V - 2(V·N)N
  330. const dot = direction.x * normal.x + direction.y * normal.y;
  331. const reflection = new Vec2(
  332. direction.x - 2 * dot * normal.x,
  333. direction.y - 2 * dot * normal.y
  334. );
  335. reflection.normalize();
  336. // 添加一些随机性,避免重复的反弹路径
  337. const randomAngle = (Math.random() - 0.5) * 0.2; // -0.1到0.1弧度的随机角度
  338. const cos = Math.cos(randomAngle);
  339. const sin = Math.sin(randomAngle);
  340. // 应用随机旋转
  341. const randomizedReflection = new Vec2(
  342. reflection.x * cos - reflection.y * sin,
  343. reflection.x * sin + reflection.y * cos
  344. );
  345. randomizedReflection.normalize();
  346. console.log('反射方向:', {
  347. original: direction,
  348. normal: normal,
  349. reflection: reflection,
  350. randomized: randomizedReflection
  351. });
  352. return randomizedReflection;
  353. }
  354. // 发射子弹
  355. fireBullet(blockNode: Node) {
  356. console.log('发射子弹!击中方块:', blockNode.name);
  357. // 检查是否有子弹预制体
  358. if (!this.bulletPrefab) {
  359. console.error('未设置子弹预制体');
  360. return;
  361. } else {
  362. console.log('子弹预制体已设置:', this.bulletPrefab.name);
  363. }
  364. // 查找Weapon节点
  365. const weaponNode = blockNode.getChildByName('Weapon');
  366. if (!weaponNode) {
  367. console.warn(`方块 ${blockNode.name} 没有Weapon子节点`);
  368. return;
  369. } else {
  370. console.log(`找到方块 ${blockNode.name} 的Weapon子节点:`, weaponNode.name);
  371. }
  372. // 实例化子弹
  373. const bullet = instantiate(this.bulletPrefab);
  374. // 将子弹添加到GameArea中
  375. const gameArea = find('Canvas/GameLevelUI/GameArea');
  376. if (gameArea) {
  377. gameArea.addChild(bullet);
  378. // 设置子弹初始位置为Weapon节点的位置
  379. const weaponWorldPos = weaponNode.worldPosition;
  380. bullet.worldPosition = weaponWorldPos;
  381. // 计算子弹方向 - 从Weapon指向小球
  382. const ballPos = this.activeBall.worldPosition;
  383. const direction = new Vec2(
  384. ballPos.x - weaponWorldPos.x,
  385. ballPos.y - weaponWorldPos.y
  386. );
  387. direction.normalize();
  388. // 添加自动销毁逻辑
  389. this.scheduleOnce(() => {
  390. if (bullet && bullet.isValid) {
  391. bullet.destroy();
  392. }
  393. }, 5); // 5秒后销毁子弹
  394. }
  395. }
  396. // 获取节点的完整路径
  397. private getNodePath(node: Node): string {
  398. let path = node.name;
  399. let current = node;
  400. while (current.parent) {
  401. current = current.parent;
  402. path = current.name + '/' + path;
  403. }
  404. return path;
  405. }
  406. update(dt: number) {
  407. // 如果使用物理引擎,不要手动更新位置
  408. if (!this.initialized || !this.activeBall || !this.activeBall.isValid) return;
  409. // 使用刚体组件控制小球移动,而不是直接设置位置
  410. const rigidBody = this.activeBall.getComponent(RigidBody2D);
  411. if (rigidBody) {
  412. // 检查速度是否过小或为零,如果是则恢复速度
  413. const currentVelocity = rigidBody.linearVelocity;
  414. const speed = Math.sqrt(currentVelocity.x * currentVelocity.x + currentVelocity.y * currentVelocity.y);
  415. if (speed < 1.0) {
  416. console.log('检测到速度过小,恢复速度:', speed);
  417. // 如果速度太小,恢复到正常速度
  418. this.direction.normalize();
  419. rigidBody.linearVelocity = new Vec2(
  420. this.direction.x * this.speed,
  421. this.direction.y * this.speed
  422. );
  423. }
  424. // 确保方向向量与实际速度方向一致
  425. if (speed > 1.0) {
  426. this.direction.x = currentVelocity.x / speed;
  427. this.direction.y = currentVelocity.y / speed;
  428. }
  429. // 手动检测墙体碰撞
  430. this.checkWallCollisions();
  431. }
  432. // 手动检测方块碰撞
  433. this.checkBlockCollisions();
  434. }
  435. // 手动检测墙体碰撞
  436. checkWallCollisions() {
  437. if (!this.activeBall || !this.activeBall.isValid) return;
  438. // 获取小球的世界坐标
  439. const ballPos = this.activeBall.worldPosition;
  440. const ballRadius = this.radius;
  441. // 检查是否碰到上下左右边界
  442. let collided = false;
  443. let normal = new Vec2(0, 0);
  444. // 上边界碰撞
  445. if (ballPos.y + ballRadius >= this.gameBounds.top) {
  446. normal.y = -1;
  447. collided = true;
  448. console.log('手动检测到上边界碰撞');
  449. // 修正位置,避免卡在边界
  450. this.activeBall.setWorldPosition(new Vec3(
  451. ballPos.x,
  452. this.gameBounds.top - ballRadius - 0.1,
  453. ballPos.z
  454. ));
  455. }
  456. // 下边界碰撞
  457. else if (ballPos.y - ballRadius <= this.gameBounds.bottom) {
  458. normal.y = 1;
  459. collided = true;
  460. console.log('手动检测到下边界碰撞');
  461. // 修正位置,避免卡在边界
  462. this.activeBall.setWorldPosition(new Vec3(
  463. ballPos.x,
  464. this.gameBounds.bottom + ballRadius + 0.1,
  465. ballPos.z
  466. ));
  467. }
  468. // 左边界碰撞
  469. if (ballPos.x - ballRadius <= this.gameBounds.left) {
  470. normal.x = 1;
  471. collided = true;
  472. console.log('手动检测到左边界碰撞');
  473. // 修正位置,避免卡在边界
  474. this.activeBall.setWorldPosition(new Vec3(
  475. this.gameBounds.left + ballRadius + 0.1,
  476. ballPos.y,
  477. ballPos.z
  478. ));
  479. }
  480. // 右边界碰撞
  481. else if (ballPos.x + ballRadius >= this.gameBounds.right) {
  482. normal.x = -1;
  483. collided = true;
  484. console.log('手动检测到右边界碰撞');
  485. // 修正位置,避免卡在边界
  486. this.activeBall.setWorldPosition(new Vec3(
  487. this.gameBounds.right - ballRadius - 0.1,
  488. ballPos.y,
  489. ballPos.z
  490. ));
  491. }
  492. // 如果碰到边界,计算反弹
  493. if (collided) {
  494. // 标准化法向量
  495. if (normal.x !== 0 || normal.y !== 0) {
  496. normal.normalize();
  497. }
  498. // 计算反射方向
  499. this.direction = this.calculateReflection(this.direction, normal);
  500. // 更新速度
  501. const rigidBody = this.activeBall.getComponent(RigidBody2D);
  502. if (rigidBody) {
  503. // 立即更新速度方向,确保反弹效果
  504. rigidBody.linearVelocity = new Vec2(
  505. this.direction.x * this.speed,
  506. this.direction.y * this.speed
  507. );
  508. console.log('手动更新速度方向:', this.direction);
  509. }
  510. }
  511. }
  512. // 手动检测方块碰撞
  513. checkBlockCollisions() {
  514. if (!this.activeBall) return;
  515. // 获取场景中所有方块
  516. const placedBlocks = find('Canvas/PlacedBlocks');
  517. if (!placedBlocks) return;
  518. const ballPos = this.activeBall.worldPosition;
  519. const ballRadius = this.radius;
  520. // 检查与每个方块的碰撞
  521. for (let i = 0; i < placedBlocks.children.length; i++) {
  522. const block = placedBlocks.children[i];
  523. if (!block.name.includes('Block')) continue;
  524. // 简单距离检测(可以根据实际情况优化)
  525. const blockPos = block.worldPosition;
  526. const distance = Vec3.distance(ballPos, blockPos);
  527. // 如果距离小于某个阈值,认为发生碰撞
  528. if (distance < ballRadius + 50) { // 50是一个估计值,根据方块大小调整
  529. console.log('手动检测到与方块碰撞:', block.name);
  530. this.fireBullet(block);
  531. break;
  532. }
  533. }
  534. }
  535. // 初始化方向
  536. initializeDirection() {
  537. // 随机初始方向
  538. const angle = Math.random() * Math.PI * 2; // 0-2π之间的随机角度
  539. this.direction.x = Math.cos(angle);
  540. this.direction.y = Math.sin(angle);
  541. this.direction.normalize();
  542. console.log('Ball initialized with direction:', this.direction);
  543. }
  544. // 初始化球的参数 - 公开方法,供GameManager调用
  545. initialize() {
  546. this.calculateGameBounds();
  547. this.createBall();
  548. }
  549. }