lundi 1 août 2016

Creating an If/Else Statement for Gamer Choices in Javascript

I'm pretty new to javascript but I'm trying to created a text based game. I've written a few scenarios and based on the player's decision I have different scenarios written out. As of now I have the code written and it works but I feel like I can be more efficient with an if else statement in my js.

Here's what I have so far in terms of player decisions.

<div class="sit_2">
                Should you go inside the cave, or do you search for                                                             food? 
</div>

                    <input type="button" class="choice_2a" value="Cave">

                    <input type="button" class="choice_2b" value="Food">
</html>
<javascript>
$(".choice_2a").click(function(){
    $(".sit_2").hide();
    $(".choice_2a").hide();
    $(".choice_2b").hide();
    $(".sit_3a").fadeIn(4000);
    $(".choice_3a").fadeIn(4000);
    $(".choice_3b").fadeIn(4000);

    });


$(".choice_2b").click(function(){
    $(".sit_2").hide();
    $(".choice_2a").hide();
    $(".choice_2b").hide();
    $(".sit_3b").fadeIn(4000);
    $(".choice_3a").fadeIn(4000);
    $(".choice_3b").fadeIn(4000);

    });

</javascript>

Not sure how clear it is, but picking choice_2a will fadeOut everything and fadeIn situation sit_3a along with choice_3a and choice_3b (which is the next situation and player choices). while choosing choice_2b will fadeOut everything and fadeIn in situation sit_3b along with choice_3a and choice_3b.

As I said this code does work and I've simplified it a bit here to avoid extraneous details but I feel like I can make it more efficient with an if/else statement but I'm not sure how to go about it. Any help would be greatly appreciated.

Also, should I make the choices into an array to make it easier?

Aucun commentaire:

Enregistrer un commentaire