123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- import { _decorator, Component, Node, Sprite, SpriteFrame } from 'cc';
- const { ccclass, property } = _decorator;
- @ccclass('FrameAni')
- export class FrameAni extends Component {
- @property({type:[SpriteFrame]})
- ani1Sprites = []
- @property({type:[SpriteFrame]})
- ani2Sprites = []
- @property({type:[SpriteFrame]})
- ani3Sprites = []
- @property({type:[SpriteFrame]})
- ani4Sprites = []
- @property({type:[Number]})
- rates = []
- private rate:number=0;
- private curFrame:number = 0;
- private curTime:number=0;
- private aniSprite:Sprite;
- private isPlay:boolean = false;
- @property({type:Boolean})
- private isLoop:boolean = false;
- curAniSprites:SpriteFrame[];
- @property({type:Number})
- aniType:number = -1;
- private callback:Function;
- private self:any;
- protected onLoad(): void {
- this.SetAniSprites();
- this.aniSprite = this.node.getComponent(Sprite);
- console.log("aniType==>>"+this.aniType)
- if (this.aniType >= 0)
- {
- this.rate = this.rates[this.aniType];
- this.curFrame = 0;
- this.isPlay = true;
- this.aniSprite.spriteFrame = this.curAniSprites[this.curFrame];
- }
- }
- public Stop()
- {
- this.isPlay = false;
- }
- private SetAniSprites()
- {
- if (this.aniType == 0)
- {
- this.curAniSprites= this.ani1Sprites;
- }else if (this.aniType == 1)
- {
- this.curAniSprites = this.ani2Sprites;
- }
- else if (this.aniType == 2)
- {
- this.curAniSprites = this.ani3Sprites;
- }
- else if (this.aniType == 3)
- {
- this.curAniSprites = this.ani4Sprites;
- }
- }
- public ChangeAniType( _aniType, _isLoop=false, isForce= false)
- {
- if (this.aniType == _aniType && !isForce) return;
- // Debug.Log("ChangeAniType==>>" + aniType + "," + _aniType + "," + _isLoop);
- this.aniType = _aniType;
- this.SetAniSprites();
- this.curFrame = 0;
- this.curTime=0;
- this.isPlay=true;
- this.isLoop = _isLoop;
- this.rate = this.rates[this.aniType];
- this.aniSprite.spriteFrame = this.curAniSprites[this.curFrame];
- }
- public SetCallBack( _callback,_self)
- {
- this.callback = _callback;
- this.self = _self;
- }
- start() {
- }
- update(deltaTime: number) {
- if (!this.isPlay) return;
- this.curTime += deltaTime;
- // console.log("update==>>"+this.curTime+","+(1.0/ this.rate))
- if(this.curTime>=1.0/ this.rate)
- {
- this.curTime = 0;
- this.curFrame++;
- if (this.curFrame>= this.curAniSprites.length)
- {
- if (!this.isLoop)
- {
- this.isPlay = false;
- this.aniType = -1;
- if (this.callback!=null)
- this.callback(this.self);
- return;
- }
- else
- {
- this.curFrame = 0;
- }
- }
- this.aniSprite.spriteFrame = this.curAniSprites[this.curFrame];
- }
- }
- }
|