Text-Based Game

Are you using it anywhere or trying to run a function on it before it's declared? Is it actually creating the object? It's possible the rendering/JS is out of sync as well.

That's why I said to set breakpoints and step through the code. Debugging is an important part of programming ;).
 
Are you using it anywhere or trying to run a function on it before it's declared? Is it actually creating the object? It's possible the rendering/JS is out of sync as well.

That's why I said to set breakpoints and step through the code. Debugging is an important part of programming ;).

Haha! Found it! I needed to make bluebtn a variable in that function, so I gave it an id in the function that the button was originally created in, then declared it in the function that it needed declaring in with the id. :D

I <3 dev tools!
 
Okay, just added a new choice with 3 options on the red function... only one button appears...?

Red Function:
Code:
function redFunction() {
			var button= document.getElementById("submit");
			var para= document.getElementById("main");
			var div= document.getElementById("buttonDiv");
			

			bluebtn.style.display="none";
			redbtn.style.display="none";

			rsub.style.display="block";
			rsub.onclick=redFunction;
			
			

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

				case "Red...ah...red":
				para.innerHTML="Christmas";
				break;

				case "Christmas":
				para.innerHTML="Red convertables...";
				break;

				case "Red convertables...":
				para.innerHTML="Red objects...";
				break;

				case "Red objects...":
				para.innerHTML="I HATE RED";
				break;

				case "I HATE RED":
				para.innerHTML="Who am I, you ask?";
				break;

				case "Who am I, you ask?":
				para.innerHTML="Why...I am...";
				break;

				case "Why...I am...":
				para.innerHTML="YOUR MOTHER!";
				break;

				case "YOUR MOTHER!":
				para.innerHTML="I'm just pulling your leg";
				break;

				case "I'm just pulling your leg":
				para.innerHTML="But seriously though, I am...";
				break;

				case "But seriously though, I am...":
				para.innerHTML="AWKDOKPORKGUY, the god of pork, guys, docks, and the noise awk!";
				break;

				case "AWKDOKPORKGUY, the god of pork, guys, docks, and the noise awk!":
				para.innerHTML="Chapter 2 <br> Tutorial";
				break;

				case "Chapter 2 <br> Tutorial":
				para.innerHTML="This part is basically a tutorial, so do what I tell you or I will slap you with slabs of pork!";
				break;

				case "This part is basically a tutorial, so do what I tell you or I will slap you with slabs of pork!":
				para.innerHTML="What do you mean, there are no tutorials in a text based game?";
				break;

				case "What do you mean, there are no tutorials in a text based game?":
				para.innerHTML="What do you mean, you just click on stuff? Text based games are much more than that. MUCH more than that";
				break;

				case "What do you mean, you just click on stuff? Text based games are much more than that. MUCH more than that":
				para.innerHTML="So let's do this tutorial! Tutorial prepared to get owned! And then slapped repeatedly in the face with slabs of pork!";
				break;

				case "So let's do this tutorial! Tutorial prepared to get owned! And then slapped repeatedly in the face with slabs of pork!":
				para.innerHTML="Alright here are the basic controls:";
				break;

				case "Alright here are the basic controls:":
				para.innerHTML="Left click: <br> Selects an option or skips to the next section of text";
				break;

				case "Left click: <br> Selects an option or skips to the next section of text":
				para.innerHTML="Right click: <br> Shoots Awkdokporkguy in the face and gets you bonus points";
				break;

				case "Right click: <br> Shoots Awkdokporkguy in the face and gets you bonus points":
				para.innerHTML="I'm just pulling your leg, there's no right click";
				break;

				case "I'm just pulling your leg, there's no right click":
				para.innerHTML="Wait, did you-";
				break;

				case "Wait, did you-":
				para.innerHTML="Chapter 3 <br> I can't believe you actually tried to shoot me in the face!";
				break;


				case "Chapter 3 <br> I can't believe you actually tried to shoot me in the face!":


				para.style.display="none";
				rsub.style.display="none";

				var shootagainbtn = document.createElement("BUTTON");
				var shootagaintext = document.createTextNode("Shoot Him Again");
				shootagainbtn.appendChild(shootagaintext);
				div.appendChild(shootagainbtn);
				shootagainbtn.style.color="Gray";
				shootagainbtn.className="shootagainbtn";
				shootagainbtn.id="shootagainbtn";
				shootagainbtn.style.width="100px";
				shootagainbtn.style.height="30px";
				shootagainbtn.onclick= shootagainFunction;



				var dontknowbtn = document.createElement("BUTTON");
				var dontknowtext = document.createTextNode("I don't know what you're talking about.");
				dontknowbtn.appendChild(dontknowtext);
				div.appendChild(dontknowbtn);
				dontknowbtn.style.color="Gray";
				dontknowbtn.className="dontknowbtn";
				dontknowbtn.id="dontknowbtn";
				dontknowbtn.style.width="100px";
				dontknowbtn.style.height="30px";
				dontknowbtn.onclick= dontknowFunction;


				var didntshootbtn = document.createElement("BUTTON");
				var didntshoottext = document.createTextNode("Try to convince him that you didn't shoot him");
				didntshootbtn.appendChild(didntshoottext);
				div.appendChild(didntshootbtn);
				didntshootbtn.style.color="Gray";
				didntshootbtn.className="didntshootbtn";
				didntshootbtn.id="didntshootbtn";
				didntshootbtn.style.width="100px";
				didntshootbtn.style.height="30px";
				didntshootbtn.onclick= didntshootFunction;
				

				break;


				








			}


File:
View attachment Lines of Story 2.zip
 
Look in the console log.

" Uncaught ReferenceError: shootagainFunction is not defined "

In your JS file, that's at the end of your definition for the first button. It's failing to create the rest of the buttons because your JS is failing when it hits that line because, well, that function is undefined so it's throwing an error and stops executing the rest of the JS file.

So either comment out all the button .onclick= lines, or create empty skeleton functions so that it doesn't fail when it hits those lines.
 
Look in the console log.

" Uncaught ReferenceError: shootagainFunction is not defined "

In your JS file, that's at the end of your definition for the first button. It's failing to create the rest of the buttons because your JS is failing when it hits that line because, well, that function is undefined so it's throwing an error and stops executing the rest of the JS file.

So either comment out all the button .onclick= lines, or create empty skeleton functions so that it doesn't fail when it hits those lines.

Yep. Perfect. I love this forum.
 
Back
Top Bottom