Michelle Field – Random Movement

In the development of my Flash knowledge I have decided to research how to do random movement using action script. I have previously made a game where the enemies move along a set path, therefore this time I wish to advance my skills and make the game more challenging to players using unpredictable movement.The following takes you through the process for random movement in Flash MX:

1) Open a new Flash document.

2) Set the stage area to 300 x 200 pixels. At this point you may also wish to draw in the stage boundaries, for example by using the rectangle tool.

3) Set the frame rate to 25 secs.

4) Draw a circle (the object in which you wish to move) using the tool box.

5) Select the circle and convert it into a movie clip by right clicking on it and selecting convert to symbol, then select movie clip.

6) Select the movie clip and add the following code into the actions box (where // is present this is additional information about each section):

onClipEvent (load) {

//data you may want to change depending on stage size
width = 300;
height = 200;
//You can increase the speed by increasing the numbers and vice versa 
speed = Math.round(Math.random()*2)+1;
//initial positions and new ending positions
x = this._x=Math.random()*width;
y = this._y=Math.random()*height;
x_new = Math.random()*width;
y_new = Math.random()*height;
}
onClipEvent (enterFrame) {

//x horizontal movement 1 being right and -1 being left
if (x_new>this._x) {

sign_x = 1;
} else {
sign_x = 1;
}
dx = Math.abs(x_newthis._x);
if ((dx>speed) || (dx<-speed)) {

this._x += sign_x*speed;
} else {
x_new = Math.random()*width;
}
//y vertical movement
if (y_new>this._y) {

sign_y = 1;
} else {
sign_y = 1;
}
//handles the speed throughout 
dy = Math.abs(y_newthis._y);
if ((dy>speed) || (dy<-speed)) {

this._y += sign_y*speed;
} else {
y_new = Math.random()*height;
}
}

7) Now you can view it by pressing Ctrl and Enter.

This information was gained from:
http://www.kirupa.com/developer/actionscript/random_motion.htm 
which will also be useful for a more detailed version of the above.

Advertisements

1 Comment »

  1. […] Original post by Flashgamer […]


{ RSS feed for comments on this post} · { TrackBack URI }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: