aniList.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { EditorSet } from "../common/editorSet";
  2. const {ccclass, property, executeInEditMode, playOnFocus} = cc._decorator;
  3. ccclass
  4. class AniData{
  5. loop: boolean;
  6. }
  7. @ccclass
  8. @executeInEditMode
  9. @playOnFocus
  10. export default class AniList extends cc.Component {
  11. @property(cc.Prefab) monster: cc.Prefab = null;
  12. @property([cc.AnimationClip]) aniClip = [];
  13. monsterNode: cc.Node;
  14. protected onLoad(): void {
  15. let monsterNode = new cc.Node('monsterGroup');
  16. this.monsterNode = monsterNode;
  17. EditorSet.DontShow(monsterNode);
  18. this.node.addChild(monsterNode);
  19. if(CC_EDITOR){
  20. let node = new cc.Node('Collide');
  21. this.node.addChild(node);
  22. node.zIndex = cc.macro.MAX_ZINDEX;
  23. EditorSet.DontShow(node);
  24. let debugDrawer = node.addComponent(cc.Graphics);
  25. debugDrawer.lineWidth = 5;
  26. debugDrawer.strokeColor = new cc.Color(255, 0, 0);
  27. debugDrawer.fillColor = new cc.Color(255, 0, 0);
  28. debugDrawer.circle(0, 0, 20);
  29. debugDrawer.stroke();
  30. }
  31. else{
  32. this.play();
  33. }
  34. }
  35. // protected onFocusInEditor(): void {
  36. // this.play();
  37. // }
  38. // protected onLostFocusInEditor(): void {
  39. // this.end();
  40. // }
  41. end(){
  42. this.monsterNode.destroyAllChildren();
  43. }
  44. play(): void {
  45. this.monsterNode.destroyAllChildren();
  46. if(!this.monster) {
  47. cc.log('设置怪物资源');
  48. return;
  49. }
  50. let monsterNode = cc.instantiate(this.monster);
  51. let cnt = 0;
  52. let callAni = ()=>{
  53. let aniNode = new cc.Node('AniItem');
  54. let parent = monsterNode.parent ? monsterNode.parent : this.monsterNode;
  55. monsterNode.removeFromParent(false);
  56. aniNode.addChild(monsterNode);
  57. parent.addChild(aniNode);
  58. let ani = aniNode.addComponent(cc.Animation);
  59. ani.addClip(this.aniClip[cnt], 'path')
  60. ani.play('path');
  61. ani.once(cc.Animation.EventType.FINISHED, ()=>{
  62. cnt++;
  63. if(cnt < this.aniClip.length){
  64. aniNode.angle = 0;
  65. callAni();
  66. }
  67. });
  68. }
  69. callAni();
  70. }
  71. }