VibrationManager.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { sys } from "cc";
  2. // VibrationManager.ts
  3. export class VibrationManager {
  4. private static instance: VibrationManager;
  5. private _switch_vibration: boolean = true;
  6. /**
  7. * 获取震动开关值
  8. */
  9. get switchVibration(): boolean {
  10. return this._switch_vibration;
  11. }
  12. /**
  13. * 设置震动开关值
  14. * @param value 开关值
  15. */
  16. set switchVibration(value: boolean) {
  17. this._switch_vibration = value;
  18. }
  19. private constructor() {}
  20. // 获取AdManager的单例对象
  21. public static getInstance(): VibrationManager {
  22. if (!VibrationManager.instance) {
  23. VibrationManager.instance = new VibrationManager();
  24. }
  25. return VibrationManager.instance;
  26. }
  27. // 震动(短)
  28. vibrateShort() {
  29. if(!this.switchVibration)return
  30. if (sys.platform === sys.Platform.WECHAT_GAME) {
  31. // 微信小游戏环境
  32. wx.vibrateShort({
  33. success: () => console.log("Vibrate short success"),
  34. fail: () => console.error("Vibrate short failed"),
  35. });
  36. } else if (sys.platform === sys.Platform.BYTEDANCE_MINI_GAME) {
  37. // 抖音小游戏环境
  38. tt.vibrateShort({
  39. success: () => console.log("Vibrate short success"),
  40. fail: () => console.error("Vibrate short failed"),
  41. });
  42. } else {
  43. console.log("Vibrate short called in unsupported platform");
  44. }
  45. }
  46. // 震动(长)
  47. vibrateLong() {
  48. if (sys.platform === sys.Platform.WECHAT_GAME) {
  49. // 微信小游戏环境
  50. wx.vibrateLong({
  51. success: () => console.log("Vibrate long success"),
  52. fail: () => console.error("Vibrate long failed"),
  53. });
  54. } else if (sys.platform === sys.Platform.BYTEDANCE_MINI_GAME) {
  55. // 抖音小游戏环境
  56. tt.vibrateLong({
  57. success: () => console.log("Vibrate long success"),
  58. fail: () => console.error("Vibrate long failed"),
  59. });
  60. } else {
  61. console.log("Vibrate long called in unsupported platform");
  62. }
  63. }
  64. }