Created
March 6, 2021 22:03
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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