vanilladocs
vanillapunch.com
API / misc/spriteanimator

misc/spriteanimator

스프라이트 애니메이터 컴포넌트.

Sprite animator component.

SpriteAnimatorextends Component

짝Counterpart: vanilla-engine SpriteAnimator

스프라이트 애니메이터 컴포넌트.

  • 이름을 붙인 클립(프레임 시퀀스)을 등록해 두고 SpriteAnimator.play(node, "run") 이나 playClip("run") 으로 전환한다.
  • 같은 클립을 다시 틀어도 재시작하지 않는다. (restartIfSame 옵션으로 바꿀 수 있다)
  • 클립이 끝나면 onComplete 콜백으로 다음 상태를 이어 갈 수 있다. (FSM 과 결합)
  • 같은 노드의 Sprite 컴포넌트에 현재 프레임(image + rect)을 반영한다.
  • 사용:
const animator = node.addComponent(SpriteAnimator);
animator.addClip("idle", image, idleRects, 10, true);
animator.addClip("attack", image, attackRects, 12, false);
SpriteAnimator.play(node, "attack", { onComplete: () => animator.playClip("idle") });
  • 두 엔진이 같은 꼴: 정적 SpriteAnimator.play(node, clipName, options) 가 노드의 애니메이터로 틀고, 애니메이터만 있을 때(완료 콜백 안)는 playClip 이다. (러스트 판 SpriteAnimator::play, play_clip)

Sprite animator component.

  • Registers named clips (frame sequences) and switches with SpriteAnimator.play(node, "run") or playClip("run").
  • Playing the same clip again does not restart it. (can be changed with the restartIfSame option)
  • When a clip ends, the next state can follow via the onComplete callback. (combines with an FSM)
  • Applies the current frame (image + rect) to the Sprite component on the same node.
  • Usage:
const animator = node.addComponent(SpriteAnimator);
animator.addClip("idle", image, idleRects, 10, true);
animator.addClip("attack", image, attackRects, 12, false);
SpriteAnimator.play(node, "attack", { onComplete: () => animator.playClip("idle") });
  • Same form in both engines: the static SpriteAnimator.play(node, clipName, options) plays with the node's animator, and playClip is for when only the animator is at hand (inside the complete callback). (the Rust SpriteAnimator::play, play_clip)
예제Example대기 클립을 돌리다 누르면 공격 한 번 뒤 대기로 돌아가기Loop an idle clip, attack once on press, then return to idle
import { Engine, EngineConfiguration, Scene, WorldNode, Sprite, SpriteAnimator, ImageAsset, Rect, Vector2, Pivot } from "./build/vanilla.js";

const imageAsset = new ImageAsset();
await imageAsset.load("examples/assets/spritesheet.png");
const image = imageAsset.getImage();
const idleRects = [];
const attackRects = [];
for (let column = 0; column < 3; ++column) {
	idleRects.push(Rect.create(column * 200, 0, 200, 200));
	attackRects.push(Rect.create(column * 200, 200, 200, 200));
}

const characterNode = new WorldNode();
characterNode.addComponent(Sprite);
const spriteAnimator = characterNode.addComponent(SpriteAnimator);
spriteAnimator.addClip("idle", image, idleRects, 6, true);
spriteAnimator.addClip("attack", image, attackRects, 12, false);
spriteAnimator.playClip("idle");

class MainScene extends Scene {
	initialize(engine) {
		super.initialize(engine);
		characterNode.setPivot(Pivot.middleCenter);
		characterNode.setLocalPosition(Vector2.create(320, 180));
		characterNode.setContentSize(Vector2.create(200, 200));
		const root = this.getRoot();
		root.addChild(characterNode);
	}

	touchPress(viewInputPosition) {
		super.touchPress(viewInputPosition);
		spriteAnimator.playClip("attack", {
			onComplete: () => {
				spriteAnimator.playClip("idle");
			},
		});
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
생성자constructorconstructor()#0.3.0 부터since 0.3.0소스source

생성.

Constructor.

짝Counterpart: vanilla-engine SpriteAnimator.new

정적staticstatic play(node, clipName, options = {})#0.3.0 부터since 0.3.0소스source

노드의 애니메이터로 재생. (정적, 같은 노드의 Sprite 에 그 자리에서 첫 프레임을 넣는다, 노드에 애니메이터가 없으면 false)

Play with the node's animator. (static, puts the first frame into the Sprite on the same node right away, false if the node has no animator)

nodeobject
SpriteAnimator 를 붙인 노드.Node with a SpriteAnimator attached.
clipNamestring
재생할 클립 이름. (등록하지 않은 이름이면 false)Name of the clip to play. (false if the name is not registered)
optionsobject
{ onComplete?, restartIfSame = false }

반환Returnsboolean 재생 시작 여부.Whether playback started.

짝Counterpart: vanilla-engine SpriteAnimator.play

메서드methodaddClip(clipName, image, frameRects, fps = 10, isLoop = true)#0.3.0 부터since 0.3.0소스source

클립 등록.

Registers a clip.

clipNamestring
클립 이름.Clip name.
imageHTMLImageElement
프레임을 잘라 쓸 이미지.Image to cut the frames from.
frameRectsRect[]
프레임마다 이미지 안에서 잘라 쓸 영역. (픽셀)Area to cut from the image for each frame. (pixels)
fpsnumber
초당 프레임 수. (기본 10)Frames per second. (default 10)
isLoopboolean
되풀이하면 참. (기본 참, 거짓이면 마지막 프레임에서 멈추고 onComplete 를 부른다)True to loop. (default true; if false, stops on the last frame and calls onComplete)

짝Counterpart: vanilla-engine SpriteAnimator.add_clip

메서드methodaddClipFromAtlas(clipName, image, atlasJson, frameIds, fps = 10, isLoop = true)#0.3.0 부터since 0.3.0소스source

아틀라스 JSON 으로 클립 등록.

  • TexturePacker 형식({ frames: { 이름: { frame: {x,y,w,h} } } })을 받는다.

Registers clips from an atlas JSON.

  • Accepts the TexturePacker format ({ frames: { name: { frame: {x,y,w,h} } } }).
clipNamestring
클립 이름.Clip name.
imageHTMLImageElement
프레임을 잘라 쓸 이미지.Image to cut the frames from.
atlasJsonobject
TexturePacker 형식 아틀라스 자료.Atlas data in TexturePacker format.
frameIdsstring[]
차례대로 쓸 프레임 이름 목록. (아틀라스에 없는 이름은 건너뛴다)List of frame names to use in order. (names not in the atlas are skipped)
fpsnumber
초당 프레임 수. (기본 10)Frames per second. (default 10)
isLoopboolean
되풀이하면 참. (기본 참, 거짓이면 마지막 프레임에서 멈추고 onComplete 를 부른다)True to loop. (default true; if false, stops on the last frame and calls onComplete)

짝Counterpart: vanilla-engine SpriteAnimator.add_clip_from_atlas

예제Example아틀라스 JSON 의 프레임 이름으로 클립 만들기Make a clip from frame names in an atlas JSON
import { SpriteAnimator, ImageAsset } from "./build/vanilla.js";

const imageAsset = new ImageAsset();
await imageAsset.load("examples/assets/spritesheet.png");
const image = imageAsset.getImage();
const atlasJson = {
	frames: {
		"run_0": { frame: { x: 0, y: 400, w: 200, h: 200 } },
		"run_1": { frame: { x: 200, y: 400, w: 200, h: 200 } },
		"run_2": { frame: { x: 400, y: 400, w: 200, h: 200 } },
	},
};
const spriteAnimator = new SpriteAnimator();
spriteAnimator.addClipFromAtlas("run", image, atlasJson, ["run_0", "run_1", "run_2"], 12, true);
console.log(spriteAnimator.hasClip("run"), spriteAnimator.playClip("run"));
spriteAnimator.tick(0.1);
console.log(spriteAnimator.getClipName(), spriteAnimator.getCurrentFrameIndex());
메서드methodplayClip(clipName, options = {})#0.4.0 부터since 0.4.0소스source

애니메이터로 재생. (러스트 판 play_clip)

  • 같은 클립이 이미 도는 중이면 다시 시작하지 않는다. (restartIfSame 이 참이면 재시작)
  • 같은 노드의 Sprite 에 그 자리에서 첫 프레임을 넣는다.

Play with the animator. (the Rust play_clip)

  • If the same clip is already playing, it does not restart. (restarts if restartIfSame is true)
  • Puts the first frame into the Sprite on the same node right away.
clipNamestring
재생할 클립 이름. (등록하지 않은 이름이면 false)Name of the clip to play. (false if the name is not registered)
optionsobject
{ onComplete?, restartIfSame = false }

반환Returnsboolean 재생 시작 여부.Whether playback started.

짝Counterpart: vanilla-engine SpriteAnimator.play_clip

메서드methodstop()#0.3.0 부터since 0.3.0소스source

정지.

Stop.

짝Counterpart: vanilla-engine SpriteAnimator.stop

메서드methodtick(timeDelta)#0.3.0 부터since 0.3.0소스source

갱신.

Update.

timeDeltanumber
지난 프레임부터의 시간. (초)Time since the last frame. (Seconds)
메서드methodapplyCurrentFrame()#0.3.0 부터since 0.3.0소스source

현재 프레임을 스프라이트에 반영.

Applies the current frame to the sprite.

짝Counterpart: vanilla-engine SpriteAnimator.apply_current_frame

메서드methodfindSprite()#0.3.0 부터since 0.3.0소스source

같은 노드의 스프라이트 찾기. (한 번 찾으면 캐시)

Finds the sprite on the same node. (cached once found)

반환ReturnsSprite | null 같은 노드의 첫 Sprite 컴포넌트. (없거나 노드에 붙지 않았으면 null)The first Sprite component on the same node. (null if none or not attached to a node)

짝Counterpart: vanilla-engine SpriteAnimator.find_sprite

메서드methodgetClipName()#0.3.0 부터since 0.3.0소스source

현재 클립 이름 반환.

Returns the current clip name.

반환Returnsstring | null 지금 클립 이름. (재생한 적이 없으면 null)Current clip name. (null if nothing has been played)

짝Counterpart: vanilla-engine SpriteAnimator.get_clip_name

메서드methodisPlaying()#0.3.0 부터since 0.3.0소스source

재생 중 여부.

Whether playing.

반환Returnsboolean 재생 중이면 참.True if playing.

짝Counterpart: vanilla-engine SpriteAnimator.is_playing

메서드methodgetCurrentFrameIndex()#0.4.0 부터since 0.4.0소스source

지금 프레임 번호 반환. (클립 안에서 0 부터)

Returns the current frame number. (from 0 within the clip)

반환Returnsnumber 지금 프레임 번호. (클립 안에서 0 부터)Current frame index. (from 0 within the clip)

짝Counterpart: vanilla-engine SpriteAnimator.get_current_frame_index

메서드methodhasClip(clipName)#0.3.0 부터since 0.3.0소스source

클립 존재 여부.

Whether the clip exists.

clipNamestring
찾을 클립 이름.Name of the clip to look up.

반환Returnsboolean 등록된 클립이면 참.True if the clip is registered.

짝Counterpart: vanilla-engine SpriteAnimator.has_clip