Spine 2D
如何在 Pixi’VN 中使用 Spine 2D 集成来加载骨骼、切换皮肤、播放和组合动画,以及控制动画轨道。
Spine 2D 是一款专为游戏开发设计的强大 2D 动画软件。它使用骨骼动画系统,这意味着角色和物体是通过控制附加部件运动的骨骼层级来进行动画的。
你可以在Spine 2D 官方网站了解更多关于 Spine 2D 的信息。
在你的 Pixi’VN 项目中,你可以使用 Spine 2D 集成为角色和物体创建复杂而流畅的动画。该集成本质上是对官方 PixiJS 版 Spine 2D 运行时 的封装,让你可以直接在 Pixi’VN 项目中使用所有 Spine 2D 功能。
安装
要在现有的 JavaScript 项目中安装 Spine 2D 包,请使用以下命令之一:
npm install @drincs/pixi-vn-spine该库必须在游戏初始化时导入,以便可以在 Lazy 组件内部使用。
import "@drincs/pixi-vn-spine";
Game.init(body, {
// ...
});你可以通过 createSpineHandler 函数,在你的 ink 脚本中启用 Spine 2D 井号命令。
import { addBaseHashtagCommands } from "@drincs/pixi-vn-ink";
import { createSpineHandler } from "@drincs/pixi-vn-spine/ink";
addBaseHashtagCommands({ bundleIds, assetAliasIds });
createSpineHandler(); show、edit 和 remove 井号命令也可以与 spine 一起使用,就像其他任何画布元素一样。
Pixi’VN Spine ships a set of Agent Skills that teach AI coding assistants how to correctly use the Spine 2D integration.
In LM Studio Bionic, ask the agent to install skills from
https://github.com/DRincs-Productions/pixi-vn-spine, or add the skill files in Settings → Skills. 要复用为 Codex 或 Claude Code 安装的技能,请启用 Settings → Skills → Use skills found in other apps。
Codex:
npx skills@latest add DRincs-Productions/pixi-vn-spine@latest --skill '*' --agent codex -g -yClaude Code:
npx skills@latest add DRincs-Productions/pixi-vn-spine@latest --skill '*' --agent claude-code -g -yOther agents:
npx skills@latest add DRincs-Productions/pixi-vn-spine@latest --all -y使用方法
你可以像使用其他任何 Pixi’VN 组件一样使用 Spine 组件。不过,在使用之前,你必须先加载 Spine 2D 资源(skeleton 和 atlas)。
例如:
import { Assets, canvas } from "@drincs/pixi-vn";
import { Spine } from "@drincs/pixi-vn-spine";
await Assets.load(["spineboySkeleton", "spineboyAtlas"]);
const spine = new Spine({
atlas: "spineboyAtlas",
skeleton: "spineboySkeleton",
xAlign: 0.5,
yAlign: 1,
animation: "idle",
});
canvas.add("boy", spine);一个 Spine 模型可以拥有多个皮肤,你可以使用 setSkin 函数在运行时切换它们。
import { Assets, canvas, newLabel } from "@drincs/pixi-vn";
import { Spine } from "@drincs/pixi-vn-spine";
export const startLabel = newLabel("start", [
async () => {
await Assets.load(["goblinsSkeleton", "goblinsAtlas"]);
const spine = new Spine({
atlas: "goblinsAtlas",
skeleton: "goblinsSkeleton",
skin: "goblin",
xAlign: 0.5,
yAlign: 1,
animation: "walk",
});
canvas.add("goblin", spine);
},
() => {
canvas.find<Spine>("goblin")?.setSkin("goblingirl");
},
]);有两个函数可用于播放 Spine 动画:setAnimation 和 addAnimation。
setAnimation会立即用新动画替换轨道上当前正在播放的任何动画。addAnimation会将新动画加入队列,使其仅在轨道上当前动画结束后才开始播放,而不会中断当前动画。
import { Assets, canvas, newLabel } from "@drincs/pixi-vn";
import { Spine } from "@drincs/pixi-vn-spine";
export const startLabel = newLabel("start", [
async () => {
await Assets.load(["spineboySkeleton", "spineboyAtlas"]);
const spine = new Spine({
atlas: "spineboyAtlas",
skeleton: "spineboySkeleton",
xAlign: 0.5,
yAlign: 1,
animation: "idle",
});
canvas.add("boy", spine);
},
() => {
canvas.find<Spine>("boy")?.addAnimation("walk", { loop: true });
},
]);你可以将 Spine 动画与 Pixi’VN 经典的基于运动的动画(canvas.animate)结合使用,如下例所示。
对于更高级的游戏,你也可以直接通过由事件(例如按钮点击)触发的计时器来驱动 Spine 动画,而不仅仅依赖预定义的序列。
import { Assets, canvas, newLabel } from "@drincs/pixi-vn";
import { Spine } from "@drincs/pixi-vn-spine";
export const startLabel = newLabel("start", [
async () => {
await Assets.load(["spineboySkeleton", "spineboyAtlas"]);
const spine = new Spine({
atlas: "spineboyAtlas",
skeleton: "spineboySkeleton",
xAlign: 0,
yAlign: 1,
animation: "walk",
});
canvas.add("boy", spine);
canvas.animate(
spine,
[
[{ xAlign: 1 }, { duration: 1, ease: "linear" }],
[{ scaleX: -1 }, { duration: 0.2 }],
[{ xAlign: 0 }, { duration: 1, ease: "linear" }],
[{ scaleX: 1 }, { duration: 0.2 }],
],
{ repeat: Infinity },
);
},
]);你也可以使用 playSequence 函数来创建动画序列。
import { Assets, canvas, newLabel } from "@drincs/pixi-vn";
import { Spine } from "@drincs/pixi-vn-spine";
export const startLabel = newLabel("start", [
async () => {
await Assets.load(["spineboySkeleton", "spineboyAtlas"]);
const spine = new Spine({
atlas: "spineboyAtlas",
skeleton: "spineboySkeleton",
xAlign: 0.5,
yAlign: 1,
});
spine.playSequence([["idle", { loop: true, duration: 0.5 }], "jump"], {
repeat: Infinity,
});
canvas.add("boy", spine);
},
]);你可以使用两种方法移除 Spine 动画:clearTrack,用于清除单条轨道上的动画;以及 clearTracks,用于清除所有轨道上的动画。
import { Assets, canvas, newLabel } from "@drincs/pixi-vn";
import { Spine } from "@drincs/pixi-vn-spine";
export const startLabel = newLabel("start", [
async () => {
await Assets.load(["spineboySkeleton", "spineboyAtlas"]);
const spine = new Spine({
atlas: "spineboyAtlas",
skeleton: "spineboySkeleton",
xAlign: 0,
yAlign: 1,
animation: "walk",
});
canvas.add("boy", spine);
},
() => {
canvas.find<Spine>("boy")?.clearTracks();
},
]);