4
\$\begingroup\$

I'm in the middle of writing my own little bullet shooter game much in the style of Touhou. Everything works fine so far but I'm disliking certain aspects of my code. Take this function for showing and unshowing the title screen:

    private function runIntro() : void
    {
        if (introFadeInTitle_ && aliceIntroTxt.alpha < 1)
            aliceIntroTxt.alpha += 0.02;
        else if (introFadeInTitle_)
        {
            var timer : Timer = new Timer(1000);
            timer.addEventListener(TimerEvent.TIMER, function() {
                timer.stop();

                introFadeInBtns_  = true;
            });
            timer.start();

            introFadeInTitle_ = false;
        } 
        else if (introFadeInBtns_ && startBtn.alpha < 1)
        {
            startBtn.alpha   += 0.02;
            optionsBtn.alpha += 0.02;
        }
        else if (introFadeInBtns_)
        {
            introFadeInBtns_ = false;

            gameState = GameState.READY;
        }

        if (introFadeOutBtns_ && startBtn.alpha > 0)
        {
            startBtn.alpha   -= 0.02;
            optionsBtn.alpha -= 0.02;
        }
        else if (introFadeOutBtns_)
        {
            var timer : Timer = new Timer(500);
            timer.addEventListener(TimerEvent.TIMER, function() {
                timer.stop();

                introFadeOutTitle_ = true;
            });
            timer.start();

            introFadeOutBtns_ = false;
        }
        else if (introFadeOutTitle_ && aliceIntroTxt.alpha > 0)
            aliceIntroTxt.alpha -= 0.02;
        else if (introFadeOutTitle_)
        {
            var timer : Timer = new Timer(500);
            timer.addEventListener(TimerEvent.TIMER, function() {
                timer.stop();

                introFadeOutBg_ = true;
            });
            timer.start();

            introFadeOutTitle_ = false;
        } 
        else if (introFadeOutBg_ && introBg.alpha > 0)
            introBg.alpha -= 0.02;
        else if (introFadeOutBg_)
        {
            introFadeOutBg_;

            gameState = GameState.RUNNING;
        }
    }

At a glance, it's just a bunch of if-else-if statements that check whether or not a given object has faded out and what the current state of the animation is. I feel that this if-else-if chain is alright for a rather short function like this, but I do have more complicated states that I wish to show (for example a ready-set-go splash before the fight begins) that would just balloon this kind of structure into an unholy mess.

What is the best way to deal with this code?

\$\endgroup\$
8
\$\begingroup\$

If ActionScript supports enums use an enum instead of the many booleans. If not, use a string with one constant for every state. It's also better than the booleans. For example:

...
else if (state == FADE_INT_ASDF) {
   state = CODE_OF_NEXT_STATE;
   gameState = GameState.READY;
}
...

If the logic was more complicated it would be worth implementing the State pattern.


If the booleans were replaced to an enum/string constants you can create a new setTimer function which could remove some code duplication:

private function setTimer(int timeout, IntroState/String newState) : void {
    var timer : Timer = new Timer(timeout);
    timer.addEventListener(TimerEvent.TIMER, function() {
        timer.stop();
        state = newState;
    });
    timer.start();
}
\$\endgroup\$
  • \$\begingroup\$ Wouldn't state pattern be an over kill here . \$\endgroup\$ – NRS Nov 14 '11 at 6:41
3
\$\begingroup\$

Start with simple re factoring.

1) Create a class called stateclass with variables introFadeInTitle_ , aliceIntroTxt , introFadeInBtns_ , startBtn etc. Set the variables of this class with the values

2) Instead of checking and setting properties in runIntro , hide the complexities in functions of the class and use those functions. Remember to give the functions a meaningful name so that the code flow is clear.

\$\endgroup\$
  • \$\begingroup\$ I thought of creating a class called 'Intro' or something that would display everything pertaining to my intro screen, but doing that would mean that I need one for all of my state-functions. If I decided to dump it all in my stateclass I think I would violate the single-responsibility rule, it would have states of multiple things in my game and that sounds like a mess. If you think I'm not understanding your concept correctly, would you please elaborate more on it? \$\endgroup\$ – IAE Nov 11 '11 at 17:56
  • \$\begingroup\$ While answering your question I also had a similar doubt. In this case the single responsibility could be setting the over all state but can also be setting individual state. Another design is creating different classes for different conditions and using a factory pattern. \$\endgroup\$ – NRS Nov 11 '11 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.