PDA

View Full Version : Random mouvement



utopiko
October 10th, 2005, 11:58 PM
Hi, i found this in the tutorial, but i have a problem using it.



// max_dv -> maximum absolute velocity change
// maxV -> maximum absolute velocity
// freq -> frequency of velocity change
// behavior -> "bounce" or "mod"; action taken when instance hits boundaries; default action: bounce
// fps -> sets animation's FPS; generally set to movie's FPS, but can be overridden
MovieClip.prototype.moveRandom = function(max_dv, maxV, freq, behavior, fps) {
this.chgSpd = max_dv;
this.maxV = maxV;
this.freq = freq;
this.vx = this.chgSpd*(Math.random()*2-1);
this.vy = this.chgSpd*(Math.random()*2-1);
if (behavior == "bounce" || behavior != "mod") {
this.$mR = function() {
this._x += this.vx;
this._y += this.vy;
// the lines that give life...
this.vx = (!(random(this.freq))) ? (this.vx+this.chgSpd*(Math.random()*2-1))%this.maxV : this.vx;
this.vy = (!(random(this.freq))) ? (this.vy+this.chgSpd*(Math.random()*2-1))%this.maxV : this.vy;
if (this._x>Stage.width || this._x<0) {
this.vx = -this.vx;
}
if (this._y>Stage.height || this._y<0) {
this.vy = -this.vy;
}
updateAfterEvent();
};
} else {
this.$mR = function() {
this._x += this.vx;
this._y += this.vy;
// the lines that give life...
this.vx = (!(random(this.freq))) ? (this.vx+this.chgSpd*(Math.random()*2-1))%this.maxV : this.vx;
this.vy = (!(random(this.freq))) ? (this.vy+this.chgSpd*(Math.random()*2-1))%this.maxV : this.vy;
if (this._x>Stage.width) {
this._x %= Stage.width;
}
if (this._x<0) {
this._x = Stage.width-this._x;
}
if (this._y>Stage.height) {
this._y %= Stage.height;
}
if (this._y<0) {
this._y = Stage.height-this._y;
}
updateAfterEvent();
};
}
this.__INTERVAL = setInterval(this, "$mR", 1000/fps);

First problem is that the function is affecting every movie clip i place on the stage. Even if i do this :


my_mc = moveRandom(2,3,2,"bounce",25);

For exemple, another mc named "movieBall" will also be affected by the function "moveRandom" that i've put on "my_mc"

And the other problem is that the 'bounce' or 'mod' behavior doesn't work....

Can anyone help me.

utopiko
October 11th, 2005, 11:25 AM
no ?

Barn
October 12th, 2005, 12:12 PM
First problem is that the function is affecting every movie clip i place on the stage.
Well, yes -- that is generally the purpose of creating a movieclip prototype function. Why did you create a prototype function, rather than a normal function?