BallItem.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { _decorator, Collider2D, Component, Contact2DType, IPhysics2DContact, log, Node, tween, UIOpacity, v3 } from 'cc';
  2. import { ColliderTag, Global } from '../Common/Global';
  3. import { GameManager } from '../Manager/GameManager';
  4. import { AudioManage } from '../Manager/AudioManage';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('BallItem')
  7. export class BallItem extends Component {
  8. _index:number;
  9. start() {
  10. let collider = this.getComponent(Collider2D);
  11. if (collider) {
  12. collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
  13. }
  14. }
  15. // onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
  16. // // 只在两个碰撞体开始接触时被调用一次
  17. // console.log('onBeginContact');
  18. // }
  19. //碰撞事件
  20. onBeginContact(self: Collider2D, other: Collider2D) {
  21. if (other.tag == ColliderTag.GouLanzi) {
  22. if (self.tag == ColliderTag.TargetBall) {
  23. GameManager.getInstance().showTips("太棒了")
  24. AudioManage.instance.playSound(Global.Audio_AddScore);
  25. tween(self.node).to(0.1, { scale: v3() }).call(() => {
  26. self.node.destroy();
  27. GameManager.getInstance().addScore(10);
  28. }).start();
  29. } else if (self.tag == ColliderTag.WriteBall) {
  30. GameManager.getInstance().showTips("请继续加油哦")
  31. AudioManage.instance.playSound(Global.Audio_MinusScore);
  32. tween(self.getComponent(UIOpacity)).to(0.1, { opacity: 0 }).to(0.1, { opacity: 255 }).to(0.1, { opacity: 0 }).call(() => {
  33. self.node.destroy();
  34. GameManager.getInstance().minusScore(10);
  35. }).start();
  36. // self.node.destroy();
  37. // GameManager.getInstance().minusScore(10);
  38. }
  39. }
  40. }
  41. update(deltaTime: number) {
  42. }
  43. }