ui_interact_input.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { _decorator, Component, instantiate, Node, Prefab, Size, Sprite, UITransform, Vec3 } from 'cc';
  2. import { ui_base } from './ui_base';
  3. import { interact_input_data, ui_att_item } from '../../../data/data';
  4. import { tools } from '../../tools';
  5. import { gameManager } from '../gameManager';
  6. import { ui_input_box_number_item } from './ui_input_box_number_item';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('ui_interact_input')
  9. export class ui_interact_input extends ui_base {
  10. @property(Node) input_default:Node = null;
  11. @property(Node) input_fail:Node = null;
  12. @property(Node) input_right:Node = null;
  13. @property(Node) input_box:Node = null;
  14. @property(Node) input_box_node:Node = null;
  15. @property(Prefab) ui_input_box_number_item_prefab:Prefab = null;
  16. @property(Node) input_btn_0:Node = null;
  17. @property(Node) input_btn_1:Node = null;
  18. @property(Node) input_btn_2:Node = null;
  19. @property(Node) input_btn_3:Node = null;
  20. @property(Node) input_btn_4:Node = null;
  21. @property(Node) input_btn_5:Node = null;
  22. @property(Node) input_btn_6:Node = null;
  23. @property(Node) input_btn_7:Node = null;
  24. @property(Node) input_btn_8:Node = null;
  25. @property(Node) input_btn_9:Node = null;
  26. @property(Node) input_btn_x:Node = null;
  27. @property(Node) input_btn_j:Node = null;
  28. private mInputList:Node[] = []
  29. private mInputData:interact_input_data = null;
  30. private mInputAttList:Map<string,ui_att_item> = new Map;
  31. private btnArrList:ui_att_item[] = []
  32. private answer_list:string[] = []
  33. private input_answer_list:string[] = []
  34. protected init(): void {
  35. this.mInputData = this.mTopData._interact_input_data;
  36. if(this.mInputData===null){
  37. return tools.showToast("配置ui 输入类型错误!")
  38. }
  39. this.loadBg(this.mInputData.bg)
  40. this.answer_list= this.mInputData.answer.split(",")
  41. this.initInputBtns()
  42. this.initInputStatusView()
  43. }
  44. protected onShow(): void {
  45. this.input_default.active = true;
  46. this.input_fail.active =false;
  47. this.input_right.active = false;
  48. this.input_box_node.removeAllChildren()
  49. this.input_answer_list = []
  50. }
  51. onInputBtnClick(any_data){
  52. let name:string = any_data.target.name.toString()
  53. let input_number = name.substring(4,name.length)
  54. this.input_answer_list.push(input_number)
  55. this.InputNumber(input_number)
  56. if(this.input_answer_list.length>=this.answer_list.length){
  57. console.log("input--",this.input_answer_list,this.answer_list)
  58. if(this.input_answer_list.toString()==this.answer_list.toString()){
  59. this.input_right.active = true;
  60. this.scheduleOnce(()=>{
  61. this.onFinishEvent()
  62. },1)
  63. }else{
  64. this.input_fail.active = true;
  65. this.scheduleOnce(()=>{
  66. this.onFialEvent()
  67. },1)
  68. }
  69. }
  70. }
  71. InputNumber(name:string){
  72. let input_number = instantiate(this.ui_input_box_number_item_prefab)
  73. input_number.parent = this.input_box_node;
  74. let sf = gameManager.getCacheSpriteFrameByName(this.mInputAttList.get(name).res)
  75. input_number.getComponent(ui_input_box_number_item).initView(sf,this.mInputData.font,name);
  76. }
  77. initInputStatusView(){
  78. gameManager.initUiBaseAtt(this.input_default,this.mInputData.input_default)
  79. this.input_fail.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(this.mInputData.input_fail.res)
  80. this.input_fail.getComponent(UITransform).setContentSize(this.input_default.getComponent(UITransform).contentSize)
  81. this.input_fail.position = this.input_default.position
  82. this.input_right.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(this.mInputData.input_right.res)
  83. this.input_right.getComponent(UITransform).setContentSize(this.input_default.getComponent(UITransform).contentSize)
  84. this.input_right.position = this.input_default.position
  85. this.input_right.active =false;
  86. this.input_fail.active = false;
  87. }
  88. initInputBtns(){
  89. this.mInputList.push(this.input_btn_0)
  90. this.mInputList.push(this.input_btn_1)
  91. this.mInputList.push(this.input_btn_2)
  92. this.mInputList.push(this.input_btn_3)
  93. this.mInputList.push(this.input_btn_4)
  94. this.mInputList.push(this.input_btn_5)
  95. this.mInputList.push(this.input_btn_6)
  96. this.mInputList.push(this.input_btn_7)
  97. this.mInputList.push(this.input_btn_8)
  98. this.mInputList.push(this.input_btn_9)
  99. this.mInputList.push(this.input_btn_x)
  100. this.mInputList.push(this.input_btn_j)
  101. this.btnArrList.push(this.mInputData.att_0)
  102. this.btnArrList.push(this.mInputData.att_1)
  103. this.btnArrList.push(this.mInputData.att_2)
  104. this.btnArrList.push(this.mInputData.att_3)
  105. this.btnArrList.push(this.mInputData.att_4)
  106. this.btnArrList.push(this.mInputData.att_5)
  107. this.btnArrList.push(this.mInputData.att_6)
  108. this.btnArrList.push(this.mInputData.att_7)
  109. this.btnArrList.push(this.mInputData.att_8)
  110. this.btnArrList.push(this.mInputData.att_9)
  111. this.btnArrList.push(this.mInputData.att_xing)
  112. this.btnArrList.push(this.mInputData.att_jing)
  113. //
  114. this.mInputAttList.set("0",this.mInputData.image_0)
  115. this.mInputAttList.set("1",this.mInputData.image_1)
  116. this.mInputAttList.set("2",this.mInputData.image_2)
  117. this.mInputAttList.set("3",this.mInputData.image_3)
  118. this.mInputAttList.set("4",this.mInputData.image_4)
  119. this.mInputAttList.set("5",this.mInputData.image_5)
  120. this.mInputAttList.set("6",this.mInputData.image_6)
  121. this.mInputAttList.set("7",this.mInputData.image_7)
  122. this.mInputAttList.set("8",this.mInputData.image_8)
  123. this.mInputAttList.set("9",this.mInputData.image_9)
  124. this.mInputAttList.set("xing",this.mInputData.image_xing)
  125. this.mInputAttList.set("jing",this.mInputData.image_jing)
  126. for (let index = 0; index < this.mInputList.length; index++) {
  127. const btn = this.mInputList[index];
  128. let btn_data = this.btnArrList[index];
  129. btn.getComponent(UITransform).setContentSize(new Size(btn_data.width,btn_data.height))
  130. btn.position = new Vec3(btn_data.x,btn_data.y)
  131. btn.on(Node.EventType.TOUCH_START,this.onInputBtnClick.bind(this))
  132. }
  133. }
  134. }