Skip to content
On this page

Class: SpriteSheetAnimation

适用于texturepacker的动画

ts
let json = await Resource.load('./example/assets/animations/Samurai/Attack.json');
let img = await Resource.load('./example/assets/animations/Samurai/Attack.png');
// 如果要控制动画大小可以使用父节点包裹的方式控制scale属性
const group = app.stage.addChild(new Transform);
group.scale.set(2);
const node = group.addChild(new Transform(Sprite));
const sprite = node.getComponent(Sprite)!;
const spriteSheet = node.addComponent(SpriteSheet);
const ssa = node.addComponent(SpriteSheetAnimation);
// 设置每秒播放帧数
ssa.fps = 8;
// 设置为无限循环播放
ssa.loop = -1;
const func = () => {
	console.log('播放到第', ssa.time, '');
};
ssa.addFrameAction(2, func, this);
ssa.emitter.on(Animation.Event.ENDED, () => {
	console.log('单次动画播放完成');
	ssa.removeFrameAction(2, func, this);
}, this);
spriteSheet.load(json.data, img);
// 需要在下一帧才能正常播放
ticker.once('update', () => {
	ssa.play('Attack');
}, this);

Hierarchy

Table of contents

Constructors

Properties

Accessors

Methods

Constructors

constructor

new SpriteSheetAnimation()

组件不能单独实例化,必须通过Transform.addComponent创建

ts
const node = new Transform();
const componet = node.addComponent(Component);

Inherited from

Animation.constructor

Defined in

src/core/component/Component.ts:37

Properties

animation

animation: string = 'default'

Inherited from

Animation.animation

Defined in

src/core/component/Animation.ts:107


animations

animations: Record<string, Timeline<{}>[]> = {}

动画配置

Inherited from

Animation.animations

Defined in

src/core/component/Animation.ts:46


emitter

emitter: EventEmitter<string | symbol, any>

事件处理器

Inherited from

Animation.emitter

Defined in

src/core/component/Component.ts:27


fps

fps: number = 60

播放的帧速率,按照每秒计算,每秒播放的帧数

Defined in

src/core/component/SpriteSheetAnimation.ts:43


id

id: number

用于表示组件的唯一标识

Inherited from

Animation.id

Defined in

src/core/component/Component.ts:12


loop

loop: number = 0

循环次数,0:不循环,-1:一直循环

Inherited from

Animation.loop

Defined in

src/core/component/Animation.ts:112


name

name: string = ''

组件名

Inherited from

Animation.name

Defined in

src/core/component/Component.ts:17


node

node: Transform<Container>

组件挂载节点

Inherited from

Animation.node

Defined in

src/core/component/Component.ts:22


paused

paused: boolean = true

指示是否被暂停

Inherited from

Animation.paused

Defined in

src/core/component/Animation.ts:125


playCount

playCount: number = 0

已经播放次数

Inherited from

Animation.playCount

Defined in

src/core/component/Animation.ts:116


speed

speed: number = 1

播放速度100%

Inherited from

Animation.speed

Defined in

src/core/component/Animation.ts:120


spriteSheet

spriteSheet: SpriteSheet

Defined in

src/core/component/SpriteSheetAnimation.ts:45


time

time: number = 0

当前时间线

Inherited from

Animation.time

Defined in

src/core/component/Animation.ts:32


timeMax

timeMax: number = 0

动画总长

Inherited from

Animation.timeMax

Defined in

src/core/component/Animation.ts:24


timeline

timeline: EventEmitter<string | symbol, any>

用于记录回调的时间线,在target时间线后执行

Inherited from

Animation.timeline

Defined in

src/core/component/Animation.ts:19


total

total: number = 0

最大帧数

Inherited from

Animation.total

Defined in

src/core/component/Animation.ts:28


Event

Static Event: Object

Type declaration

NameTypeDescription
ENDED"ended"动画播放完成

Inherited from

Animation.Event

Defined in

src/core/component/Animation.ts:10

Accessors

targets

get targets(): Timeline<{}>[]

时间线
每一个对象一个时间线
还有一个总时间线,可以用于记录一些信息

Returns

Timeline<{}>[]

Inherited from

Animation.targets

Defined in

src/core/component/Animation.ts:39

Methods

addFrameAction

addFrameAction(time, callback, target): void

添加事件

Parameters

NameType
timenumber
callback() => void
targetany

Returns

void

Inherited from

Animation.addFrameAction

Defined in

src/core/component/Animation.ts:198


addTimeline

addTimeline(timeline): void

添加一个熟悉变更时间线

Parameters

NameType
timelineTimeline<{}>

Returns

void

Inherited from

Animation.addTimeline

Defined in

src/core/component/Animation.ts:84


destroy

destroy(): void

消毁

Returns

void

Inherited from

Animation.destroy

Defined in

src/core/component/Component.ts:55


next

next(): void

播放下一帧

Returns

void

Inherited from

Animation.next

Defined in

src/core/component/Animation.ts:53


nextAction

Protected nextAction(val): void

每次当需要更新帧动画时调用

Parameters

NameTypeDescription
valTimeline<{}>即将更新的帧动画配置

Returns

void

Overrides

Animation.nextAction

Defined in

src/core/component/SpriteSheetAnimation.ts:70


onDestroy

Optional onDestroy(): void

组件被销毁时触发

Returns

void

Inherited from

Animation.onDestroy

Defined in

src/core/component/Component.ts:62


play

play(): void

播放

Returns

void

Inherited from

Animation.play

Defined in

src/core/component/Animation.ts:127

play(name): void

播放

Parameters

NameTypeDescription
namestring要播放的动画

Returns

void

Inherited from

Animation.play

Defined in

src/core/component/Animation.ts:128

play(loop): void

播放

Parameters

NameTypeDescription
loopnumber要循环的次数

Returns

void

Inherited from

Animation.play

Defined in

src/core/component/Animation.ts:129

play(name, loop): void

播放

Parameters

NameTypeDescription
namestring要播放的动画
loopnumber要循环的次数

Returns

void

Inherited from

Animation.play

Defined in

src/core/component/Animation.ts:130


prev

prev(): void

播放上一帧

Returns

void

Inherited from

Animation.prev

Defined in

src/core/component/Animation.ts:103


puase

puase(): void

暂停

Returns

void

Inherited from

Animation.puase

Defined in

src/core/component/Animation.ts:168


removeFrameAction

removeFrameAction(time, callback, target): void

移除事件

Parameters

NameType
timenumber
callback() => void
targetany

Returns

void

Inherited from

Animation.removeFrameAction

Defined in

src/core/component/Animation.ts:205


setTimeline

setTimeline(timelines, name?): void

设置一个属性变更时间线

Parameters

NameType
timelinesTimeline<{}>[]
name?string

Returns

void

Inherited from

Animation.setTimeline

Defined in

src/core/component/Animation.ts:93


start

start(): void

组件被挂载到节点时触发

Returns

void

Overrides

Animation.start

Defined in

src/core/component/SpriteSheetAnimation.ts:47


stop

stop(): void

停止

Returns

void

Inherited from

Animation.stop

Defined in

src/core/component/Animation.ts:160


update

update(time): void

每帧调用

Parameters

NameTypeDescription
timenumber和上一帧的间隔时间 ms

Returns

void

Inherited from

Animation.update

Defined in

src/core/component/Animation.ts:172