I'm pretty new to JavaScript, and 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.
$(document).ready(function(){
$(".sit_1, .sit_2, .sit_3a, .sit_3b, .sit_4a").hide();
$(".choice_1, .choice_2a, .choice_2b, .choice_3a, .choice_3b, .choice_4a, .choice_4b").hide();
$(".sit_1").fadeIn(2000);
$(".choice_1").fadeIn(4000);
$(".choice_1").click(function(){
$(".sit_1").hide();
$(".choice_1").hide();
$(".sit_2").fadeIn(4000);
$(".choice_2a").fadeIn(8000);
$(".choice_2b").fadeIn(8000);
});
$(".choice_2a").click(function(){
$(".sit_2").hide();
$(".choice_2a").hide();
$(".choice_2b").hide();
$(".sit_3a").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);
});
$(".choice_3a").click(function(){
$(".sit_3b").hide();
$(".choice_3a").hide();
$(".choice_3b").hide();
$(".sit_4a").fadeIn(3000);
$(".choice_4a").fadeIn(4000);
$(".choice_4b").fadeIn(4000);
});
});
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="S1">
<div class="sit_1">
You are lost in the woods.
</div>
<div class="sit_2">
You find a cave. Do you go inside or go look for food? <!-- choices are choice_2a and choice_2b -->
</div>
<div class="sit_3a">
You walk into the cave.<!--no more choices for this one -->
</div>
<div class="sit_3b">
You walk away from the cave, to search for food. You find berries. Do you eat them or not?
</div> <!-- choice_3a and choice_3b -->
<div class="sit_4a">
You eat the berries.
</div>
</div>
<div id="a1">
<input type="button" class="choice_1" value="Continue on...">
<input type="button" class="choice_2a"value="Go inside">
<input type="button" class="choice_2b" value="Look for food">
<input type="button" class="choice_3a" value="Eat the berries">
<input type="button" class="choice_3b" value="Don't eat the berries">
</div>
</html>
Not sure how clear this is but I basically want all my situations (sit_) to be hidden along with the player choices (choice_) until the player presses a button or makes a choice and then the old situations along with the old choices will disappear to make way for the new situations and choices.
As I said this code does work 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?