I'm not sure what's wrong, it's my first html file, but it's for a class. I had to create a Beat-The-Buzzer game with 100 buttons, rules were up to me, entirely in notepad2. I thought I'd finished the basic version, but the button to begin the game does nothing.
And I've heard I could create the table in a for loop to shorten code, but my one attempt there did nothing. So I sucked it up for 100 lines for buttons.
shouldn't be there and i wouldn't have a space between the function name and the brackets (even though it works).
These two things jumped out at me immediately so I haven't looked any further.
Ugh!! You are using bgcolor and the center tag. Both are OBSOLETE - throw away that script! and get a modern one.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
Last edited by AndrewGSW; 02-01-2013 at 12:26 AM..
You *REALLY* need to learn to use a debugger. Really. Now.
I would recommend using CHROME as your browser. It's the easiest debugger to use.
Just bring up your page in CHROME and then hit the F12 key.
Click on the CONSOLE tab and voila! There is your FIRST error:
Code:
function GameOver ();
Oops...what is that semicolon doing there! Kill it quick.
But your colorChange() *IS* working. The problem is that, since your buttons have no *VALUES*, the color has nothing to affect! Put an "X" (say) as the value of each button and the "X" will change color.
Or try changing the background color.
And why did you choose black for the default? That means that if black comes up the user gets no clue at all, right?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
ooh, I did not think of resetting the buttons. Crap. As for the old code, my professor hasn't actually taught. He rambles but doesn't show any implementation.
Also, your code can (and did!) pick the same square and same color twice in a row! So the user can't see any change.
And you have some problem with the timeout on a new game, as things seem to speed up a lot.
Oh...and the browser converts style.backgroundColor="Blue" to "blue". Chrome does, at least. I vaguely recall the FireFox converts it to RGB. So you really should *NOT* be testing the color of the cell. Instead, just remember it in a variable.
Also, IDs that start with a digit are frowned on. There are circumstances where they won't work.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
i already thought of a way to take care of resetting the colors, I'll have them start out white and reset to white after a click or miss.
transparent is even better than white.
But a better answer is as I gave you: Dump the unneeded <input>s and just add the onclick's to the <td> cells themselves.
Way back in MSIE 4 days you couldn't do that. If your book tells you that you must, that you can't add onclick to almost any element, then your book is horribly out of date.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
I wish there was a book. He has not listed a book for his course.
Well, that may not be all bad. So many of the JS books are pure crap.
There is a fairly well known author who published one of the "popular" ones (popular with ignorant instructors!) in 2011 and it is *TRASH*. Worse, it sells new for around $90! You can't help but feel that every student who got suckered into buying that nonsense should demand their money back from the school...not only for the book but for the entire course...because *CLEARLY* any instructor who chooses that book knows nothing about real world JavaScript programming.
There is a *HOPE* that your instructor may avoid some of the pitfalls.
Here are a few:
-- alert() and prompt() and document.write() are obsolete. You might use them while debugging but never in final code.
-- putting <script> in the <head> is usually not the best idea. Among other things, it means you are pretty much stuck with doing set up code via window.onload (or <body onload="...">). It's not "wrong" to do that (goodness knows I wrote enough pages that way!), but it often makes tons more sense to put the JS after the HTML is going to affect.
-- putting JavaScript event handlers inline (e.g., <input onclick="...">) is considered...well, not wrong, but stylistically bad.
There are many others. Felgall can educate you on. Read his site.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Sorry Old Pendant, but I am attempting to learn so I have not copied your code. I'm mostly looking for why something doesn't work, and not how to fix it. On the plus side, I've made progress.
Code:
<!DOCTYPE html>
<html>
<head>
<title>Beat the Buttons</title>
<link rel="StyleSheet" href="example.css" type="text/css">
<script type="text/javascript">
var points = 0;
var missCount = 0;
var choice = 0;
var timer1;
function GameOver()
{
window.alert('You scored ' + points + '. Press Start again to try and beat your score.');
points = 0;
missCount = 0;
}
function miss()
{
missCount++;
points = points - 5;
var currentButton = getElementById(choice);
currentButton.style.backgroundColor = 'white';
if (missCount >= 5)
{
GameOver();
}
else
{
colorChange();
}
}
function colorChange()
{
choice = Math.floor(100*Math.random()+1);
var colorChoice = Math.floor(4*Math.random()+1);
var currentButton = document.getElementById(choice);
switch (colorChoice)
{
case 1:
currentButton.style.backgroundColor = 'red';
timer1 = window.setTimeout("miss()", 4000);
break;
case 2:
currentButton.style.backgroundColor = 'blue';
timer1 = window.setTimeout("miss()", 3000);
break;
case 3:
currentButton.style.backgroundColor = 'green';
timer1 = window.setTimeout("miss()", 2000);
break;
case 4:
currentButton.backgroundColor = 'black';
timer1 = window.setTimeout("colorChange()", 5000);
}
}
function point()
{
window.clearTimeout (timer1);
var currentButton = document.getElementById(choice);
if (currentButton.style.backgroundColor == 'red')
{
points++;
}
else if (currentButton.style.backgroundColor == 'blue')
{
points = points + 3;
}
else if (currentButton.style.backgroundColor == 'green')
{
points = points + 5;
}
else
{
miss();
}
currentButton.style.backgroundColor = 'white';
colorChange();
}
</script>
</head>
<body bgcolor="#F8F8FF" onload="window.alert('Score as high as possible.\n1. Missing loses you 5 points\n2. Missing 5 means Game Over\n3. Hitting Black counts as a Miss\n4. Red is 1\n5. Blue is 3\n6. Green is 5')">
<center><p><br />
<h2>Beat the Buttons!</h2>
<br />
<table border="1" cellspacing="5" cellpadding="5">
<script>
for (var y=0; y<10; y++)
{
document.write('<tr bgcolor="778899">')
for (var x=1; x<=10; x++)
{
document.write('<td width=10%><form><input type="button" value="X" id=' + (y*10+x) + ' this.style.backgroundColor=\'white\' onclick="point()"></form></td>')
}
document.write('</tr>')
}
</script>
</table>
<form><input type="text" id="score" size="10"></form>
<form><input type="text" id="missed" size="10"></form>
<form><input type="button" id="Start" value="Start" onclick='colorChange()'></form>
</body>
</html>
The following was found out through inserting window.alert messages:
1. The miss() function is not running after I set the currentButton variable. Note, that when I removed that variable from the functions and made it global, no functions worked.
2. colorChange() is hitting the black case, but not changing the button. Upon failing to change the button, the whole process stops.
So these are the key issues, and I need to know why they're happening so I can fix them myself. I am aware that my attempt to set white as a default color has failed, likely due to quotation placement, and that the two textboxes are blank. They are meant to display the score and number of misses, but I'll fix them later.
You really ought to get rid of the document.write statements and move the script to the bottom of the page. The code you currently have looks more appropriate for Netscape 4 than it does for more modern browsers. That you are using such an antiquated approach makes it more difficult for others to help you as it is easy to forget all the restrictions that apply to code that became obsolete over ten years ago.