egret-code-AnimationTextField

介绍

动画一个一个字的显示文本的内容。

代码

AnimationTextField.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
class AnimationTextField extends egret.TextField
{
private _timeId:number;
private _aniTextFieldType:AniTextFieldType;

private _textCache: string;
private _textFlowCache: Array<egret.ITextElement>;
private _curTime:number;
private _showTime:number;
private _interval:number;

private _endCallBack:Function;
constructor()
{
super();
this._timeId = -1;
this._showTime = 0;
this._interval = 0.1;
this._aniTextFieldType = AniTextFieldType.text;
this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.OnRemoveFromStage, this);
}

public set textCache(value:string)
{
this._textCache = value;
this._aniTextFieldType = AniTextFieldType.text;
}

public set showTime(value:number){
this._showTime = value;
}

public set interval(value:number){
this._interval = value;
}

public set textFlowCache(value:Array<egret.ITextElement>)
{
this._textFlowCache = value;
this._aniTextFieldType = AniTextFieldType.textFlow;
}

public beginShow(endCall:Function = null){
this._curTime = 0;
this._endCallBack = endCall;
if(this._aniTextFieldType == AniTextFieldType.text)
{
this.textShowAction();
} else {
this.textFlowShowAction();
}
}

private textShowAction()
{
let textLength = this._textCache.length;
let subLength = 1;
let actionTime = textLength;
if(this._showTime){
actionTime = Math.floor(this._showTime/this._interval);
subLength = Math.ceil(textLength/actionTime);
}
this.text = "";
this._timeId = GameTimer.AddTimerBehavior(()=>{
this.appendText(this._textCache.substr(this._curTime*subLength,subLength));
this._curTime ++;
if(this._curTime*subLength >= textLength){
this.Stop();
this._endCallBack();
}
// this.text = this._textCache.substr(0,this._curTime*subLength);
// if(this._curTime >= actionTime && this._endCallBack){
// this._endCallBack();
// this.Stop();
// }
},actionTime,0,this._interval);
}

private textFlowShowAction()
{
this.textFlow = <Array<egret.ITextElement>>[];
this._timeId = GameTimer.AddTimerBehavior(()=>{
if(this._textFlowCache.length > 0){
if(this._curTime &&this.textFlow.length){
this.textFlow.splice(-1,1);
}
let element = <egret.ITextElement>{};
if(this._textFlowCache[0].style){
element.style = this._textFlowCache[0].style;
}
this._curTime ++;
element.text = this._textFlowCache[0].text.substr(0,this._curTime);
this.appendElement(element);

if(this._curTime == this._textFlowCache[0].text.length){
this._textFlowCache.splice(0,1);
this._curTime = 0;
if(this._textFlowCache.length == 0){
this.Stop();
if(this._endCallBack){
this._endCallBack();
}
}
}
}
},-1,0,this._interval);
}



// private textFlowShowAction()
// {
// this.textFlow = [];
// this._timeId = GameTimer.AddTimerBehavior(()=>{
// if(this._textFlowCache.length > 0){
// if(this._curTime == 0){
// let element = <egret.ITextElement>{};
// if(this._textFlowCache[0].style){
// element.style = this._textFlowCache[0].style;
// }
// element.text = "";
// this.textFlow.push(element);
// }
// this._curTime ++;
// this.textFlow[this.textFlow.length -1].text = this._textFlowCache[0].text.substr(0,this._curTime);
// if(this._curTime == this._textFlowCache[0].text.length){
// this._textFlowCache.splice(0,1);
// this._curTime = 0;
// if(this._textFlowCache.length == 0){
// this.Stop();
// if(this._endCallBack){
// this._endCallBack();
// }
// }
// }
// }
// },-1,0,this._interval);
// }
public ActionToEnd()
{
this.Stop();
if(this._aniTextFieldType == AniTextFieldType.text)
{
this.text = this._textCache;
} else {
this.textFlowActionToEnd();
}
if(this._endCallBack){
this._endCallBack();
}
}

private textFlowActionToEnd()
{
if(this._textFlowCache.length > 0){
if(this._curTime != 0){
this.textFlow[this.textFlow.length -1].text = this._textFlowCache[0].text;
this._textFlowCache.splice(0,1);
}
if(this._textFlowCache.length > 0){
for (let index = 0; index < this._textFlowCache.length; index++) {
const element = this._textFlowCache[index];
this.textFlow.push(element);
}
this._textFlowCache = [];
}
}
}

private Stop()
{
if(this._timeId > 0){
GameTimer.RemoveTimerBehavior(this._timeId);
this._timeId = -1;
}
}
// 从舞台移除消息处理
private OnRemoveFromStage( evt:egret.Event )
{
this.Stop();
// this.DestroyDragonBone();
}

}

enum AniTextFieldType {text,textFlow}