# Help with Random Movement

**URL:** https://forum.kirupa.com/t/help-with-random-movement/183739
**Category:** Uncategorized
**Created:** [March 29, 2006, 5:04pm UTC](https://forum.kirupa.com/t/help-with-random-movement/183739 "2006-03-29T17:04:24Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![leeharding](https://avatars.discourse-cdn.com/v4/letter/l/22d042/32.png) [@leeharding](https://forum.kirupa.com/u/leeharding)
#### Post date: [March 29, 2006, 5:04pm UTC](https://forum.kirupa.com/t/help-with-random-movement/183739/1 "2006-03-29T17:04:24Z")

</div>

Hello everyone.

I hope someone can help… I have a movieclip with a grid of 9 squares (3x3) what i would like to do is have each square move either on the x or y axis into a position of another square (like one of those puzzles where you move the tiles around to form a picture…) a random square in a random direction at random intervals…

I have tried editing the random movement tutorial on Kirupa, but i cannot get the effect i need…

[http://www.kirupa.com/developer/mx/random\_motionMX.htm](http://www.kirupa.com/developer/mx/random_motionMX.htm)

is this an easy task? - would someone care to point me in the right direction? - i would appreciate it if you could.

kind regards, and thanks in advance

Lee

---

<div class="post-metadata">

### Author: ![B\_L\_U\_E](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@B\_L\_U\_E](https://forum.kirupa.com/u/B_L_U_E)
#### Post date: [March 29, 2006, 5:38pm UTC](https://forum.kirupa.com/t/help-with-random-movement/183739/2 "2006-03-29T17:38:09Z")

</div>

I don’t think it should be too difficult. Randomize a variable between 0 and 3, each is a direction the box can move, and do it and random intervals. Something like [AS]  
var totalSq = 9;  
var squ = Math.round(Math.random()\*(totalSq-1))+1;  
var interval = Math.round(Math.random()\*500); //which ever time you want  
var direction = Math.round(Math.random()\*3); //up,down,left,right - respectively  
moveSq = function() {  
direction==0 ? square[squ].\_y -= square[sq].\_height : null;  
direction==1 ? square[squ].\_y += square[sq].\_height : null;  
direction==2 ? square[squ].\_x -= square[sq].\_width : null;  
direction==3 ? square[squ].\_x += square[sq].\_width : null;  
clearInterval(intSq);  
}  
intSq = setInterval(movesq,interval);[/AS] and add upon that code

---

<div class="post-metadata">

### Author: ![leeharding](https://avatars.discourse-cdn.com/v4/letter/l/22d042/32.png) [@leeharding](https://forum.kirupa.com/u/leeharding)
#### Post date: [March 29, 2006, 6:45pm UTC](https://forum.kirupa.com/t/help-with-random-movement/183739/3 "2006-03-29T18:45:58Z")

</div>

Thanks for the qiuck response, i am trying to get my head round that - sorry my AS is not that great -

so i have 9 squares on the stage named square1, square2 etc  
and i have pasted this script into the first frame… and traced the variables to try and see whats going on, but i am a little confused.

can you elaborate a little further please…

thanks for your time

Lee
