制作一个可以重复使用的“FlxSubState”,而不会在第二个“打开”时崩溃?

发布于 2025-01-12 18:12:44 字数 357 浏览 0 评论 0原文

通常,当我想打开在代码中定义的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拔了角的鹿 2025-01-19 18:12:44

默认情况下,当 FlxSubState 关闭时,它也会被 destroy()ed - 删除所有添加的对象等。

此外,由于 newcreate 仅在第一次打开子状态时调用,您在其中添加的任何内容都不会被重新添加(并且您不希望有new/create 调用每个 解决方案很简单:在打开

子状态的 FlxState 中,设置了一个标志 destroySubStates设置为 false 并且子状态不会在关闭时被销毁。

...以及如何在打开之间更改子状态?您可以在FlxSubState中使用openCallback,当您打开子状态时,在调用create之后(如果有的话)和子状态之前,它会被触发显示。

所以:

如果你想要一个更新的 FlxSubState 可以重复使用来减少打开时的“延迟”,这就是我所做的:

在我的 PlayState 中:

override public function create():Void
{
  ...
  mySubState = new MySubState();
  destroySubStates = false;
  ...
}

public function openMySubState():Void
{
  openSubState(mySubState);
}

在 MySubState 中:

public function new():Void
{
  super(0); 
  openCallback = refresh;
}

override public function create():Void
{
  // create and add all of my elements
}

private function refresh():Void
{
  // anything that changed between last time it was open and now, update .text/etc
}

并且这会在每次打开时消除延迟尖峰亚国家!

...只需记住在 PlayState.destroy(); 中调用 mySubState.destroy(); 即可正确清理!

By default, when a FlxSubState closes, it is destroy()ed as well - removing all added objects, etc.

Also, since new and create are only called the very first time the substate is opened, anything you add there doesn't get re-added (and you don't want to have new/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 flag destroySubStates set this to false 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 in FlxSubState which gets triggered when you open the substate, after create 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:

override public function create():Void
{
  ...
  mySubState = new MySubState();
  destroySubStates = false;
  ...
}

public function openMySubState():Void
{
  openSubState(mySubState);
}

in MySubState:

public function new():Void
{
  super(0); 
  openCallback = refresh;
}

override public function create():Void
{
  // create and add all of my elements
}

private function refresh():Void
{
  // anything that changed between last time it was open and now, update .text/etc
}

AND this cuts out the lag spike everytime you open the substate!

...just remember to call mySubState.destroy(); inside your PlayState.destroy(); to clean up properly!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文