pixijs-scene-sprite
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePixiJS has three sprite classes for different drawing tasks. is the default image-drawing leaf; is a resizable UI-panel variant that preserves corner art; repeats a texture across an area. The subclass of cycles through texture frames for frame-based animation.
SpriteNineSliceSpriteTilingSpriteAnimatedSpriteSpriteAssumes familiarity with . All sprite classes are leaf nodes; they cannot have children. Wrap multiple sprites in a to group them.
pixijs-scene-core-conceptsContainerPixiJS有三种用于不同绘图任务的精灵类。是默认的图像绘制叶节点;是可调整大小的UI面板变体,能保留边角样式;会在指定区域重复纹理。是的子类,可循环切换纹理帧以实现逐帧动画。
SpriteNineSliceSpriteTilingSpriteAnimatedSpriteSprite本文假设你已熟悉。所有精灵类都是叶节点,无法拥有子元素。若要将多个精灵分组,需将它们包裹在中。
pixijs-scene-core-conceptsContainerQuick Start
快速开始
ts
const texture = await Assets.load("bunny.png");
const sprite = new Sprite({
texture,
anchor: 0.5,
tint: 0xff8888,
});
sprite.x = app.screen.width / 2;
sprite.y = app.screen.height / 2;
app.stage.addChild(sprite);Position is set after construction because depends on the live renderer size. Literal positions can go directly in the options object via / (inherited from ).
app.screen.width / 2xyContainerRelated skills: (leaves, transforms), (texture loading), (thousands of sprites), (spritesheets, batching).
pixijs-scene-core-conceptspixijs-assetspixijs-scene-particle-containerpixijs-performancets
const texture = await Assets.load("bunny.png");
const sprite = new Sprite({
texture,
anchor: 0.5,
tint: 0xff8888,
});
sprite.x = app.screen.width / 2;
sprite.y = app.screen.height / 2;
app.stage.addChild(sprite);需在构造完成后设置位置,因为依赖于渲染器的实时尺寸。固定位置可直接通过/(继承自)写入选项对象。
app.screen.width / 2xyContainer相关技能: (叶节点、变换)、(纹理加载)、(批量精灵)、(精灵表、批处理)。
pixijs-scene-core-conceptspixijs-assetspixijs-scene-particle-containerpixijs-performanceVariants
变体对比
| Variant | Use when | Trade-offs | Reference |
|---|---|---|---|
| Draw a single texture at a position | Fixed size = texture size | references/sprite.md |
| Frame-based animation from a texture array or spritesheet | Pre-rendered frames only; no tweening | references/animated-sprite.md |
| Resizable UI panels, buttons, dialog frames | Border width is fixed; center stretches | references/nineslice-sprite.md |
| Scrolling backgrounds, parallax, repeating patterns | Single texture repeated; | references/tiling-sprite.md |
AnimatedSpriteSpriteSpriteEach variant's constructor options are documented in its sub-reference file (). All variants also accept the options (, , , , , , etc.) — see .
references/{variant}.mdContainerpositionscaletintlabelfilterszIndexskills/pixijs-scene-core-concepts/references/constructor-options.md| 变体类型 | 适用场景 | 优缺点 | 参考文档 |
|---|---|---|---|
| 在指定位置绘制单个纹理 | 尺寸固定为纹理尺寸 | references/sprite.md |
| 通过纹理数组或精灵表实现逐帧动画 | 仅支持预渲染帧;不支持补间动画 | references/animated-sprite.md |
| 可调整大小的UI面板、按钮、对话框边框 | 边框宽度固定;中间区域会被拉伸 | references/nineslice-sprite.md |
| 滚动背景、视差效果、重复图案 | 仅重复单个纹理;通过 | references/tiling-sprite.md |
AnimatedSpriteSpriteSprite每个变体的构造函数选项可在其子参考文档中查看()。所有变体也支持的选项(、、、、、等)——详见。
references/{variant}.mdContainerpositionscaletintlabelfilterszIndexskills/pixijs-scene-core-concepts/references/constructor-options.mdWhen to use what
场景选型指南
- "I want to draw a single image at a position" → . The default choice for 90% of 2D game and app content.
Sprite - "I want to animate a character through a series of frames" → . Load a spritesheet via Assets and pass
AnimatedSprite. Seesheet.animations['walk'].references/animated-sprite.md - "I want a UI button/panel that resizes without stretching the borders" → . Set border widths, then set
NineSliceSprite/width. Seeheight.references/nineslice-sprite.md - "I want a scrolling repeating background" → . Animate
TilingSpriteto scroll. SeetilePosition.references/tiling-sprite.md - "I want thousands of identical sprites" → Use with
ParticleContainerinstances (seeParticle), not plain sprites.pixijs-scene-particle-container - "I want to draw shapes or paths" → Use (see
Graphics), not a sprite.pixijs-scene-graphics
- 「我想在指定位置绘制单张图片」 → 使用。90%的2D游戏和应用内容都会默认选择它。
Sprite - 「我想让角色通过一系列帧实现动画」 → 使用。通过Assets加载精灵表,传入
AnimatedSprite。详见sheet.animations['walk']。references/animated-sprite.md - 「我想要一个可调整大小且边框不会拉伸的UI按钮/面板」 → 使用。设置边框宽度后,再调整
NineSliceSprite/width。详见height。references/nineslice-sprite.md - 「我想要一个可滚动的重复背景」 → 使用。通过动画
TilingSprite实现滚动。详见tilePosition。references/tiling-sprite.md - 「我想要创建数千个相同的精灵」 → 使用搭配
ParticleContainer实例(详见Particle),而非普通精灵。pixijs-scene-particle-container - 「我想要绘制形状或路径」 → 使用(详见
Graphics),而非精灵。pixijs-scene-graphics
Quick concepts
核心概念速览
Anchor vs pivot
Anchor与Pivot的区别
Sprite.anchor[0, 1]Container.pivotanchor.set(0.5)Sprite.anchor[0, 1]Container.pivotanchor.set(0.5)Loading before creating
先加载再创建
Sprite.from(id)await Assets.load(...)Texturenew Sprite(texture)Sprite.from(id)await Assets.load(...)Texturenew Sprite(texture)Dynamic textures
动态纹理
Once a texture is loaded, modifying its or swapping its source does not automatically notify sprites. Set once, or call manually after changes.
frametexture.dynamic = truesprite['onViewUpdate']()纹理加载完成后,修改其或切换源不会自动通知精灵。只需设置一次,或在修改后手动调用。
frametexture.dynamic = truesprite['onViewUpdate']()Common Mistakes
常见错误
[HIGH] Using Texture.from(url)
to load
Texture.from(url)[高风险] 使用Texture.from(url)
加载资源
Texture.from(url)Wrong:
ts
const texture = Texture.from("https://example.com/image.png");Correct:
ts
const texture = await Assets.load("https://example.com/image.png");Texture.from()Assets.load()错误示例:
ts
const texture = Texture.from("https://example.com/image.png");正确示例:
ts
const texture = await Assets.load("https://example.com/image.png");在v8版本中,仅读取缓存。请先使用;其返回值即为纹理。
Texture.from()Assets.load()[HIGH] Confusing anchor and pivot
[高风险] 混淆Anchor与Pivot
Wrong:
ts
sprite.pivot.set(sprite.width / 2, sprite.height / 2);Correct:
ts
sprite.anchor.set(0.5);anchorpivot错误示例:
ts
sprite.pivot.set(sprite.width / 2, sprite.height / 2);正确示例:
ts
sprite.anchor.set(0.5);anchorpivot[HIGH] Old NineSlicePlane
name
NineSlicePlane[高风险] 使用旧名称NineSlicePlane
NineSlicePlaneNineSlicePlaneNineSliceSpritenew NineSliceSprite({ texture, leftWidth, topHeight, rightWidth, bottomHeight })在v8版本中,已更名为,并改用选项对象构造函数:。
NineSlicePlaneNineSliceSpritenew NineSliceSprite({ texture, leftWidth, topHeight, rightWidth, bottomHeight })[MEDIUM] Adding children to a sprite
[中风险] 为精灵添加子元素
SpriteNineSliceSpriteTilingSpriteallowChildren = falseContainerSpriteNineSliceSpriteTilingSpriteallowChildren = falseContainer