# Going only \_x in Random Movement

**URL:** <https://forum.kirupa.com/t/going-only--x-in-random-movement/13353>\
**Category:** flash\
**Created:** [February 12, 2003, 12:43am UTC](https://forum.kirupa.com/t/going-only--x-in-random-movement/13353 "2003-02-12T00:43:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![darkfx](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@darkfx](https://forum.kirupa.com/u/darkfx)\
**Post date:** [February 12, 2003, 12:43am UTC](https://forum.kirupa.com/t/going-only--x-in-random-movement/13353/1 "2003-02-12T00:43:04Z")

</div>

Hi Guys,  
Does anyone know how to change the below code to only move randomly along the \_x axis and NOT the \_y axis?

I’m not sure what to change…

Thanks so much.

//Random Movement: [kirupa.com](http://kirupa.com)  
//Thanks to Suprabeener for the original code!

function getdistance (x, y, x1, y1) {  
var run, rise;  
run = x1-x;  
rise = y1-y;  
return (hyp(run, rise));  
}  
function hyp (a, b) {  
return (Math.sqrt(a_a+b_b));  
}  
MovieClip.prototype.reset = function () {  
var dist, norm, movie\_height, movie\_width;

// movie\_height: refers to the height of your movie  
// movie\_width: refers to the width of your movie

//---------------------------------

movie\_height = 550;  
movie\_width = 400;

//---------------------------------

speed = Math.random()_4+2;  
targx = Math.random()_(movie\_width-\_width);  
targy = Math.random()\*(movie\_height-\_height);  
dist = \_root.getdistance(\_x, \_y, targx, targy);

norm = speed/dist;  
diffx = (targx-\_x)\*norm;  
diffy = (targy-\_y)\*norm;  
};

MovieClip.prototype.move = function () {  
var cycle;

// cycle: specifies speed of the movement. The smaller  
// number, the faster the objects move.

//--------------------------------------------

cycle = 100;

//--------------------------------------------

if (\_root.getdistance(\_x, \_y, targx, targy)\>speed) {x += diffx;y += diffy;  
} else {x = targx;  
y = targy;  
if (!this.t) {  
t = getTimer();  
}if (getTimer()-t\>cycle) {reset();  
t = 0;  
}  
}  
\_x = x;  
\_y = y;  
}

---

<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:** [February 12, 2003, 1:02am UTC](https://forum.kirupa.com/t/going-only--x-in-random-movement/13353/2 "2003-02-12T01:02:26Z")

</div>

Bah! I got it!

Incase anyone is interested, i set the movie\_height = 1;

It was much easier than I thought…
