我想在预加载状态下的图像/精灵添加a
到phaserGameInstance.world
切换到状态之后b
(其中,phaserGameInstance
是的一个实例Phaser.Game
)。
由于所有资产都全局存储在浏览器缓存中,phaserGameInstance.cache
因此应在所有状态下都可用,但实际上并非如此。
我为此找到的解决方法是使用状态缓存对象b
的属性扩展状态缓存对象,a
这有点令人不安,而且可能不是预期的方式:
var priorCache = phaserGameInstance.state.getCurrentState().cache; // State 'a'
phaserGameInstance.state.start('b'); // Switch to state 'b'
jQuery.extend( // Merges the cache of state 'a' recursively
true, // into the cache of the current state 'b'
phaserGameInstance.state.getCurrentState().cache,
priorCache
);
我还没有测试当同时预加载状态时的资产时是否可行b
(也许合并过程会覆盖state的属性b
),但是由于我只在状态中预加载了我的东西,a
这就是我当前正在使用的解决方案。
如何独立于状态使用预加载资产?
调用phaserGameInstance.state.start('b')
实际上不会将状态从切换a
为b
。它只是将状态分配b
为要切换到的挂起状态。这意味着它某种程度上是一种异步方法。
phaserGameInstance.world
在调用之后向右添加对象phaserGameInstance.state.start('b')
会a
在状态b
仍处于待处理状态时将对象添加到状态,并将在下一次游戏更新中激活。在该更新之后phaserGameInstance.world
为空,因为状态a
关闭时所有对象都将被删除。因此,无需合并缓存或其他任何内容。
解决方法如下:
phaserGameInstance.state.add(
'b',
{
create: function () { // Add the assets loaded in state 'a' here ...
phaserGameInstance.add.image(0, 0, 'myImage');
}
}
);
phaserGameInstante.state.start('b');
// ... not here! State 'a' is still active in this line.
自从将对象添加到状态以来a
,似乎它们将依赖于已预加载的状态,但实际上,它们是状态独立的,如我所问的那样。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句