Skip to content

Instantly share code, notes, and snippets.

@KinoAR
Created March 6, 2021 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KinoAR/fc6dc6bca41cd4fecf6cab6eba15109c to your computer and use it in GitHub Desktop.
Save KinoAR/fc6dc6bca41cd4fecf6cab6eba15109c to your computer and use it in GitHub Desktop.
An example of a pause menu in HaxeFlixel. Note this won't just work in your game as it contains custom code.
package game.states;
import game.ui.TextButton;
/*
* Custom Pause Scene I made for my game.
*/
class PauseSubState extends FlxSubState {
public var pauseText:FlxText;
private var pauseExitSound:FlxSound;
private var initialPosition:Float;
private var timeCount:Float;
public function new() {
super(KColor.RICH_BLACK_FORGRA_LOW); // Lower Opacity RICH_Black
}
override public function create() {
pauseExitSound = FlxG.sound.load(AssetPaths.pause_out__wav);
FlxG.mouse.visible = true;
pauseText = new FlxText(0, 0, -1, 'Pause', Globals.FONT_L);
pauseText.screenCenter();
pauseText.y -= 30;
pauseText.scrollFactor.set(0, 0);
initialPosition = pauseText.y;
add(pauseText);
var resumeButton = new TextButton(0, 0, 'Resume', Globals.FONT_N,
resumeGame);
resumeButton.screenCenter();
resumeButton.y += 40;
resumeButton.hoverColor = KColor.BURGUNDY;
resumeButton.clickColor = KColor.BURGUNDY;
var returnToTitleButton = new TextButton(0, 0, 'To Title',
Globals.FONT_N, toTitle);
returnToTitleButton.screenCenter();
returnToTitleButton.y += 80;
returnToTitleButton.hoverColor = KColor.BURGUNDY;
returnToTitleButton.clickColor = KColor.BURGUNDY;
add(resumeButton);
add(returnToTitleButton);
super.create();
}
override public function update(elapsed:Float) {
super.update(elapsed);
updatePausePosition(elapsed);
}
public function updatePausePosition(elapsed:Float) {
timeCount += elapsed;
pauseText.y = initialPosition + (30 * Math.sin(timeCount));
if (timeCount > 30) {
timeCount = 0;
}
}
//Resume game closes the substate allowing you to return to the previous scene.
public function resumeGame() {
pauseExitSound.play();
close();
}
//The same as above happens here for toTitle. The Substate will close, but also switch to a main state like
//the titleState/Scene.
public function toTitle() {
pauseExitSound.play();
FlxG.camera.fade(KColor.BLACK, 1, false, () -> {
close();
FlxG.switchState(new TitleState());
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment