Z
zmcelrath87
I am having a problem involving the scope of timeouts and intervals.
Since timeouts and intervals execute in the global scope, dynamically
generated local interval/timeout declarations do not work, because the
dynamically generated code they have to execute involves variables
that have been thrown out by the time of execution. In the first of
the two following code examples, my interval declaration includes code
that relies on variables defined within a local function, and they
have to be defined locallly for the script to work. Basically, as the
second of the two following code examples shows, I am trying to define
20 "bullet" objects and their movement functions dynamically and with
prototypes so that a lot of memory and script file size is not wasted.
So far though, the only way that I can get this to work is statically
defining a separate movement function for each "bullet" object. Each
function is the same except for 3 or 4 places where it refers to
specific bullets—but when I do it dynamically, it cannot work because
of scope restrictions. If someone has an idea of how to work around
this, that'd be awesome.
******************
// when a certain key is pressed, this is executed
var the_bullet ;
var found_one = false ;
for (var i = 1 ; i < 21 ; i++) {
the_bullet = "bullet" + i ;
if (all_bullets[the_bullet]["inuse"] == false) {
all_bullets[the_bullet]["inuse"] = true ;
found_one = true ;
break ;
}
}
if (found_one != true) return false ;
// Here is the important interval declaration
all_bullets[the_bullet].intervalID =
setInterval('all_bullets[the_bullet]["moveBullet"]();', 180) ;
******************
Previously defined in the script file is the following, which creates
20 different bullet objects that are part of the all_bullets object,
and defines a prototype moveBullet function.
*****************
var all_bullets = new Object() ;
function primaryBullet(bullet_name) {
this.name = bullet_name ;
this.intervalID = null ;
}
all_bullets.prototype.moveBullet = function() {
var div_ref = window.document.getElementById(this.name) ;
div_ref.style.left = parseInt(div_ref.style.left) - 0 + 20 + "px" ;
// does other stuff like this
}
for (var i = 1 ; i < 21 ; i++) {
var new_bullet_name = "bullet" + i ;
all_bullets[new_bullet_name] = new primaryBullet(new_bullet_name) ;
}
*****************
Since timeouts and intervals execute in the global scope, dynamically
generated local interval/timeout declarations do not work, because the
dynamically generated code they have to execute involves variables
that have been thrown out by the time of execution. In the first of
the two following code examples, my interval declaration includes code
that relies on variables defined within a local function, and they
have to be defined locallly for the script to work. Basically, as the
second of the two following code examples shows, I am trying to define
20 "bullet" objects and their movement functions dynamically and with
prototypes so that a lot of memory and script file size is not wasted.
So far though, the only way that I can get this to work is statically
defining a separate movement function for each "bullet" object. Each
function is the same except for 3 or 4 places where it refers to
specific bullets—but when I do it dynamically, it cannot work because
of scope restrictions. If someone has an idea of how to work around
this, that'd be awesome.
******************
// when a certain key is pressed, this is executed
var the_bullet ;
var found_one = false ;
for (var i = 1 ; i < 21 ; i++) {
the_bullet = "bullet" + i ;
if (all_bullets[the_bullet]["inuse"] == false) {
all_bullets[the_bullet]["inuse"] = true ;
found_one = true ;
break ;
}
}
if (found_one != true) return false ;
// Here is the important interval declaration
all_bullets[the_bullet].intervalID =
setInterval('all_bullets[the_bullet]["moveBullet"]();', 180) ;
******************
Previously defined in the script file is the following, which creates
20 different bullet objects that are part of the all_bullets object,
and defines a prototype moveBullet function.
*****************
var all_bullets = new Object() ;
function primaryBullet(bullet_name) {
this.name = bullet_name ;
this.intervalID = null ;
}
all_bullets.prototype.moveBullet = function() {
var div_ref = window.document.getElementById(this.name) ;
div_ref.style.left = parseInt(div_ref.style.left) - 0 + 20 + "px" ;
// does other stuff like this
}
for (var i = 1 ; i < 21 ; i++) {
var new_bullet_name = "bullet" + i ;
all_bullets[new_bullet_name] = new primaryBullet(new_bullet_name) ;
}
*****************