javascript - setInterval() speeds up when using multiple from object method -


i have been having problems setintervals(). know these issues appear lot can't seem work out exact problem implementation is. every time instantiate new obstacle() clears set interval used rotate instance of obstacle, , next instantiation of obstacle seem rotate twice fast! i'm sure it's scope i'm relative beginner i'm not quite sure what's going on here. more info can provided.

var obstaclecount = 1; function obstacle(){     this.angle = 0;     this.id = obstaclecount;     this.elprefix = "cookie-";     this.el = '.' + this.elprefix + this.id;     $('#game-wrapper').append('<div class="' + this.elprefix + this.id + '"></div>');     obstaclecount += 1; }  var intervals = new array();  obstacle.prototype.roll = function() {     self = this;     intervals[self.id] = setinterval(function(){         self.angle -= 3;         $(self.el).rotate(self.angle);     }, 5);     $(self.el).animate({         right: 1000     }, 4000, 'linear', function(){         $(self.el).remove();         clearinterval(intervals[self.id]);     }); };  var obstacles = new array();  setinterval(function(){     obstacleid = obstaclecount;     obstacles[obstacleid] = new obstacle();     obstacles[obstacleid].roll(); }, 1000); 

in games there single update loop handles of update logic game. recommend using single interval in objects updated instead of giving each object it's own scheduled update via setinterval. gain few advantages update loop method:

  1. don't have keep track of intervals.

  2. since since setinterval not consistant timing (it fires when main script finished executing , has time before next go around, if it's interval time up. means cannot rely on it's timing being asked be.) better off having objects updated @ time consistant can be.

general pseudo code started:

    initialize objects     add objects array       setinterval(updateobjects, 30);      updateobjects(){       each object in array          object.roll();     } 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -