Stone.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { _decorator, Component, instantiate, Node, Prefab, randomRangeInt, RigidBody2D, Sprite, SpriteFrame, Vec2 } from 'cc';
  2. import { endPos, UI_Main } from '../../../ui/main/UI_Main';
  3. import { GameState } from '../../GameState';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('Stone')
  6. export class Stone extends Component {
  7. state: number
  8. rigt: RigidBody2D
  9. @property([SpriteFrame])
  10. stoneSkin1: SpriteFrame[] = [];
  11. @property([SpriteFrame])
  12. stoneSkin2: SpriteFrame[] = [];
  13. @property([SpriteFrame])
  14. stoneSkin3: SpriteFrame[] = [];
  15. @property([SpriteFrame])
  16. stoneSkin4: SpriteFrame[] = [];
  17. private uiMain: UI_Main | null = null;
  18. stoneSpriteFrame: SpriteFrame | null = null;
  19. start() {
  20. this.checkStonePosition();
  21. // this.node.getComponent(Sprite).spriteFrame = GameState.inst.spf
  22. }
  23. // 初始化方法,接收精灵帧
  24. init(uiMain: UI_Main) {
  25. this.uiMain = uiMain;
  26. }
  27. // // 初始化方法,接收精灵帧
  28. // init(uiMain: UI_Main, spriteFrame: SpriteFrame) {
  29. // this.uiMain = uiMain;
  30. // this.stoneSpriteFrame = spriteFrame;
  31. // // 立即设置精灵帧
  32. // this.node.getComponent(Sprite).spriteFrame = spriteFrame;
  33. // }
  34. update(dt: number) {
  35. // 每帧检查石头的位置
  36. this.checkStonePosition();
  37. }
  38. // 重置石头状态
  39. reset() {
  40. this.node.active = true;
  41. const rigidBody = this.node.getComponent(RigidBody2D);
  42. if (rigidBody) {
  43. rigidBody.linearVelocity = Vec2.ZERO;
  44. rigidBody.angularVelocity = 0;
  45. }
  46. }
  47. private checkStonePosition() {
  48. if (this.node.position.y < endPos.ballEndY && this.uiMain) {
  49. // 使用回收代替销毁
  50. this.uiMain.recycleStone(this.node);
  51. }
  52. }
  53. }