This is an archived tutorial from the kirupa.com legacy collection. It covers software that may no longer be available, but it is kept online because the ideas still hold up.
Before you begin, please not that for Flash MX, a different version of this tutorial can be found by clicking here.
One of the more difficult areas in Flash programming is programmatic movement! While it is easy to use a timeline and tween (animate) movies and objects around a screen, it takes an infinite amount of more skill to program motion. For those who think nothing cool can come out of programming and motion, I say, "Think again!"
The following animation speaks volumes as to the power of random programmatic motion:
The source code for the above animation and the animation you will have created at the end of this tutorial can be found near the bottom of the page.
Here is how to program Random Motion:

[ your timeline should look like this image ]

[ it is important that you set the code to Expert Mode ]
[ copy and paste the above code in the Frame Actions window ]

[ the circle after it was converted to a movie clip ]

[ my movie after I copied, pasted, and modified the circles ]
Customizing the CodeWhile the movement is random, just like life, there are certain boundaries. If the movement was truly random, the movie clips would move beyond your movie's width and height. The movie could even move to a 15000 pixel distance while your movie is only about 550 pixels in width. The boundaries in the code keep the movie contained within your movie. Because no two movies are of the same width and height, the following section will help you to modify the dimensions and speed of the random movement.
// movie_height: refers to the height of your movie // movie_width: refers to the width of your movie //-------------------------------------------- movie_height = 150; movie_width = 300; //--------------------------------------------
The above code can be found deeply embedded within the source code you pasted in Frame 1 of the 'actions' layer. The height and width are in pixels in code, so you should modify the values to equal the width and height of your movie. Because you created a new movie, I am assuming your movie width and height equal 550 and 400 respectively. Therefore, your code mentioned above might look like the following:
//-------------------------------------------- movie_height = 550; movie_width = 400; //--------------------------------------------
// cycle: specifies the number of milliseconds waited // between movements //-------------------------------------------- cycle = 200; //--------------------------------------------
This section of code controls the time at which the objects wait and switch paths. The smaller the number, the lesser the pause between movements.
These two sentences conclude the section on creating programmatic random movement. You can replace the circles with images as I did in my introduction example. Right click on File and select Save Target As and save to your hard drive before opening. The Actions window does not work properly when viewed without downloading the file to the hard drive first.
ConclusionThese two sentences conclude the section on creating programmatic random movement. You can replace the circles with images as I did in my introduction example.
I'd like everyone to thank Suprabeener, who helps a lot of people with programming questions in the ActionScript forum. I posted a question in my Flash ActionScript forum asking if anyone knew how to create random movement. I had been gnashing my teeth after being unable to create random movement that worked. He was kind enough to reply back with a more complex version of the code you used on this tutorial. Read his post and you can also learn how to make an object randomly look like it is shaking: Supra's Post Thanks again supra!
Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence, ads, and algorithm-driven doodads. A huge thank you to all of you who buy kirupa's books, became a paid subscriber, watch the videos, and/or interact on the forums.
Your support keeps this site going! 😇
:: Copyright KIRUPA 2026 //--