monsterFactory.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
  7. const {ccclass, property, executeInEditMode, playOnFocus} = cc._decorator;
  8. @ccclass
  9. @executeInEditMode
  10. @playOnFocus
  11. export default class MonsterFactory extends cc.Component {
  12. @property(cc.Prefab) monster: cc.Prefab = null;
  13. @property(cc.AnimationClip) aniClip:cc.AnimationClip = null;
  14. @property(cc.Integer)
  15. public get num(): number {
  16. return this._num;
  17. }
  18. public set num(value: number) {
  19. this._num = value;
  20. if(!CC_EDITOR) return;
  21. this.play();
  22. }
  23. @property(cc.Integer)
  24. private _num: number = 0;
  25. @property(cc.Integer)
  26. public get time(): number {
  27. return this._time;
  28. }
  29. public set time(value: number) {
  30. this._time = value;
  31. if(!CC_EDITOR) return;
  32. this.play();
  33. }
  34. @property(cc.Integer)
  35. private _time: number = 1;
  36. protected start(): void {
  37. this.play();
  38. }
  39. protected play(): void {
  40. this.node.destroyAllChildren();
  41. for (let index = 0; index < this.num; index++) {
  42. let node = cc.instantiate(this.monster);
  43. let ani = node.addComponent(cc.Animation);
  44. ani.addClip(this.aniClip)
  45. node._objFlags |= cc.Object.Flags.DontSave | cc.Object.Flags.LockedInEditor;
  46. node.active = false;
  47. this.node.addChild(node);
  48. setTimeout(() => {
  49. node.active = true;
  50. ani.play('path1');
  51. }, 1000 * this.time * index);
  52. }
  53. }
  54. }