# Random Motion

**URL:** <https://forum.kirupa.com/t/random-motion/2583>\
**Category:** flash\
**Created:** [November 11, 2001, 7:03pm UTC](https://forum.kirupa.com/t/random-motion/2583 "2001-11-11T19:03:19Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 11, 2001, 7:03pm UTC](https://forum.kirupa.com/t/random-motion/2583/1 "2001-11-11T19:03:19Z")

</div>

Hi everyone,\rI’ve been working endlessly to figure out if there is a method to make an object move randomly. I know there is a way, because I have seen many sites using it. Yet, I am unable to create anything besides specified movement in the x and y values. I can’t make anything move randomly. \r\rAny help will be highly appreciated!\r\rThanks,\rKirupa

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 11, 2001, 8:32pm UTC](https://forum.kirupa.com/t/random-motion/2583/2 "2001-11-11T20:32:34Z")

</div>

what kind of random motion do you want? vibration? choosing a random destination and moving there?\r\ryou could make a movie that will gravitate towards a given coordinate, and then set new coordinates with Math.random(); whenever it gets there.\r\rputting this in a movie will create random vibration:

```auto
 \r\ronClipEvent(load){\r\r&nbsp &nbsp &nbsp &nbsp sx = guitar._x;\r\r&nbsp &nbsp &nbsp &nbsp sy = guitar._y;\r\r}\r\r\r\ronClipEvent(enterFrame){\r\r&nbsp &nbsp &nbsp &nbsp guitar._x = sx + Math.random()*6-3;\r\r&nbsp &nbsp &nbsp &nbsp guitar._y = sy + Math.random()*6-3;\r\r}

```

\r\rwhat do you need?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 11, 2001, 8:34pm UTC](https://forum.kirupa.com/t/random-motion/2583/3 "2001-11-11T20:34:13Z")

</div>

Hey supra,\rThanks for replying. I need to make a movie clip reach a random destination and continue moving to random destinations. I’m trying to make a small square randomly move around a flash movie.\r\rThanks!\rKirupa

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2001, 6:27am UTC](https://forum.kirupa.com/t/random-motion/2583/4 "2001-11-12T06:27:08Z")

</div>

_Edited to update for mx_\r\<embed src=“[http://members.shaw.ca/rlinton/eg/randomzombie.swf](http://members.shaw.ca/rlinton/eg/randomzombie.swf)” quality=high width=100 height=100\>\r\rrandomness is fun… ; )\r\rput a few functions in \_root, frame 1:

```auto
//// functionsfunction getdistance(x,y,x1,y1){	var run,rise;	run = x1 - x;	rise = y1 - y;	return(_root.hyp(run,rise));}function hyp(a,b){	return(Math.sqrt(a*a + b*b));}MovieClip.prototype.reset = function(){	var dist,norm;	this.x = this._x;	this.y = this._y;	this.speed = Math.random()*4+2;	this.targx = Math.random()*100;	this.targy = Math.random()*100;	dist = _root.getdistance(this.x,this.y,this.targx,this.targy);	norm = this.speed/dist;	this.diffx = (this.targx-this.x)*norm;	this.diffy = (this.targy-this.y)*norm;}MovieClip.prototype.move = function(){	if(_root.getdistance(this.x,this.y,this.targx,this.targy)>this.speed){ this.x += this.diffx; this.y += this.diffy;	}else{ this.x = this.targx; this.y = this.targy; if(!this.t){this.t=getTimer();} if(getTimer()-this.t>1000){this.reset();this.t=0;}	}	this._x = this.x;	this._y = this.y;}

```

\rand then in each clip you want to behave like this:

```auto
\ronClipEvent(enterFrame){	move();}

```

\rsince reset and move have been defined as methods, you can’t use them as variable names.\r\rcheers.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2001, 12:47pm UTC](https://forum.kirupa.com/t/random-motion/2583/5 "2001-11-12T12:47:33Z")

</div>

Thanks supra. This is truly amazing! I am creating a section in which Flash techniques that don’t warrant a tutorial will be posted. Would you mind if I include this in one of my upcoming tip sections? Your name will appear on the page highlighting that this is your idea!\r\rThanks,\rKirupa

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2001, 7:09pm UTC](https://forum.kirupa.com/t/random-motion/2583/6 "2001-11-12T19:09:12Z")

</div>

kirupa, you have carte blanche to use anything i post as tips / tuts.\r\rcredit is always appreciated. i should credit the internet as a whole for my flash education.\r\ra few notes i left off:\r\rmove and reset are not the only variables created in each movie by those functions, there’s diffx/y, targx/y, t, and speed.\r\rin the reset method, the 100’s define the range of possible placement. perhaps that’s obvious, but if you’ve put this in a larger movie and are wondering why everything happens in the top left corner, that’ll be why.\r\rcheers.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 22, 2001, 2:53pm UTC](https://forum.kirupa.com/t/random-motion/2583/7 "2001-11-22T14:53:27Z")

</div>

Instead of making a wimpy tips section like the other sites, I decided to make a tutorial altogether. Here’s how the tutorial looks: [www.kirupa.com/developer/…random.asp](http://www.kirupa.com/developer/actionscript/random.asp) \r\rThanks again supra. I did modify your code slightly to make it easier for users who are not proficient in actionscript to modify the width, height, and speed to cpmplete.\r\rCheers,\rKirupa

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 22, 2001, 5:24pm UTC](https://forum.kirupa.com/t/random-motion/2583/8 "2001-11-22T17:24:40Z")

</div>

hey kirupa,\r\rgreat tut. the variable “cycle” represents the number of milliseconds it will wait between movements. strictly speaking, it should probably be set in the if (!this.t) { block. right now it’s being set every frame! for the speed, adjust how “speed” is set in the reset() function.\r\rif a movie’s registration point (the crosshairs) is in the center, there’s no reason to subtract its \_width/\_height from the scope of the randomness. i have a habit of moving the registration point to the top left of all my movies. that way it works like the stage (0,0 represents the top left corner). that’s why i did it. actually i had subtracted half the \_widht/\_height, effectively making it behave as if the registration point was in the centre (so why did i move it in the first place? oh well…).\r\ri don’t mean to nitpick, i’m just thrilled to see something i did up there.\r\rthanks!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 22, 2001, 10:06pm UTC](https://forum.kirupa.com/t/random-motion/2583/9 "2001-11-22T22:06:35Z")

</div>

Hey supra,\rI did not know that. I’ll fix the terminology for cycle in a few hours 🙂 \r\rThanks,\rKirupa

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 23, 2001, 1:06am UTC](https://forum.kirupa.com/t/random-motion/2583/10 "2001-11-23T01:06:57Z")

</div>

I want to thank the both of you, kirupa, and supra both. I got into flash back when only version 3 was out. Then i quit using it, and then I hopped back on when version 5 was out, and so much had changed. I came here “[www.kirupa.com](http://www.kirupa.com)” , and was totally impressed with the detail in the tutorials. It really got be back on track. Then I met supra, and he has helped me a lot with actionscript. So thanks to the both of you for helping me out. \r\rCheers, Brian

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 23, 2001, 8:51am UTC](https://forum.kirupa.com/t/random-motion/2583/11 "2001-11-23T08:51:54Z")

</div>

Glad to see you back into it. Flash 5.0 is an amazing step forward for sw objects.\r\rSupra is in fact, the best a/s’er I’ve had the pleasure to bulletin board with. Thank you Supra… you rule!!!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 23, 2001, 12:17pm UTC](https://forum.kirupa.com/t/random-motion/2583/12 "2001-11-23T12:17:40Z")

</div>

Supra does rock! Supra, are you Colin Moock in disguise lol j/k? Thanks for the nice comments zao, and upuaut8, nice to see you again. You took a long hiatus from the board and half the posts were empty hehe.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 7, 2002, 3:37pm UTC](https://forum.kirupa.com/t/random-motion/2583/13 "2002-01-07T15:37:29Z")

</div>

xcvxcvxcv

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 4, 2002, 7:05am UTC](https://forum.kirupa.com/t/random-motion/2583/14 "2002-03-04T07:05:46Z")

</div>

hi there\r\ri’m relatively new to flash and i need a little help! i am trying to achieve something similar to the random motion actionscript that supra has posted, and i’m not sure how to go about it. i’d like to have lots of shapes floating around randomly, such as in the examples in the tute, with the added ability to click on the shapes (with each linked to a separate page/url). can anyone let me know how i should go about this or indeed if it is possible? also on my wishlist (although probably very tricky) is for the shapes to bounce off each other when a collision is detected.\r\ryour wisdom would be very much appreciated!\r\rsoumali 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 4, 2002, 10:40am UTC](https://forum.kirupa.com/t/random-motion/2583/15 "2002-03-04T10:40:20Z")

</div>

Concerning your first question, just read Kirupa’s tutorial about randomness, and put buttons inside the movie clips that are randomly moving.\rConcerning your second question, tricky tricky… You’d better wait for Supra or Thoriphes or Dan or Sinfiniti or Upu or Phil (just kidding)…\r\rpom 0]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 4, 2002, 2:21pm UTC](https://forum.kirupa.com/t/random-motion/2583/16 "2002-03-04T14:21:44Z")

</div>

lol, wuts with all the old posts being drugged back up to the top of the list. 11/11/01!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 5, 2002, 4:32am UTC](https://forum.kirupa.com/t/random-motion/2583/17 "2002-03-05T04:32:53Z")

</div>

you can use a hitTest() with each of the movieclips, then what you can do is make them go the opposite direction. i won’t go into detail right now cause it’s late.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 11, 2002, 1:07pm UTC](https://forum.kirupa.com/t/random-motion/2583/18 "2002-03-11T13:07:29Z")

</div>

is this all the code thats needed to vibrate a clip? Is “guitar” then name of the instance?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 12, 2002, 9:04pm UTC](https://forum.kirupa.com/t/random-motion/2583/19 "2002-03-12T21:04:17Z")

</div>

yes, that’s right.\r\ri cut and paste that code form a project and neglected to anonymise it.\r

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 5, 2002, 2:43pm UTC](https://forum.kirupa.com/t/random-motion/2583/20 "2002-05-05T14:43:51Z")

</div>

Hey folks, \rgreat code and ideas… but i have been trying it in MX now and it doesnt work, whereas it works when i export i for the flash 5 player… can u tell me why?

[Next page](https://forum.kirupa.com/t/random-motion/2583.md?page=2)
