我不會依賴jQuery的animate
這個,因爲你的情況比較特殊......相反,使用「遊戲循環模式」:有一個遊戲對象,它保持一組粒子,這些粒子被移動(碰撞......)和然後在常規的interv阿爾斯。
這裏的一個基本結構:
function Particle(x, y) {
this.x = x;
this.y = y;
this.speed = 0; // in pixels per second
this.direction = 0; // in radians per second
}
Particle.prototype.move = function(d_time) {
this.x += Math.cos(this.direction) * this.speed;
this.y += Math.sin(this.direction) * this.speed;
}
Particle.prototype.draw = function() {
// either set the position of a DOM object belonging to this particle
// or draw to a canvas
}
function Game() {
this.particles = Array();
this.MS_PER_FRAME = 20; // in milliseconds
this.D_TIME = 1000.0/this.MS_PER_FRAME;
}
Game.prototype.tick = function() {
$.each(this.particles, function(_, particle) {
particle.move(this.D_TIME);
particle.draw();
})
}
Game.prototype.go = function() {
setInterval(this.tick, this.MS_PER_FRAME)
})
然後就可以操縱速度和粒子的方向,只要你喜歡,也許通過引入額外的部件d_speed
(加速度)和d_direction
左右。
你應該像隨機變化的加速度那樣獲得路徑。 –