Tools.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { Animation, AnimationClip, Node, Sprite, SpriteFrame, Vec3 } from "cc";
  2. export class Tools {
  3. static createNode(parent?: Node, pos?: Vec3):Node{
  4. const node = new Node();
  5. parent && node.setParent(parent);
  6. pos && node.setPosition(pos);
  7. return node;
  8. }
  9. static createSprite(frame: SpriteFrame, parent?: Node, pos?: Vec3): Node{
  10. const node = this.createNode(parent, pos);
  11. node.addComponent(Sprite).spriteFrame = frame;
  12. return node;
  13. }
  14. static createAnimation(frames: SpriteFrame[], sample: number, obj: Node | Animation, mode: AnimationClip.WrapMode = AnimationClip.WrapMode.Normal){
  15. let ani: Animation;
  16. if(obj instanceof Animation){
  17. ani = obj;
  18. }else{
  19. ani = obj.addComponent(Animation);
  20. }
  21. //添加动画剪辑
  22. const clip = AnimationClip.createWithSpriteFrames(frames, sample);
  23. clip.wrapMode = mode;
  24. ani.addClip(clip);
  25. ani.defaultClip = clip;
  26. return ani;
  27. }
  28. }