Skip to main content

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.

当你初始化 Application 时,PixiJS 会自动包含此插件,但你也可以选择不使用并手动添加它。

🌐 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

  • 如果使用共享计时器,其他代码也可能在注册更新,因此执行顺序可能会有所不同。
  • 如果使用自定义计时器,你可以完全控制时间和更新顺序。

生命周期控制

🌐 Lifecycle Control

你可以手动停止和启动 ticker:

🌐 You can manually stop and start the ticker:

app.stop(); // Stop automatic rendering
app.start(); // Resume

这适用于:

🌐 This is useful for:

  • 暂停游戏或动画
  • 非活动选项卡的性能限制
  • 管理可见性事件

API 参考

🌐 API Reference