Author Topic: FlxSubState - (dialog and pause menus with ease!)  (Read 7647 times)

IQAndreas

  • Member
  • **
  • Posts: 35
  • Karma: +0/-0
    • View Profile
    • IQAndreas.com
These changes to Flixel allow for "SubStates", where another state is displayed on top of the current one, and has the capability of having a different background color, as well as preventing the "parent state" from updating or receiving keyboard and mouse input.

I still haven't merged the changes to "my" master flixel branch, but instead still have everything stored in a separate branch:
https://github.com/IQAndreas/flixel/tree/dev_FlxSubState

The example game (a branch of AdamAtomic's EZPlatformer) can be found here (with the sample SWF in the "bin" folder):
https://github.com/IQAndreas/EZPlatformer


DETAILS
You can set the sub state from any FlxState as follows:
Code: [Select]
this.setSubState(new PauseMenu(), <optional callback when the menu closes>);
The current "FlxState" (likely named "GameState") is still running in the background and still in memory. The new FlxSubState (named "PauseMenu" in the example) does not replace "FlxG.currentState", but is in a sense a "child" of the current GameState instance. (but it won't show up in the list of the GameState members and interfere with other children)

You can extend "FlxSubState" which works just like a "FlxState" but adds a "isBlocking" option which if set to true prevents the parent FlxState from running it's "update" method. (Note that the parent state's "draw" method is always run, even if the subState is blocking the update command)

So for instance, the "Pause Menu" in MineCraft would have "isBlocking" set to true, so the "game state" stops.
The "Opening Chest" menu in MineCraft would not be blocking, since the world keeps playing in the background.

 - FlxSubState can have a different background color (even transparent or partially transparent, so the "parent state" still shows up behind it.)
 - The SubState will ALWAYS be rendered on top of the parent state.
 - When opening a FlxSubState, you can add a callback for when the state closes.
 - When the FlxSubState closes, you can pass a string to the parent state with information (for instance, why the state closed).

Nitty gritty:
 - The SubState does not have a public reference to it's parent. This is to adhere to proper encapsulation so the SubState doesn't need to know anything about it's parent state. You can however pass parameters to the constructor of the SubState if needed.
 - You can use the FlxSubState as a regular FlxState without any problems. Just make sure you override the "close()" function to handle the behavior properly. You can use "isSubState" to see if the current instance is the main state, or a sub state.
 - There are two additional parameters added to the standard "FlxState" class, "bgColor" and "useMouse". Instead of using the "FlxG" commands for setting those two properties, this will ensure that FlxSubStates also have those properly set.
 - If a parent state has "useMouse" set to false, but the sub state has it to true (such as if there are buttons on the screen), when the sub state closes, it will go back to the parent's mouse settings, hiding the mouse (and vice versa)

KunoNoOni

  • "Never tell me the odds"
  • Active Member
  • ***
  • Posts: 170
  • Karma: +0/-0
  • Code is Love
    • View Profile
Re: FlxSubState - (dialog and pause menus with ease!)
« Reply #1 on: Thu, Dec 22, 2011 »
Wow! That is pretty cool, I'm sure we'll get alot of use out of it :)


-KunoNoOni

c023-DeV

  • Game Artisan
  • Active Member
  • ***
  • Posts: 176
  • Karma: +0/-0
    • View Profile
    • DeV-ZoO
Re: FlxSubState - (dialog and pause menus with ease!)
« Reply #2 on: Mon, Jan 9, 2012 »
Ok, got it working. Nice addOn! Thx!
If you aint got no dedication, you won't get no education!

IQAndreas

  • Member
  • **
  • Posts: 35
  • Karma: +0/-0
    • View Profile
    • IQAndreas.com
Re: FlxSubState - (dialog and pause menus with ease!)
« Reply #3 on: Mon, Jan 9, 2012 »
Ok, got it working. Nice addOn! Thx!
Awesome. :) If you get it working in a game where you will be releasing the source, let me know and I can link to it as an example of how to use the library (in case the EZPlatformer game I modified isn't enough)

If you have any feature requests or bugs let me know either here or on GitHub.

JAH2488

  • Member
  • **
  • Posts: 32
  • Karma: +0/-0
    • View Profile
Re: FlxSubState - (dialog and pause menus with ease!)
« Reply #4 on: Mon, Jan 9, 2012 »
Hey IQAndreas,

Are your additions to Github plugins or are they a branch of the main repository?

IQAndreas

  • Member
  • **
  • Posts: 35
  • Karma: +0/-0
    • View Profile
    • IQAndreas.com
Re: FlxSubState - (dialog and pause menus with ease!)
« Reply #5 on: Mon, Jan 9, 2012 »
Are your additions to Github plugins or are they a branch of the main repository?
There was no easy way to add this capability without modifying core classes, so I'm afraid I had to create a new branch.

The branch is updated to the latest release version of Flixel (2.55 is it?).
I have been meaning to add all the latest bug fixes (the unofficial bug fixes), but have not yet.

JAH2488

  • Member
  • **
  • Posts: 32
  • Karma: +0/-0
    • View Profile
Re: FlxSubState - (dialog and pause menus with ease!)
« Reply #6 on: Mon, Jan 9, 2012 »
There was no easy way to add this capability without modifying core classes, so I'm afraid I had to create a new branch.

The branch is updated to the latest release version of Flixel (2.55 is it?).
I have been meaning to add all the latest bug fixes (the unofficial bug fixes), but have not yet.

I thought so, I was just wondering. Are you planning on issuing some pull requests to the main repo or are you going to try and just keep updating your branch(es) along side the master branch as it gets updated?

IQAndreas

  • Member
  • **
  • Posts: 35
  • Karma: +0/-0
    • View Profile
    • IQAndreas.com
Re: FlxSubState - (dialog and pause menus with ease!)
« Reply #7 on: Mon, Jan 9, 2012 »
Are you planning on issuing some pull requests to the main repo or are you going to try and just keep updating your branch(es) along side the master branch as it gets updated?
I have been sending pull requests for the bug fixes, but not any of my other branches. (I figure I bug him enough with my pull requests for bug fixes anyway, :P and none of my changes actually are needed, but are instead nice little additions or handy changes.)

But if AdamAtomic makes any further updates, I will of course pull those changes in to my branches as soon as possible.

I made a list of my branches since the "about" field on GitHub isn't big enough:
http://forums.flixel.org/index.php/topic,5693.0.html
Plus a fancy little chart of the branches courtesy of GitHub:
https://github.com/IQAndreas/flixel/network
« Last Edit: Mon, Jan 9, 2012 by IQAndreas »

JAH2488

  • Member
  • **
  • Posts: 32
  • Karma: +0/-0
    • View Profile
Re: FlxSubState - (dialog and pause menus with ease!)
« Reply #8 on: Mon, Jan 9, 2012 »
I have been sending pull requests for the bug fixes, but not any of my other branches. (I figure I bug him enough with my pull requests for bug fixes anyway, :P and none of my changes actually are needed, but are instead nice little additions or handy changes.)

But if AdamAtomic makes any further updates, I will of course pull those changes in to my branches as soon as possible.

I made a list of my branches since the "about" field on GitHub isn't big enough:
http://forums.flixel.org/index.php/topic,5693.0.html
Plus a fancy little chart of the branches courtesy of GitHub:
https://github.com/IQAndreas/flixel/network

Awesome. Very Cool. Oh, you could take the contents of that forum post and put it at the top of the Flixel readme.txt and then it'll be on the page atleast. You could even change the readme to a markdown file and get some nice links and bolds and underlines working in the read me.

paala

  • Contributor
  • ****
  • Posts: 250
  • Karma: +0/-1
    • View Profile
Re: FlxSubState - (dialog and pause menus with ease!)
« Reply #9 on: Wed, Mar 21, 2012 »
Code: [Select]
text.scrollFactor.x = text.scrollFactor.y = 0; should be added  to PauseMenu.as for a game that  has scrolling. also the same for the buttons.