arbitter Posted May 26, 2011 Share Posted May 26, 2011 Hya fellas! I've got quite a few problems with a script I have. I have 10 rows and ten columns in a table. Each table has a different id. I want to make a sort of game, that sets all the cells red except for one. you have to click it in a certain amount of time, or you fail and the 'game' gets reset. I have a function allColor(id) that changes all the cellcolors to the same color, this works. Here is the function that 'runs' the game: <script type='text/javascript'> function startGame(id){ if (id == 'start'){ document.getElementById('button').style.visibility='hidden'; allColor('red'); var rand1 = Math.floor(Math.random( )*11); var rand2 = Math.floor(Math.random( )*11); document.getElementById('r' + rand1 + 'c' + rand2).bgColor='green'; var timerDrie = setTimeout("alert('Te laat! Begin opnieuw.');restartGame();",3000); document.getElementById('greenId').value = 'r' + rand1 + 'c' + rand2; }else if(document.getElementById(id).bgColor=='yellow'){ }else{ var greenId = document.getElementById('greenId').value; if(id != greenId && greenId != '0'){ alert('Fout! Begin opnieuw.'); restartGame(); clearTimeout(timerDrie); } clearTimeout(timerDrie); allColor('red'); var rand1 = Math.floor(Math.random( )*11); var rand2 = Math.floor(Math.random( )*11); document.getElementById('r' + rand1 + 'c' + rand2).bgColor='green'; var timerDrie = setTimeout("alert('Te laat! Begin opnieuw.');restartGame();",3000); document.getElementById('greenId').value = 'r' + rand1 + 'c' + rand2; } } function restartGame(){ document.getElementById('button').style.visibility='visible'; allColor('yellow'); } </script> A short explanation: a function makes a random cell number (r$c$ with $ number between 1 and 10). This cell gets green, and a hidden field (greenId) gets the cell number for reference when the function is called again. Each cell has an onclick with it's own id in it. When it is clicked and the id doesn't match with the id from the hidden field, you get a popup and the game should restart. Now I've got tons of problems. For one: the timers do not get stopped. Never. Even if the game ends, timers keep popping up for a while. And so you can't play the game. Second, if a cell was green and the next cell randomly selected is the same one, the cell stays red and doesn't turn green. I get an error 'Cannot set property bgColor of null' Since this is only the base of my program I am sure there will be dozens of problems coming up after these are fixed, but you have to start somewhere! Thanks in advance! arbitter Quote Link to comment https://forums.phpfreaks.com/topic/237575-timer-problems-other/ Share on other sites More sharing options...
arbitter Posted May 28, 2011 Author Share Posted May 28, 2011 After dozens of problems gotten further and the whole timer problem has been resolved too. Also the problem where the same element would b e chosen twice after each other is resolved. I rewrote it all after finding a major bug in my randomnumber getter - sometimes it would get a value of 0 and the script would keep the element before as the active element, causing loads of same elements after each other. Heres the code: function count(){ var max=9; var min=1; var rand1 = Math.floor(Math.random() * (max-min) + min); var rand2 = Math.floor(Math.random() * (max-min) + min); cellid = 'r' + rand1 + 'c' + rand2; } function startGame(id){ if(id == 'start'){ document.getElementById('button').style.visibility='hidden'; allColor('red'); count(); document.getElementById('greenId').value=cellid; document.getElementById(cellid).bgColor='green'; var seconden = 3; startTimer(seconden); document.getElementById('level').innerHTML='1'; teller = '0'; }else{ var huidige = document.getElementById('greenId').value; if(document.getElementById(id).bgColor == 'yellow'){ }else if(id != huidige){ stopTimer(); alert('Fout!'); restartGame(); }else{ stopTimer(); do{count();}while(id == cellid); document.getElementById(id).bgColor='red'; document.getElementById(cellid).bgColor='green'; document.getElementById('greenId').value=cellid; var seconden = 3; if(teller >= 20){ seconden = 2; }else if(teller >= 40){ seconden=1; } startTimer(seconden); teller++; } } } Quote Link to comment https://forums.phpfreaks.com/topic/237575-timer-problems-other/#findComment-1221522 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.