WOneByone.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import i18n from "../../../../i18n/i18n";
  2. import { SpriteActionType } from "../../object/FSprite";
  3. /**
  4. * 文字一个个的出现
  5. */
  6. const {ccclass, property} = cc._decorator;
  7. @ccclass
  8. export default class WOneByone extends cc.Component {
  9. @property(cc.Label)
  10. label: cc.Label = null;
  11. text: string = '';
  12. private indexA = 0;
  13. private indexB = 0;
  14. public isOver = true;
  15. private interval = 100;//字出现间隔100毫秒
  16. private jumpThis = false;//是否跳过
  17. private ctime = 0;
  18. public dialogs:Array<string> = null;
  19. public spine:sp.Skeleton = null;
  20. public callback:()=>void;
  21. public setCallback(callback:()=>void){
  22. this.callback = callback;
  23. }
  24. public _start(){
  25. this.jumpThis = false;
  26. this.indexA = 0;
  27. this.nextAction();
  28. }
  29. update(dt){
  30. if(this.isOver){
  31. return;
  32. }
  33. if(this.jumpThis){
  34. this.showWorld();
  35. }else{
  36. let time = new Date().getTime();
  37. if(time - this.ctime > this.interval){
  38. this.ctime = time;
  39. this.showWorld();
  40. }
  41. }
  42. }
  43. public reset(){
  44. this.indexA = 0;
  45. this.indexB = 0;
  46. this.label.string = '';
  47. this.isOver = false;
  48. this.jumpThis = false;
  49. }
  50. public jump(){
  51. if(this.isOver){
  52. this.indexA ++;
  53. if(this.indexA >= this.dialogs.length){
  54. this.callback();
  55. return;
  56. }
  57. this.nextAction();
  58. return;
  59. }
  60. this.jumpThis = true;
  61. }
  62. private showWorld(){
  63. if(this.indexB >= this.text.length){
  64. this.isOver = true;
  65. return;
  66. }
  67. this.indexB ++;
  68. let tmp = this.text.substring(0,this.indexB);
  69. this.label.string = tmp;
  70. }
  71. //执行下一个动作
  72. private nextAction(){
  73. this.indexB = 0;
  74. this.text = i18n.t(this.dialogs[this.indexA]);
  75. this.jumpThis = false;
  76. //判断是表情还是文字
  77. if(this.text.indexOf('#') == 0){//第一个字符为#表示表情
  78. if(this.spine){
  79. this.node.children[0].active = false;
  80. this.spine.setCompleteListener(()=>{
  81. this.spine.setCompleteListener(null);
  82. this.node.children[0].active = true;
  83. this.spine.setAnimation(0, SpriteActionType.stand, true);
  84. this.jump();
  85. })
  86. let action = this.text.replace('#','');
  87. this.spine.setAnimation(0,action, false);
  88. }else{
  89. this.jump();
  90. }
  91. }else{
  92. this.isOver = false;
  93. }
  94. }
  95. }