# Effect in certain area?

**URL:** <https://forum.kirupa.com/t/effect-in-certain-area/119696>\
**Category:** Uncategorized\
**Created:** [August 17, 2004, 1:53pm UTC](https://forum.kirupa.com/t/effect-in-certain-area/119696 "2004-08-17T13:53:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![B3NKobe](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@B3NKobe](https://forum.kirupa.com/u/B3NKobe)\
**Post date:** [August 17, 2004, 1:53pm UTC](https://forum.kirupa.com/t/effect-in-certain-area/119696/1 "2004-08-17T13:53:59Z")

</div>

Hi - in my flash site im creating i have a certain area which i call an effect box/area and in it is just going to be funny stupid little effects.

Anyway i have script to make an object move randomly around; except i do not know how to make it so that the effect is in my effect box/area instead of all over my site?? i have played around with the width and height varibles but just cant get it to work???

Here is my script:

onClipEvent (load) {  
//data you may want to change  
width = 600;  
height = 600;  
speed = Math.round(Math.random()\*2)+1;  
//initial positions  
x = Math.random()\*width;  
y = Math.random()\*height;  
this.\_x = x;  
this.\_y = y;  
x\_new = Math.random()\*width;  
y\_new = Math.random()_height;  
}  
onClipEvent (enterFrame) {  
//x movement  
if (x\_new\>this.\_x) {  
sign\_x = 1;  
} else {  
sign\_x = -1;  
}  
dx = Math.abs(x\_new-this.\_x);  
if ((dx\>speed) || (dx\<-speed)) {  
this.\_x += sign\_x_speed;  
} else {  
x\_new = Math.random()_width;  
}  
//y movement  
if (y\_new\>this.\_y) {  
sign\_y = 1;  
} else {  
sign\_y = -1;  
}  
dy = Math.abs(y\_new-this.\_y);  
if ((dy\>speed) || (dy\<-speed)) {  
this.\_y += sign\_y_speed;  
} else {  
y\_new = Math.random()\*height;  
}  
}

---

<div class="post-metadata">

**Author:** ![RvGaTe](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/rvgate/32/126_2.png) [@RvGaTe](https://forum.kirupa.com/u/RvGaTe)\
**Post date:** [August 18, 2004, 1:55am UTC](https://forum.kirupa.com/t/effect-in-certain-area/119696/2 "2004-08-18T01:55:48Z")

</div>

this is how i fixed it:

create a box on the stage where you want your effect taken place…  
then give it the instance name “box”

then replace your code with:

```php

onClipEvent (load) {
	//data you may want to change
	box_x = _root.box._x;
	box_width = _root.box._width-_width;
	box_y = _root.box._y;
	box_height = _root.box._height-_height;
	//
	x_new = box_x+Math.random()*box_width;
	y_new = box_y+Math.random()*box_height;
	speed = Math.round(Math.random()*2)+1;
	//initial positions
	x = box_x+Math.random()*box_width;
	y = box_y+Math.random()*box_height;
	this._x = x;
	this._y = y;
}
onClipEvent (enterFrame) {
	//x movement
	if (x_new>this._x) {
		sign_x = 1;
	} else {
		sign_x = -1;
	}
	dx = Math.abs(x_new-this._x);
	if ((dx>speed) || (dx<-speed)) {
		this._x += sign_x*speed;
	} else {
		x_new = box_x+Math.random()*box_width;
	}
	//y movement
	if (y_new>this._y) {
		sign_y = 1;
	} else {
		sign_y = -1;
	}
	dy = Math.abs(y_new-this._y);
	if ((dy>speed) || (dy<-speed)) {
		this._y += sign_y*speed;
	} else {
		y_new = box_y+Math.random()*box_height;
	}
}

```

note these lines:

> 

```
box_x = _root.box._x;
box_width = _root.box._width-_width;
box_y = _root.box._y;
box_height = _root.box._height-_height;

```

should fix it 🙂 (if you dont want the box, just change the box\_ vars to co-ordinates…)
