UISpineMovie.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { CCString, Color, Component, sp, _decorator} from 'cc';
  2. const { ccclass, property } = _decorator;
  3. /**
  4. * spine动画表现管理
  5. * @author
  6. */
  7. @ccclass('UISpineMovie')
  8. export default class UISpineMovie extends Component {
  9. @property({ type: [CCString], displayName: '动画列表' })
  10. private animat_names: Array<string> = [];
  11. @property({type: sp.Skeleton, tooltip: '角色动画' })
  12. private spine:sp.Skeleton = null!;
  13. private cur:string =null!;
  14. private index:number=0;
  15. onLoad() {
  16. }
  17. start(){
  18. //this.spine.clearTracks()
  19. /*this.spine.setStartListener(function(){
  20. console.log("动画开始")
  21. }.bind(this));*/
  22. /* this.spine.setEventListener(((trackEntry:any, event:any) => {
  23. //var animationName = trackEntry.animation ? trackEntry.animation.name : "";
  24. //console.log("[track %s][animation %s] event: %s, %s, %s, %s", trackEntry.trackIndex, animationName, event.data.name, event.intValue, event.floatValue, event.stringValue);
  25. this.unit.DoEvent(event.data.name);
  26. }) as any);*/
  27. //
  28. //let duration = this.spine.getCurrent(0).animation.duration;
  29. //let frames = Math.ceil(frameRate * duration);
  30. //let secondsPerFrame = 1 / frameRate;
  31. //`总帧数 ${this.frames}`;
  32. //this.spine.setCompleteListener(trackEntry => {
  33. //});
  34. if(this.animat_names.length>0){
  35. this.index=0;
  36. if(this.index==this.animat_names.length-1){
  37. this.DoAnimation(this.animat_names[this.index],true);
  38. }else{
  39. this.DoAnimation(this.animat_names[this.index],false);
  40. }
  41. this.spine.setCompleteListener(function(){
  42. this.index++;
  43. if(this.index<this.animat_names.length){
  44. if(this.index==this.animat_names.length-1){
  45. this.DoAnimation(this.animat_names[this.index],true);
  46. }else{
  47. this.DoAnimation(this.animat_names[this.index],false);
  48. }
  49. }else{
  50. this.spine.setCompleteListener(null);
  51. }
  52. }.bind(this));
  53. }
  54. }
  55. public SetSkin(skin:string):void{
  56. this.spine.setSkin(skin);
  57. }
  58. public DoAnimation(animation:string,loop:boolean=true,track:number=0,force:boolean=false){
  59. if(force){
  60. this.spine.setAnimation(track,animation,loop);
  61. return;
  62. }
  63. if(this.cur==animation)return;
  64. this.spine.setAnimation(track,animation,loop);
  65. }
  66. /**设置是否暂停 */
  67. public SetPaused(paused:boolean):void{
  68. this.spine.paused=paused;
  69. }
  70. /**获取是否暂停 */
  71. public GetPaused():boolean{
  72. return this.spine.paused;
  73. }
  74. public CheckPaused(paused:boolean):void{
  75. if(this.spine.paused!=paused)this.spine.paused=paused;
  76. }
  77. /** 闪色表现 Color.RED 0.1*/
  78. public Flash(color:Color,time:number,reColor:Color=Color.WHITE):void{
  79. if(this.spine.color!=color){
  80. this.spine.color=color;
  81. this.unscheduleAllCallbacks();
  82. this.scheduleOnce(()=>{
  83. this.spine.color = reColor;
  84. },time);
  85. }
  86. }
  87. /**
  88. * 缩放动画播放速率
  89. * @override
  90. * @param scale 缩放倍率
  91. */
  92. public scaleTime(scale: number) {
  93. if (scale > 0)
  94. this.spine.timeScale = scale;
  95. }
  96. public getBone(name: string): any {
  97. var bone = this.spine.findBone(name);
  98. return bone
  99. }
  100. }