user-interfacegame-engineflixel

Flixel Game Over Screen


I am new to game development but familiar with programming languages. I have started using Flixel and have a working Breakout game with score and lives.

I am just stuck on how I can create a new screen/game over screen if a player runs out of lives. I would like the process to be like following:

  1. Check IF lives are equal to 0
  2. Pause the game and display a new screen (probably transparent) that says 'Game Over'
  3. When a user clicks or hits ENTER restart the level

Here is the function I currently have to update the lives:

private function loseLive(_ball:FlxObject, _bottomWall:FlxObject):void
{
    // check for game over
    if (lives_count == 0)
    {

    }
    else 
    {
        FlxG:lives_count -= 1;
        lives.text = 'Lives: ' + lives_count.toString()
    }
}

Here is my main game.as:

package
{
    import org.flixel.*;

    public class Game extends FlxGame
    {
        private const resolution:FlxPoint = new FlxPoint(640, 480);
        private const zoom:uint = 2;
        private const fps:uint = 60;

        public function Game()
        {
            super(resolution.x / zoom, resolution.y / zoom, PlayState, zoom);
            FlxG.flashFramerate = fps;
        }
    }
}

Solution

  • There are multiple ways to go about doing this...

    You could use different FlxStates, like I described in the answer to your other post: Creating user UI using Flixel, although you'll have to get smart with passing the score or whatever around, or use a Registry-type setup

    If you want it to actually work like you described above, with a transparent-overlay screen, you can try something like this (keep in mind, the exact details may differ for your project, I'm just trying to give you an idea):

    First, make sure you have good logic for starting a level, lets say it's a function called StartLevel.

    You'll want to define a flag - just a Boolean - that tracks whether or not the game is still going on or not: private var _isGameOver:Boolean; At the very end of StartLevel(), set this to false.

    In your create() function for your PlayState, build a new FlxGroup which has all the things you want on your Game Over screen - some text, an image, and something that says "Press ENTER to Restart" (or whatever). Then set it to visible = false. The code for that might look something like:

    grpGameOver = new FlxGroup();
    grpGameOver.add(new FlxSprite(10,10).makeGraphic(FlxG.Width-20,FlxG.Height-20,0x66000000));  // just a semi-transparent black box to cover your game screen.
    grpGameOver.add(new FlxText(...)); // whatever you want to add to the group...
    grpGameOver.visible = false;
    add(grpGameOver); // add the group to your State.
    

    Depending on how your game is setup, you may also want to set the objects in your group's scrollFactor to 0 - if your game screen scrolls at all:

    grpGameOver.setAll("scrollFactor", new FlxPoint(0,0));
    

    In your update() function, you'll need to split it into 2 parts: one for when the game is over, and one for if the game is still going on:

    if (_isGameOver)
    {
        if (FlxG.keys.justReleased("ENTER"))
        {
            grpGameOver.visible = false;
            StartLevel();
        }
    }
    else
    {
        ... the rest of your game logic that you already have ...
    }
    super.update();
    

    Keep in mind, if you have things that respond to user input anywhere else - like a player object or something, you might need to change their update() functions to check for that flag as well.

    Then, the last thing you need to do is in your loseLive() logic:

    if (lives_count == 0)
    {
        _isGameOver = true;
        grpGameOver.visible = true;
    }
    else 
    ...
    

    That should do it!

    I would highly recommend spending some time with different tutorials and sample projects to kind of get a better feel for Flixel in general. Photon Storm has some great material to play with (even though he's jumped over to HTML5 games)

    I also want to note that if you get comfortable with the way Flixel handles updates, you can get really smart with your state's update() function and have it only call update on the grpGameOver objects, instead of having to change all your other objects updates individually. Pretty advanced stuff, but can be worth it to learn it.