Text-Based Game

dwarfdude77

In Runtime
Messages
270
Location
USA
Hello everyone! I am making a text based game as a starter game for me using JavaScript. I had another post here ->http://www.techist.com/forums/f11/how-center-javascript-created-object-273413/ that has all of my previous questions. I have decided to just make a new post, as the other one was getting long and off-topic.

So, here is my first problem!


My blue button chain stops at the first line and won't go on to the next?

HTML:
Code:
<html>
<head>
	<title> Lines of Story </title>
	<link rel="stylesheet" type="text/css" href="style.css">
	<script src="script.js"></script>
</head>

<body onload="buttonReady()">
	<p id="main">Lines of Story <br> Presented by Cameron Palmer</p>

	<div id="buttonDiv"class="buttonDiv"> </div>

	<p id="button">
	<button id="submit" align="middle" onclick="change()"> Press </button>
	</p>





</body>
</html>

CSS:
Code:
#main{text-align: center;}
#button{text-align: center;}
div.buttonDiv{text-align: center;}

All of the JavaScript:
Code:
function buttonReady(){
	var b= document.getElementById("submit");
	var p= document.getElementById("main");
	var d= document.getElementById("buttonDiv");
	b.style.height="30px";
	b.style.width="60px";
}


function change() {
	var b= document.getElementById("submit");
	var p= document.getElementById("main");
	var d= document.getElementById("buttonDiv");

	switch(p.innerHTML)	{

		default:
		p.innerHTML="The end!";
		break;

		case "Lines of Story <br> Presented by Cameron Palmer":
		p.innerHTML="This is a text-based game.";
		break;

		case "This is a text-based game.":
		p.innerHTML="It will be very boring.";
		break;

		case "It will be very boring.":
		p.innerHTML="It's made for old people and nerds.";
		break;

		case "It's made for old people and nerds.":
		p.innerHTML="Ahh... text based games.";
		break;

		case "Ahh... text based games.":
		p.innerHTML="Chapter 1<br>Which color do you like more?";
		var redbtn = document.createElement("BUTTON");
		var redtext = document.createTextNode("Red");
		redbtn.appendChild(redtext);
		d.appendChild(redbtn);
		redbtn.style.color="red";
		redbtn.className="redbtn";

		var bluebtn = document.createElement("BUTTON");
		var bluetext = document.createTextNode("Blue");
		bluebtn.appendChild(bluetext);
		d.appendChild(bluebtn);
		bluebtn.style.color="blue";
		bluebtn.className="bluebtn";

		b.style.display="none";

		redbtn.style.width="60px";
		redbtn.style.height="30px";

		bluebtn.style.width="60px";
		bluebtn.style.height="30px";

		bluebtn.onclick= blueFunction;
		redbtn.onclick= redFunction;

		
		break;

		function blueFunction() {
			
			bluebtn.style.display="none";
			redbtn.style.display="none";
			var bluesubmit = document.createElement("BUTTON");
			var bluesubmitext = document.createTextNode("Press");
			bluesubmit.appendChild(bluesubmitext);
			d.appendChild(bluesubmit);
			bluesubmit.style.color="blue";
			bluesubmit.style.width="60px";
			bluesubmit.style.height="30px";

			switch(p.innerHTML){
				case "Chapter 1<br>Which color do you like more?":
				p.innerHTML="So... blue?";
				break;

				case "So... blue?":
				p.innerHTML="I always hated blue.";
				break;

				case "I always hated blue.":
				p.innerHTML="(Sniffle) <br> It... it makes me sad.";
				break;

			}

		}

		function redFunction() {
			bluebtn.style.display="none";
			redbtn.style.display="none";
			var redsubmit = document.createElement("BUTTON");
			var redsubmitext = document.createTextNode("Press");
			redsubmit.appendChild(redsubmitext);
			d.appendChild(redsubmit);
			redsubmit.style.color="red";
			redsubmit.style.width="60px";
			redsubmit.style.height="30px";

			switch(p.innerHTML) {
				case "Chapter 1<br>Which color do you like more?":
				p.innerHTML="Red...ah...red";
				break;


			}

		}





	}
}

The specific Javascript:
Code:
function blueFunction() {
			
			bluebtn.style.display="none";
			redbtn.style.display="none";
			var bluesubmit = document.createElement("BUTTON");
			var bluesubmitext = document.createTextNode("Press");
			bluesubmit.appendChild(bluesubmitext);
			d.appendChild(bluesubmit);
			bluesubmit.style.color="blue";
			bluesubmit.style.width="60px";
			bluesubmit.style.height="30px";

			switch(p.innerHTML){
				case "Chapter 1<br>Which color do you like more?":
				p.innerHTML="So... blue?";
				break;

				case "So... blue?":
				p.innerHTML="I always hated blue.";
				break;

				case "I always hated blue.":
				p.innerHTML="(Sniffle) <br> It... it makes me sad.";
				break;

			}

		}

Thanks always!
 
Hello everyone! I am making a text based game as a starter game for me using JavaScript. I had another post here ->http://www.techist.com/forums/f11/how-center-javascript-created-object-273413/ that has all of my previous questions. I have decided to just make a new post, as the other one was getting long and off-topic.

So, here is my first problem!


My blue button chain stops at the first line and won't go on to the next?

Not sure what you mean by that...mind giving more details?
 
My chain, or string, line, whatever you want to call it, on the blue choice stops at the first statement and does not go on. Does that clear it up? Sorry I was unclear that first time :)
 
I still don't know what you mean by "chain". I'm assuming you're talking about the first line in the "blueFunction" JS function? Or are you talking about your switch statement in your "blueFunction" function?

If so..open up your web dev console in your browser and see what error it's throwing. Should give some indication as to what's not working.
 
I still don't know what you mean by "chain". I'm assuming you're talking about the first line in the "blueFunction" JS function? Or are you talking about your switch statement in your "blueFunction" function?

If so..open up your web dev console in your browser and see what error it's throwing. Should give some indication as to what's not working.

Yes, I am talking about the switch statement on the blue function.

The browser console sees no errors?
 
You have no definition for the object you're switching on, your variable "p".

You're trying to access an object that doesn't exist in the blueFunction's scope (I'm assuming this based on my knowledge of other programming languages - I don't know how scoping works in JS).

You'll need to define your var p in blueFunction as well (or pass it in as a parameter).

BTW...I'd suggest staying away from single character variable names... you're not limited to the # of characters you can use, so you might as well use variable names that make sense and are readable.
 
You have no definition for the object you're switching on, your variable "p".

You're trying to access an object that doesn't exist in the blueFunction's scope (I'm assuming this based on my knowledge of other programming languages - I don't know how scoping works in JS).

You'll need to define your var p in blueFunction as well (or pass it in as a parameter).

BTW...I'd suggest staying away from single character variable names... you're not limited to the # of characters you can use, so you might as well use variable names that make sense and are readable.

Okay, figured out my problem. I did not tell the button that it was to be clicked more than once. Now I am working on not creating the button more than once :)
 
Okay, I think I have a solution to the multiple-buttons problem. Quick question, after button.style.display="none", how do I show it again?
 
Back
Top Bottom