从…开始v2.9.0BeeEngine支持插件。引擎保持“盲目”:它不导入或了解任何插件。每个插件都用attach(engine)和删除自己detach().第一个官方插件是BeeLocale:让你的画布游戏多语言化的小帮手。npm install beeengine2.9.01.问题是:硬编码文本// ❌ Before: to translate the game you have to hunt every string in your codectx.fillText(Score: ’ score, 20, 30);ctx.fillText(‘Game Over’, 300, 200);ctx.fillText(‘Press SPACE to restart’, 260, 240);2.解决方案:BeeLocaleimport { BeeEngine } from ‘beeengine’;import { BeeLocale } from ‘beeengine/plugins’;const canvas document.getElementById(‘game’);const engine new BeeEngine(canvas, 800, 600);// All translations in one placeconst locale new BeeLocale({language: ‘it’, // active languagefallback: ‘en’, // backup languagestrings: {en: {score: ‘Score’,gameover: ‘Game Over’,restart: ‘Press SPACE to restart’},it: {score: ‘Punteggio’,gameover: ‘Hai perso!’,restart: ‘Premi SPAZIO per ricominciare’}}});// The plugin registers itself in the engine as ‘locale’locale.attach(engine);3.在你画文字的任何地方使用它// ✅ After: only keys in your codectx.fillText(locale.t(‘score’) : ’ score, 20, 30);ctx.fillText(locale.t(‘gameover’), 300, 200);ctx.fillText(locale.t(‘restart’), 260, 240);get(key)和…完全一样t(key)如果你喜欢这个名字:locale.get(‘gameover’); // “Hai perso!”你也可以在游戏的任何地方从引擎中抓取它:const loc engine.plugin(‘locale’);loc.t(‘score’); // “Punteggio”4.即时切换语言document.getElementById(‘btn-en’).onclick () locale.setLanguage(‘en’);document.getElementById(‘btn-it’).onclick () locale.setLanguage(‘it’);// Or automatically, from the browser languagelocale.setLanguage(navigator.language.slice(0, 2)); // “it”, “en”, “fr”…下一帧已经用新语言绘制了文本。5.Fallback:从不为空字符串BeeLocale按以下顺序查找一个键:主动语言 (it)后备语言 (en)钥匙本身左侧可见const locale new BeeLocale({language: ‘fr’,fallback: ‘en’,strings: {en: { score: ‘Score’, lives: ‘Lives’ },fr: { score: ‘Score’ } // “lives” is missing in French}});locale.t(‘score’); // “Score” → found in Frenchlocale.t(‘lives’); // “Lives” → missing in French, falls back to Englishlocale.t(‘bonus’); // “bonus” → missing everywhere, shows the key丢失的翻译会立即出现在屏幕上https://www.iissbbs.com而不是无声无息地消失。空字符串(‘’)也算失踪。6.分离插件locale.detach();engine.plugin(‘locale’); // null引擎盖下:插件合同每个插件只需要两个方法:attach(engine)和detach().class MyPlugin {attach(engine) {this.engine engine;engine.registerPlugin(‘my-plugin’, this);return this;}detach() { if (this.engine) this.engine.unregisterPlugin(my-plugin); this.engine null; return this; }}插件不能呼叫engine.loop()覆盖update/render或者修补主循环。如果它需要每帧运行一些东西它使用onTick它返回一个关闭挂钩的函数:class FrameCounter {attach(engine) {this.engine engine;this.frames 0;engine.registerPlugin(‘frames’, this);this.off engine.onTick(() { this.frames; });return this;}detach() { if (this.off) this.off(); // always turn the hook off if (this.engine) this.engine.unregisterPlugin(frames); this.engine null; return this; }}BeeLocale不使用onTick一点也不:它只在你打电话的时候起作用t()所以每帧不用花钱。