OptionsCtr.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { _decorator, color, Color, Component, Label, Node, Sprite, tween, UIOpacity, Vec3 } from "cc"
  2. const { ccclass, property } = _decorator
  3. @ccclass("OptionsCtr")
  4. export class OptionsCtr extends Component {
  5. // 直接引用四个子节点
  6. @property({ type: Node })
  7. public optionNodes: Node[] = []
  8. @property({ type: Color })
  9. selectedColor: Color = new Color(180, 173, 121) // B4AD79
  10. @property({ type: Sprite })
  11. selectedSprite: Sprite = null // 设置你想要的选中背景图
  12. @property({ type: Sprite })
  13. notSelectedSprite: Sprite = null // 设置你想要的选中背景图
  14. start() {}
  15. setOptions(options: string[]) {
  16. if (options.length !== 4) {
  17. console.warn("Expected 4 options!")
  18. return
  19. }
  20. //显示前先制空lebel
  21. for(let i=0;i<4;i++)
  22. {
  23. const nodeLabel = this.optionNodes[i].getChildByName("text_option").getComponent(Label);
  24. nodeLabel.string = ''
  25. }
  26. tween(this.node.getComponent(UIOpacity))
  27. .to(1, { opacity: 255 })
  28. .call(() => {
  29. for (let i = 0; i < 4; i++) {
  30. this.setOptionText(this.optionNodes[i], options[i])
  31. }
  32. })
  33. .start()
  34. }
  35. setOptionText(node: Node, text: string) {
  36. const labelComponent = node.getChildByName("text_option").getComponent(Label)
  37. const uiOpacity = labelComponent.node.getComponent(UIOpacity) || labelComponent.node.addComponent(UIOpacity)
  38. uiOpacity.opacity = 0 // 初始设置为透明
  39. labelComponent.string = text
  40. // 使用tween实现淡出效果
  41. tween(uiOpacity)
  42. .to(1, { opacity: 255 }) // 1秒内淡入到完全不透明
  43. .start()
  44. }
  45. selectOption(index: number) {
  46. if (index < 1 || index > 4) {
  47. console.warn("Invalid option index!")
  48. return
  49. }
  50. // 重置所有选项的背景和颜色
  51. for (let i = 0; i < 4; i++) {
  52. const optionNode = this.optionNodes[i]
  53. const labelComponent = optionNode.getChildByName("text_option").getComponent(Label)
  54. if (i === index - 1) {
  55. // 这是被选中的选项
  56. labelComponent.color = this.selectedColor
  57. // 设置选中背景图
  58. const spriteComponent = optionNode.getComponent(Sprite)
  59. if (spriteComponent) {
  60. spriteComponent.spriteFrame = this.selectedSprite.spriteFrame
  61. }
  62. // 使用tween实现颜色渐变效果
  63. const currentColor = labelComponent.color.clone()
  64. tween(labelComponent)
  65. .to(2, {
  66. color: Color.lerp(new Color(), currentColor, this.selectedColor, 0.1)
  67. }) // 这里的0.1表示每次插值的比例,你可以根据需要调整
  68. .start()
  69. this.selectedAction(optionNode)
  70. } else {
  71. // 这是未被选中的选项
  72. labelComponent.color = Color.WHITE // 假设默认颜色为白色,你可以根据需要更改
  73. // 移除选中背景图
  74. const spriteComponent = optionNode.getComponent(Sprite)
  75. if (spriteComponent) {
  76. spriteComponent.spriteFrame = this.notSelectedSprite.spriteFrame
  77. }
  78. }
  79. }
  80. }
  81. selectedAction(optionNode: Node) {
  82. tween(optionNode)
  83. .to(0.1, { scale: new Vec3(1.1, 1.1, 1.1) }) // 放大到原始大小的110%
  84. .by(0.1, { position: new Vec3(5, 0, 0) }) // 向右移动5个单位
  85. .by(0.1, { position: new Vec3(-10, 0, 0) }) // 向左移动10个单位
  86. .by(0.1, { position: new Vec3(10, 0, 0) }) // 向右移动10个单位
  87. .by(0.1, { position: new Vec3(-10, 0, 0) }) // 向左移动10个单位
  88. .by(0.1, { position: new Vec3(5, 0, 0) }) // 向右移动5个单位回到原位置
  89. .to(0.1, { scale: new Vec3(1, 1, 1) }) // 缩小回原始大小
  90. .start()
  91. }
  92. private scaleUp(node: Node) {
  93. return tween(node).to(0.1, { scale: new Vec3(1.1, 1.1, 1.1) }) // 放大到原始大小的110%
  94. }
  95. private scaleDown(node: Node) {
  96. return tween(node).to(0.1, { scale: new Vec3(1, 1, 1) }) // 缩小回原始大小
  97. }
  98. private shake(node: Node) {
  99. return tween(node)
  100. .by(0.05, { position: new Vec3(5, 0, 0) }) // 向右移动5个单位
  101. .by(0.05, { position: new Vec3(-10, 0, 0) }) // 向左移动10个单位
  102. .by(0.05, { position: new Vec3(10, 0, 0) }) // 向右移动10个单位
  103. .by(0.05, { position: new Vec3(-10, 0, 0) }) // 向左移动10个单位
  104. .by(0.05, { position: new Vec3(5, 0, 0) }) // 向右移动5个单位回到原位置
  105. }
  106. update(deltaTime: number) {}
  107. }