00:00
00:00
Newgrounds Background Image Theme

wilwz just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS: Random movement

5,870 Views | 11 Replies
New Topic Respond to this Topic

AS: Random movement 2005-09-11 07:38:17


I'm aware of Begoners thread, but that isn't the same as this because this thread actually covers random movement.

AS: Main

What I am going to explain is how to make a movie clip move around the stage randomly, changing direction every few seconds.

So, what you are going to do is make two variables on the chosen movie clip. One of the variables will handle a _x location while the other handles a _y location. The movie clip will determine it's speed/direction (velocity) by taking the _x variable (which will be random) from it's current _x location, and the _y variable (random again) from it's current _y position.

Code
Here is my code which will cause the movie clip to move about the stage randomly.

onClipEvent (load) {
speed = 60
//You can change that to suit your flash best
//A higher speed will make the MC move slower
function getPoints() {
getX=random(550), getY=random(400);
//get 2 numbers, one for the _x axis and one for the _y
//Change 550 and 400 to the maximum _x and _y for your flash
gotoX=(getX-_x)/speed, gotoY=(getY-_y)/speed;
//declaring the speed and direction (velocity)
clearInterval(id);
id = setInterval(getPoints, random(2000)+500);
//make the amount of time in between change in direction random.
}
id = setInterval(getPoints, 1000);
getX=random(550), getY=random(400);
}
onClipEvent (enterFrame) {
_x += gotoX, _y += gotoY;
//move
}

Links:

Example
Another tutorial (much harder + does the same thing)
Google: Random Movement

Any questions?


Sup, bitches :)

BBS Signature

Response to AS: Random movement 2005-09-11 08:23:51


Here are the three random movement types I used for Herd:
First off, straight path with random rotate at random intervals

onClipEvent(load){
spd=5;
shcd=0;
omg= 1;
}
onClipEvent(enterFrame){
mY = (spd*Math.cos (Math.PI/180*_rotation));
mX = (spd*Math.sin (Math.PI/180*_rotation));
if (_rotation>180) {
_y += mY; _x -= mX;
} else {
_y -= mY; _x += mX;
}
shcd++;
if (shcd>=omg) {
_rotation = random(360);
shcd = 0;
omg = random(20)+15;
}
}

Second, a 'weaving movement' created by changing the rotation to positive or negative at random intervals.
I used this for the hanggliders

onClipEvent(load){
spd=5;
shcd=0;
omg= 1;
blim=0;
}
onClipEvent(enterFrame){
mY = (spd*Math.cos (Math.PI/180*_rotation));
mX = (spd*Math.sin (Math.PI/180*_rotation));
if (_rotation>180) {
_y += mY; _x -= mX;
} else {
_y -= mY; _x += mX;
}
shcd++;
if (shcd>=omg) {
if (blim) {
blim = 0;
shcd = 0;
omg = random(20)+15;
} else {
blim = 1;
shcd = 0;
omg = random(20)+15;
}
}
if (blim) {
_rotation += 4;
} else {
_rotation -= 4;
}
}

Finally, a random jittery movement I used for the sheep:

onClipEvent(load){
spd=5;
}
onClipEvent(enterFrame){
mY = (spd*Math.cos (Math.PI/180*_rotation));
mX = (spd*Math.sin (Math.PI/180*_rotation));
if (_rotation>180) {
_y += mY; _x -= mX;
} else {
_y -= mY; _x += mX;
}
_rotation += random(60)-30;
}

All of these will require some extra code to prevent your MC from going off-stage.


- - Flash - Music - Images - -

BBS Signature

Response to AS: Random movement 2005-09-11 09:28:13


Here's a really simple/editable one for noobs I did. Sorry, no rotation in this one.

Just replace the speed variable, and for the _x, and _y, put in where you want them to spawn at in your game.

For xx and yy, put in the X and Y's of where you want their movement bounds to be.

For timerMax, put in how long you want them to move for to their certain destination.

That should be it, again it's for noobs, and it's very simple.

onClipEvent (load) {
speed = random(6)+4;
_x = random(600);
_y = random(200);
xx = random(600);
yy = random(200);
timer = 0;
timerMax = random(30)+30;
}
onClipEvent (enterFrame) {
timer++;
if (timer>=timerMax) {
speed = random(6)+4;
xx = random(600);
yy = random(200);
timerMax = random(30)+30;
timer = 0;
}
if (_x>xx) {
_x -= speed;
}
if (_x<xx) {
_x += speed;
}
if (_y>yy) {
_y -= speed;
}
if (_y<yy) {
_y += speed;
}
}


the events are merely fictional, written, directed, and acted out by all who create them

BBS Signature

Response to AS: Random movement 2005-09-11 09:43:03


I made a random movement AS once and i think it's pretty good. Here it is:

onClipEvent (load) {
myMove = random(3);
humanspeed = 5;
}
onClipEvent (enterFrame) {
if (myMove == 0) {
this._x += humanspeed;
myMove2 = random(10);
} else if (myMove == 1) {
this._x -= humanspeed;
myMove2 = random(10);
} else if (myMove == 2) {
this._y += humanspeed;
myMove2 = random(10);
} else if (myMove == 3) {
this._y -= humanspeed;
myMove2 = random(10);
}
if (myMove2 == 1) {
myMove = random(3);
}
if (this._x>=550) {
this._x=0;
} else if (this._x<=0) {
this._x=550;
}
if (this._y>=355) {
this._y=21;
} else if (this._y<=21) {
this._y=355;
}
}

Response to AS: Random movement 2005-09-11 09:49:47


first post in the topic to not really be helpful

thanks liam, denvish and star ogre. i was thinking about something like this.


WEBSITE

BLOG ~ Dont fuck around with my dog. All that I can see I steal. ~

NG FFR ~ Automatic for the people.

BBS Signature

Response to AS: Random movement 2006-04-17 13:08:32


in the original script, how do you stop that pause at the beggining?


BBS Signature

Response to AS: Random movement 2006-04-17 13:13:57


At 4/17/06 01:08 PM, Hoeloe wrote: in the original script, how do you stop that pause at the beggining?

At this part:
id = setInterval(getPoints, 1000);

Change 1000 to 0 or a much lower number, it will automatically start it

Response to AS: Random movement 2006-04-21 05:25:35


how about if i want something to move along the _x only, wich i have done, and swap _xscale depending on which way it is going?


BBS Signature

Response to AS: Random movement 2006-04-21 05:28:58


fixed it now :D


BBS Signature

Response to AS: Random movement 2006-06-05 15:13:01


hiya,
i've tried the first code in this thread. i'm using a big stage - 1024 in width and using the code to make fireflies which buzz around randomly. thing is, they don't seem to like the right hand side of the stage. if i place them there to start with, they whizz straight across to the left and all buzz around there together :)
is this because my stage is so wide? is there something in the code i can change to encourage them over the right hand side of the stage?
thanks!
emma.

Response to AS: Random movement 2006-06-05 15:16:00


At 6/5/06 03:13 PM, zoobuffalo wrote: is this because my stage is so wide? is there something in the code i can change to encourage them over the right hand side of the stage?
thanks!
emma.

liaaaam wrote:
//Change 550 and 400 to the maximum _x and _y for your flash

ie, change both instances of 550 in the code to 1024


- - Flash - Music - Images - -

BBS Signature

Response to AS: Random movement 2007-09-02 11:52:22


How would I change liaaams so that it's in the main time line and its being duplicated.
I've got:
for (i=0; i<level; i++) {
duplicateMovieClip("enemy", "enemy_"+i, i);
}

So how would I make all that code through the duplication, for example I would add:

_root["enemy_"+i]._x = random(700);
etc.

But It doesn't seem to be working. Any help?


They used to call me souled...