MovieClip
对象的方法中,并没有提供这样的api,但我们可以通过为动画的某一帧添加事件,再在代码中监听该事件,调用停止方法即可。具体如下——
步骤一
在生成的动画数据json中,在events
数组里添加对象。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{
"mc": {
"dog_action0": {
"frameRate": 25,
"events": [
// 添加在这里 ->
{
"name": "@stop", // 事件名
"frame": 1 //事件发生所在帧
}, {
"name": "@fall",
"frame": 2
}
// <- end
],
"frames": [
{
"res": "1_00000",
"x": 6,
"y": 10
},
...
]
},
...
}
}
步骤二
在代码中监听FRAME_LABEL
事件,监听到指定事件名,让动画停止。1
2
3
4
5this.mc.removeEventListener(egret.MovieClipEvent.FRAME_LABEL, function (e:egret.TouchEvent):void {
if (e.frameLabel == "@stop") {
this.mc.stop();
}
}, this)
或者
如果想给任意帧添加暂停事件,我们也可在events
数组中给每一帧都加上事件,像这样:
1 | { |
然后,再在代码中监听FRAME_LABEL
事件,判断当前帧数即可。1
2
3
4
5
6let anyFrame = 3;
this.mc.removeEventListener(egret.MovieClipEvent.FRAME_LABEL, function (e:egret.TouchEvent):void {
if (this.mc.currentFrame == anyFrame) {
this.mc.stop();
}
}, this)
end