JavaScript: function only taking constants, no variables. :/
I am making a Simon game for my web site. It utilizes javascript as it's language to run the game. Sadly, at one point in my game loop, it checks if it's the computers turn to do its' thing. If it is the computers turn, It generates a random number, and adds it to it's objects variable array.(algor[]) Than it goes through a for loop, and makes a setTimeout() function to another function(Called S_ChgImg(img_iter, img_num)). The for loop iterates every element of algor to S_ChgImg(img_iter, img_num). There is only 4 possible values (0,1,2,3) to send through S_ChgImg(). (one for every color on the simon game table)
The rest of the information on how the function works doesn't really matter (the code should be easy enough to be understandable, yet if you want more info, just ask), but here is my problem & question: I can only send constants (1,2,3,4,5.. etc) but no variables (S_GameData.algor[0], img_array, etc.) through the function. Even when I write 'document.write(img_iter)' directly in S_ChgImg(img_iter, img_num) body of code, only if I click on the image (in which no variables are passed, only constants) will that line be reached. Or image change, if the line is taken out. Any ideas? Here is the relevant functions, variables, objects: Code:
S_GameData = { Code:
/* Code:
/* |
Ok, I've seem to fix the problem with :
Code:
var timeOutOne = setTimeout("S_ChgImg(S_GameData.algor[iter],1)",1000 * iter); Code:
var timeOutOne = setTimeout(S_ChgImg(S_GameData.algor[iter],1),1000 * iter); --UPDATE-- Looks like I found where my problems lies. To begin with, here is the problem block of code: Code:
function S_GameLoop() It runs that line, than it stops running the function. :/ Any ideas? |
--UPDATE UPDATE--
I got passed that problem, I got the lines to run, but the functions that are supposed to run after a certain period of time, do NOT run. Here is the new code: Code:
var timeOutOne = setTimeout(function(){S_ChgImg(S_GameData.algor[iter],1);},1000 * iter); |
How did you determine which lines have run? From my tests, it's the loop that doesn't run.
I don't know much Javascript so I'm not sure what Code:
S_GameData = { Code:
<html> Quote:
and an error in Firefox's JavaScript Console (which you should definitely use): Quote:
|
Fixed the .length issue.
...and wow... I remember JavaScript console, but that was a while ago, and I forgot about it. Thanks for the tip. Quote:
-- 800th post. |
Um, how did you fix it? Did it help anything else?
Quote:
Code:
var timeOutOne = setTimeout("S_ChgImg(S_GameData.algor[iter],1)",1000 * iter); |
Yea, and I wrote it only seemed to work if I used constants. But the variables didn't work. :/ I did some usage of the JS console, and I noticed that I can't have a document.getElementById(id)[element]. How would I be able to access different elements, instead of making a unique id over and over again?
--EDIT-- I changed the: Code:
S_GameData.algo[S_GameData.algor.length] = 4; Code:
S_GameData.algo[S_GameData.algor.length +1] = 4; I still cannot send my variable S_GameData.algor[iter] to S_ChgImg. I added a debuging <div> tag in my code, and here is what is brought up in my S_ChgImg() function, when failing a simple case statement to see if img_iter is a value of 0 to 3: NOTE: undefined (color Blue) is img_iter, and is the parameter that S_GameData.aglor[iter] copys its' value to. Code:
-DEBUG-S_ChgImg(undefined, 1) img_iter switch found a non valid value. NOTE: The first line (BEFORE) is what the array S_GameData.algor length, & followed by what it's width is, before and after the random number was inserted into it's array. Code:
LENGTH ELEMENT 0 |
Quote:
Quote:
Quote:
|
Yes... leaving
Code:
S_GameData.algo[S_GameData.algor.length +1] = 4; Code:
S_GameData.algo[S_GameData.algor.length] = 4; Code seems to be working. I'll add more info on the fix later on today, once I get some sleep. |
All times are GMT -5. The time now is 08:58 AM. |