-
supervillain
there HAS to be a quicker way to get random movement.
Code:
function getdistance (x, y, x1, y1) {
var run, rise;
run = x1-x;
rise = y1-y;
return (hyp(run, rise));
}
function hyp (a, b) {
return (Math.sqrt(a*a+b*b));
}
MovieClip.prototype.reset = function () {
var dist, norm, movie_height, movie_width;
movie_height = 12;
movie_width = 310;
speed = Math.random()*4+2;
targx = Math.random()*(movie_width-_width);
targy = Math.random()*(movie_height-_height);
dist = _root.getdistance(_x, _y, targx, targy);
norm = speed/dist;
diffx = (targx-_x)*norm;
diffy = (targy-_y)*norm;
};
MovieClip.prototype.move = function () {
var cycle;
cycle = 100;
if (_root.getdistance(_x, _y, targx, targy)>speed) {x += diffx;y += diffy;
} else {x = targx;
y = targy;
if (!this.t) {
t = getTimer();
}if (getTimer()-t>cycle) {reset();
t = 0;
}
}
_x = x;
_y = y;
}
then, for each symbol I use
Code:
onClipEvent (enterFrame) {
move();
}
there has GOTTA be a quicker method.
-
Code:
onClipEvent (load) {
minX = 0;
maxX = 550;
minY = 0;
maxY = 400;
speed = 5;
xDest = random(maxX - minX) + minX;
yDest = random(maxY - minY) + minY;
}
onClipEvent (enterFrame) {
if (Math.round(_x) != xDest) {
_x += xDif;
xDif = (xDest - _x) / speed;
} else {
xDest = random(maxX - minX) + minX;
}
if (Math.round(_y) != yDest) {
_y += yDif;
yDif = (yDest - _y) / speed;
} else {
yDest = random(maxY - minY) + minY;
}
}
This might work?
Josh Dura
-
Senior Member
gerbick, what are you trying to do?
Your code above just moves the mc left and right a bit. What is your objective, what do you want it to do?
-
supervillain
yeah razor... that code is directly from my footer. I want to take a MC, or multiple versions thereof, and basically create TRUE random motion.
jdura... thanks for the code. but I gotta ask. how do I attach that to a MC?
yes, you can even treat me like an idiot on the answer
-
Senior Member
Originally posted by gerbick
jdura... thanks for the code. but I gotta ask. how do I attach that to a MC?
create a movieclip, right click on it and choose actions. then paste the text. Don't put it on a frame or it will generate errors.
-
Senior Member
heres a simpler albeit 'jerkier' version. Speed the frame rate up to 40 and watch it go.
Code:
onClipEvent (load) {
min = "0";
max = "200";
}
onClipEvent (enterFrame) {
if (this._x <= min) {
setProperty (this, _x, "100");
}
if (this._y <= min) {
setProperty (this, _y, "100");
}
if (this._x >= max) {
setProperty (this, _x, "100");
}
if (this._y >= min) {
setProperty (this, _y, "100");
}
newx = random(20)-10;
newy = random(20)-10;
setProperty (this, _x, this._x + newx);
setProperty (this, _y, this._y + newy);
}
-
Gross Pecululatarian
Okay, heres a stripped down version of some code I used ages ago to make "intellegant" movment. The use of trig isn't neccessary.
Adjust the random to give you the amount of rotation.
Code:
onClipEvent (load) {
function Move (deg, speed) {
var rad = deg*(Math.PI/180);
var x = Math.cos(rad)*speed;
var y = -Math.sin(rad)*speed;
_y += y;
_x += x;
}
speed = 2;
angle = Math.random()*360;
xb = 400;
yb = 400;
}
onClipEvent (enterFrame) {
angle +=( Math.random()*40)-20;
Move(angle, speed);
if(_x < 0 || _x > xb || _y < 0 || _y > yb){
angle += 30;
}
}
-
Originally posted by gerbick
and basically create TRUE random motion.
Sounds like what you want is Brownian motion -- a bounded random walk over a 2D lattice. A web search should turn up dozens of pages that will help you out. I used this same technique to animate a flickering flame in MAX using script controllers not too long ago.
have fun
too bad I don't speak ActionScript
-
Gross Pecululatarian
If you want brownian motion, change the random line in my script to this:
Change
angle +=( Math.random()*40)-20;
To
angle = Math.round(Math.random()*5)==5 ? Math.random()*360 : angle;
So it makes this:
Code:
onClipEvent (load) {
function Move (deg, speed) {
var rad = deg*(Math.PI/180);
var x = Math.cos(rad)*speed;
var y = -Math.sin(rad)*speed;
_y += y;
_x += x;
}
speed = 10;
angle = Math.random()*360;
xb = 400;
yb = 400;
}
onClipEvent (enterFrame) {
angle = Math.round(Math.random()*2)==2 ? angle + Math.random()*160 + 100 : angle;
Move(angle, speed);
if(_x < 0 || _x > xb || _y < 0 || _y > yb){
angle += 30;
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|