Skip to content

Commit

Permalink
feat: frame info & stop at last frame (#151)
Browse files Browse the repository at this point in the history
* - feat: frame info
- feat: stop at last frame

* fix: use forward to stop at last frame
  • Loading branch information
fanmingfei authored Nov 23, 2021
1 parent 74c33df commit e3fd4d6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
3 changes: 2 additions & 1 deletion examples/src/spriteAnimation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function init(canvas) {
},
},
preload: false,
},
}
]);

const game = new Game({
Expand All @@ -44,6 +44,7 @@ export async function init(canvas) {
resource: 'fruit',
speed: 100,
autoPlay: true,
forwards: true
}),
);

Expand Down
30 changes: 23 additions & 7 deletions packages/plugin-renderer-sprite-animation/lib/component.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import {Component} from '@eva/eva.js';
import {type, step} from '@eva/inspector-decorator';
import {SpriteAnimation as SpriteAnimationEngine} from '@eva/renderer-adapter';
import { Component } from '@eva/eva.js';
import { type, step } from '@eva/inspector-decorator';
import { SpriteAnimation as SpriteAnimationEngine } from '@eva/renderer-adapter';

export interface SpriteAnimationParams {
resource: string;
autoPlay: boolean;
speed: number;
autoPlay?: boolean;
speed?: number;
/** Stop at last frame */
forwards?: boolean;
}

export default class SpriteAnimation extends Component<SpriteAnimationParams> {
static componentName: string = 'SpriteAnimation';
@type('string') resource: string = '';
@type('boolean') autoPlay: boolean = true;
@type('number') @step(10) speed: number = 100;
@type('boolean') forwards: boolean = false;
_animate: SpriteAnimationEngine;
private waitPlay: boolean = false;
private waitStop: boolean = false;
Expand All @@ -22,8 +25,12 @@ export default class SpriteAnimation extends Component<SpriteAnimationParams> {
obj && Object.assign(this, obj);
this.on('loop', () => {
if (++this.count >= this.times) {
this.animate.stop();
this.emit('complete');
if (this.forwards) {
this.animate.animatedSprite.loop = false
} else {
this.animate.stop();
this.emit('complete');
}
}
});
}
Expand All @@ -35,6 +42,9 @@ export default class SpriteAnimation extends Component<SpriteAnimationParams> {
if (!this.animate) {
this.waitPlay = true;
} else {
if (times === 1 && this.forwards) {
this.animate.animatedSprite.loop = false
}
this.animate.play();
this.count = 0;
}
Expand Down Expand Up @@ -66,4 +76,10 @@ export default class SpriteAnimation extends Component<SpriteAnimationParams> {
gotoAndStop(frameNumber) {
this.animate.gotoAndStop(frameNumber);
}
get currentFrame() {
return this.animate?.animatedSprite?.currentFrame
}
get totalFrames() {
return this.animate?.animatedSprite?.totalFrames
}
}

0 comments on commit e3fd4d6

Please sign in to comment.