123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- class AnimatedSprite2D extends Sprite2D
- {
- constructor(name, p5Image, spriteFrames)
- {
- super(name, p5Image);
- this.spriteFrames = spriteFrames;
- this.playing = false;
- this.frame = 0;
- this.currentAnimation = 0;
- this.timeSinceLastFrame = 0;
- }
-
- setCurrentAnimationByIndex(idx)
- {
- if (idx < this.spriteFrames.numAnimations)
- this.currentAnimation = idx;
- else this.currentAnimation = null;
- this.frame = 0;
- this.timeSinceLastFrame = 0;
- }
- setCurrentAnimationByName(name)
- {
- this.currentAnimation = this.spriteFrames.getAnimationIndexByName(name);
- this.frame = 0;
- this.timeSinceLastFrame = 0;
- }
- setCurrentFrameTime(time)
- {
- this.getCurrentAnimation().setFrameTime(time);
- }
- setCurrentFPS(fps)
- {
- this.getCurrentAnimation().setFPS(fps);
- }
-
- getAnimationByIndex(idx)
- {
- return this.spriteFrames.getAnimationByIndex(idx);
- }
- getCurrentAnimation()
- {
- return this.spriteFrames.getAnimationByIndex(this.currentAnimation);
- }
- getCurrentFrame()
- {
- return this.getCurrentAnimation().getFrame(this.frame);
- }
- getCurrentFrameTime()
- {
- return this.getCurrentAnimation().getFrameTime();
- }
- getCurrentNumFrames()
- {
- return this.getCurrentAnimation().getNumFrames();
- }
-
- play()
- {
- this.playing = true;
- }
- stop()
- {
- this.playing = false;
- }
- isPlaying()
- {
- return this.playing;
- }
- update(delta)
- {
- if (this.playing)
- {
- this.timeSinceLastFrame += delta;
- if (this.timeSinceLastFrame >= this.getCurrentFrameTime())
- {
- this.frame = (this.frame + 1) % this.getCurrentNumFrames();
- this.timeSinceLastFrame = 0;
- }
- }
- this.P5Image = this.getCurrentFrame();
- this._update(delta);
- for (let i = 0; i < this.children.length; i++)
- this.children[i].update(delta);
- }
- }
|