制作一个可以重复使用的“FlxSubState”,而不会在第二个“打开”时崩溃?
通常,当我想打开在代码中定义的 FlxSubState
时,我将使用:
openSubState(new MySubState());
我的所有创建/添加逻辑都在 MySubState.create()
中
这工作得很好,除了,如果该子状态上有很多东西,它可能会导致巨大的滞后尖峰,使游戏在显示子状态之前“冻结”。
如果我尝试为我的子状态设置一个变量并重新使用它,它会工作一次,但是当我尝试第二次打开它时游戏崩溃,因为子状态正在被自动销毁关闭。
Typically, when I want to open a FlxSubState
that I have defined in my code, I will use:
openSubState(new MySubState());
I have all of my create/add logic inside MySubState.create()
Which works fine, except, if there is a lot of stuff on that SubState it can cause a huge lag spike making the game 'freeze' before the substate is displayed.
If I try set a variable to my substate and re-use it, it works once, but then crashes the game when I try to open it a second time, because the substate is being auto-destoyed on close.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,当
FlxSubState
关闭时,它也会被destroy()
ed - 删除所有添加的对象等。此外,由于
new
和create
仅在第一次打开子状态时调用,您在其中添加
的任何内容都不会被重新添加(并且您不希望有new
/create
调用每个 解决方案很简单:在打开子状态的
FlxState
中,设置了一个标志destroySubStates
设置为false
并且子状态不会在关闭时被销毁。...以及如何在打开之间更改子状态?您可以在
FlxSubState
中使用openCallback
,当您打开子状态时,在调用create
之后(如果有的话)和子状态之前,它会被触发显示。所以:
如果你想要一个更新的 FlxSubState 可以重复使用来减少打开时的“延迟”,这就是我所做的:
在我的 PlayState 中:
在 MySubState 中:
并且这会在每次打开时消除延迟尖峰亚国家!
...只需记住在
PlayState.destroy();
中调用mySubState.destroy();
即可正确清理!By default, when a
FlxSubState
closes, it isdestroy()
ed as well - removing all added objects, etc.Also, since
new
andcreate
are only called the very first time the substate is opened, anything youadd
there doesn't get re-added (and you don't want to havenew
/create
called every time you open the substate, since that would not stop the lag-spike)The solution is simple: in the
FlxState
that is opening your substate, there is a flagdestroySubStates
set this tofalse
and the substates will not be destroyed on close....and how do you make changes to the substate between opens? You can use the
openCallback
inFlxSubState
which gets triggered when you open the substate, aftercreate
gets called (if it does) and before the substate is displayed.So:
If you want to have an updating FlxSubState that can be re-used to cut down on 'lag' when it is opened, here's what I did:
in my PlayState:
in MySubState:
AND this cuts out the lag spike everytime you open the substate!
...just remember to call
mySubState.destroy();
inside yourPlayState.destroy();
to clean up properly!