Ticker 插件
¥Ticker Plugin
TickerPlugin
为你的 PixiJS Application
提供了内置更新循环。此循环以固定的节奏(默认情况下,每个动画帧调用一次)调用 .render()
,并与 PixiJS 的 Ticker
系统集成,以精确控制基于帧的更新。
¥The TickerPlugin
provides a built-in update loop for your PixiJS Application
. This loop calls .render()
at a regular cadence—by default, once per animation frame—and integrates with PixiJS's Ticker
system for precise control over frame-based updates.
PixiJS 会在你初始化 Application
时自动包含此插件,但你也可以选择退出并手动添加。
¥PixiJS includes this plugin automatically when you initialize an Application
, but you can also opt out and add it manually.
用法
¥Usage
const app = new Application();
await app.init({
sharedTicker: false,
autoStart: true,
});
app.ticker.add((ticker) => {
// Custom update logic here
bunny.rotation += 1 * ticker.deltaTime;
});
默认行为
¥Default Behavior
除非禁用,否则会自动包含 TickerPlugin
:
¥The TickerPlugin
is included automatically unless disabled:
const app = new Application();
await app.init({
autoStart: true, // Automatically starts the render loop
sharedTicker: false, // Use a dedicated ticker
});
手动注册
¥Manual Registration
如果你自行管理扩展:
¥If you're managing extensions yourself:
import { extensions, TickerPlugin } from 'pixi.js';
extensions.add(TickerPlugin);
共享 vs 自定义 Ticker
¥Shared vs Custom Ticker
该插件支持两种模式:
¥The plugin supports two modes:
选项 | 描述 |
---|---|
sharedTicker: true | 使用 Ticker.shared ,所有应用共享。 |
sharedTicker: false | 创建一个作用域为应用的私有代码实例。 |
行为差异
¥Behavior Differences
-
如果使用共享 ticker,其他代码可能也会注册更新,因此执行顺序可能会有所不同。
¥If using a shared ticker, other code may also be registering updates, so the order of execution can vary.
-
如果使用自定义 ticker,你可以完全控制时间和更新顺序。
¥If using a custom ticker, you get complete control over timing and update order.
生命周期控制
¥Lifecycle Control
你可以手动停止和启动 ticker:
¥You can manually stop and start the ticker:
app.stop(); // Stop automatic rendering
app.start(); // Resume
这适用于:
¥This is useful for:
-
暂停游戏或动画
¥Pausing the game or animation
-
非活动选项卡的性能限制
¥Performance throttling on inactive tabs
-
管理可见性事件
¥Managing visibility events
API 参考
¥API Reference