# Scripting a boundary

**URL:** <https://forum.kirupa.com/t/scripting-a-boundary/10688>\
**Category:** Uncategorized\
**Created:** [January 5, 2003, 6:45pm UTC](https://forum.kirupa.com/t/scripting-a-boundary/10688 "2003-01-05T18:45:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmb597](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@jmb597](https://forum.kirupa.com/u/jmb597)\
**Post date:** [January 5, 2003, 6:45pm UTC](https://forum.kirupa.com/t/scripting-a-boundary/10688/1 "2003-01-05T18:45:30Z")

</div>

I am working on a small flash piece for a site and I was wondering if anyone could help me out.

I am trying to have the words in the flash piece to move randomly across in a set sized box as is. I want it more smooth (not as important though) and I also want a boundary on it so the words stay inside the box that they are in right now. I have a mask over it right now so when the words go across the box, they get cut off instead of staying inside of it. If anyone could help me out on that, that would be great!!!

Please let me know if I confused you! I sooooooooooo appreciate it! Thanks!!!

I tried to attach the flash file, but it was too big (105 kb)… if anyone is interested in helping me out, I can email the flash file to you!

---

<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 6, 2003, 12:59am UTC](https://forum.kirupa.com/t/scripting-a-boundary/10688/2 "2003-01-06T00:59:34Z")

</div>

Can you post your code?

---

<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 6, 2003, 1:07am UTC](https://forum.kirupa.com/t/scripting-a-boundary/10688/3 "2003-01-06T01:07:54Z")

</div>

you can try this:

```auto

MovieClip.prototype.constrainTo = function(cb){
	if (typeof cb == "movieclip") cb = cb.getBounds(this._parent);
	var tb = this.getBounds();
	if (this._x + tb.xmin < cb.xmin) this._x = cb.xmin + tb.xmax;
	if (this._x + tb.xmax > cb.xmax) this._x = cb.xmax + tb.xmin;
	if (this._y + tb.ymin < cb.ymin) this._y = cb.ymin + tb.ymax;
	if (this._y + tb.ymax > cb.ymax) this._y = cb.ymax + tb.ymin;	
}

```

whereever you have the clip being moved, put after it:  
movingText.constrainTo(boxClip);  
where boxClip is a movieclip box you want to keep it in. You can also use a getBounds() formatted object if not a movieclip specificalyl

( [http://proto.layer51.com/d.aspx?f=660](http://proto.layer51.com/d.aspx?f=660) )

---

<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 6, 2003, 1:11am UTC](https://forum.kirupa.com/t/scripting-a-boundary/10688/4 "2003-01-06T01:11:29Z")

</div>

Senocular, you impress me 🙂

---

<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 6, 2003, 1:18am UTC](https://forum.kirupa.com/t/scripting-a-boundary/10688/5 "2003-01-06T01:18:06Z")

</div>

> \*Originally posted by ilyaslamasse \*  
> \*\*Senocular, you impress me 🙂 \*\*

:beam:

---

<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 6, 2003, 2:26am UTC](https://forum.kirupa.com/t/scripting-a-boundary/10688/6 "2003-01-06T02:26:31Z")

</div>

Hi, thanks for replying!! The code I have was used for lines to randomly move, I just replaced the lines with words hoping it would work. It did…but now I want to set the boundary…  
This code is placed in the keyframe:

width = 300;  
function getdistance(x, x1) {  
var run, rise;  
run = x-x1;  
return (\_root.hyp(run));  
}  
function hyp(a, b) {  
return (Math.sqrt(a_a+b_b));  
}  
MovieClip.prototype.reset = function() {  
// -------------------  
var dist, norm;  
this.x = this.\_x;  
this.speed = Math.random()\*5+2;  
this.targx = Math.random()\*width;  
dist = \_root.getdistance(this.x, this.targx);  
norm = this.speed/dist;  
this.diffx = (this.targx-this.x)\*norm;  
};  
MovieClip.prototype.move = function() {  
if (\_root.getdistance(this.x, this.targx)\>this.speed) {  
this.x += this.diffx;  
} else {  
this.x = this.targx;  
if (!this.t) {  
this.t = getTimer();  
}  
if (getTimer()-this.t\>1000) {  
this.reset();  
this.t = 0;  
}  
}  
this.\_x = this.x;  
};  
for (i=0; i\<5; i++) {  
\_root.line.duplicateMovieClip(“line”+i, i);  
myLine = \_root[“line”+i];  
myLine.\_x = Math.random()\*width;  
}

When you click on the actual symbol, the code is:

onClipEvent (enterFrame) {  
move();  
}

I don’t know if I did it all wrong or what, but the words do move…

Thanks so much for taking the time to look at this!

---

<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 6, 2003, 2:29am UTC](https://forum.kirupa.com/t/scripting-a-boundary/10688/7 "2003-01-06T02:29:30Z")

</div>

By the way, thanks senocular, I am going to try what you gave!!!

---

<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 6, 2003, 3:27pm UTC](https://forum.kirupa.com/t/scripting-a-boundary/10688/8 "2003-01-06T15:27:37Z")

</div>

I couldn’t get the code to work for me…I will keep trying though. If anyone can help me out with the code I posted, that would be great.

senocular, the code you posted above…where would the the constrainto code go? I tried many different places in my project, but couldn’t get it right.

I can still email a flash file to anyone…its all done but the boundary code. I really did try to do it all myself. This project is due on Wednesday, I really do appreciate your time. Thanks!

---

<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 6, 2003, 4:09pm UTC](https://forum.kirupa.com/t/scripting-a-boundary/10688/9 "2003-01-06T16:09:23Z")

</div>

with your case, you really wouldnt need my code. The code you have creates a random span of movement between 0 and the width variable you have at the beginning of your code. Depending on what bounds you want to constrain your movement to depends on this and the setting of  
this.targx = Math.random()\*width;  
to determine that. What you can do is use

```auto

Math.randomRange = function(min, max){
	return Math.floor(Math.random()*(max-min+1))+min;
}

```

(to replace width = 300)  
and change  
this.targx = Math.random()\*width;  
to  
this.targx = Math.randomRange(100,400)  
or where ever it is you want to keep your text within. ^ that sets it to an \_x between 100 and 400. This way, whenever a new ‘targx’ is calculated, its kept within those points.

---

<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 6, 2003, 4:12pm UTC](https://forum.kirupa.com/t/scripting-a-boundary/10688/10 "2003-01-06T16:12:46Z")

</div>

THANK YOU!!!

---

<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 6, 2003, 5:07pm UTC](https://forum.kirupa.com/t/scripting-a-boundary/10688/11 "2003-01-06T17:07:52Z")

</div>

I am so annoying. The code worked perfectly for my random line movement (I have both lines and words doing the same thing with the same exact code), but it didn’t work for my words…any advice? so sorry.

---

<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 6, 2003, 5:43pm UTC](https://forum.kirupa.com/t/scripting-a-boundary/10688/12 "2003-01-06T17:43:35Z")

</div>

what about it doesnt work?

---

<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 6, 2003, 5:59pm UTC](https://forum.kirupa.com/t/scripting-a-boundary/10688/13 "2003-01-06T17:59:49Z")

</div>

Ill assume its because you want none of the word to cross your set boundry and not just its ‘position’

so to fix that you can replace

this.targx = Math.randomRange(100,400)

with

var b = this.getBounds(this);  
this.targx = Math.randomRange(100-b.xmin,400-b.xmax);

---

<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 6, 2003, 8:36pm UTC](https://forum.kirupa.com/t/scripting-a-boundary/10688/14 "2003-01-06T20:36:31Z")

</div>

jeez, I don’t understand why its not working…I probably put it in the wrong place or something. It’s like it’s not even being read, it doesn’t matter what I do to change the numbers or anything.

You are right about what you assumed…I don’t want the words to pass the boundary…here is the latest code

Math.randomRange = function(min, max){  
return Math.floor(Math.random()_(max-min+1))+min;  
}  
function getdistance(x, x1) {  
var run, rise;  
run = x-x1;  
return (\_root.hyp(run));  
}  
function hyp(a, b) {  
return (Math.sqrt(a_a+b\*b));  
}  
MovieClip.prototype.reset = function() {  
// -------------------  
var dist, norm;  
this.x = this.\_x;  
this.speed = Math.random()\*5+2;  
var b = this.getBounds(this);  
this.targx = Math.randomRange(100-b.xmin,300-b.xmax);  
dist = \_root.getdistance(this.x, this.targx);  
norm = this.speed/dist;  
this.diffx = (this.targx-this.x)\*norm;  
};  
MovieClip.prototype.move = function() {  
if (\_root.getdistance(this.x, this.targx)\>this.speed) {  
this.x += this.diffx;  
} else {  
this.x = this.targx;  
if (!this.t) {  
this.t = getTimer();  
}  
if (getTimer()-this.t\>1000) {  
this.reset();  
this.t = 0;  
}  
}  
this.\_x = this.x;  
};  
for (i=0; i\<5; i++) {  
\_root.line.duplicateMovieClip(“line”+i, i);  
myLine = \_root[“line”+i];  
myLine.\_x = Math.random()\*width;  
}

Thanks again for all of your help…I am not good at this

---

<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 6, 2003, 8:48pm UTC](https://forum.kirupa.com/t/scripting-a-boundary/10688/15 "2003-01-06T20:48:08Z")

</div>

that should work there… are you sure you are using move on the text movieclip?

you didnt re-define move again somewhere else did you? If so that would replace that code with its own.

---

<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 6, 2003, 8:55pm UTC](https://forum.kirupa.com/t/scripting-a-boundary/10688/16 "2003-01-06T20:55:09Z")

</div>

Let’s see, I do have move on the text movieclips…and they are moving so I am assuming it’s in the right spot. The words just continue on over the set boundary.

---

<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 6, 2003, 9:54pm UTC](https://forum.kirupa.com/t/scripting-a-boundary/10688/17 "2003-01-06T21:54:29Z")

</div>

Here is a link to the swf file. I put it up so you can see what I am talking about.

[Link](http://www.code-fiends.com/example.htm)

I don’t want the words to hit any white area on the left, and then on the right, I want them to stay inside of the first navy blue outline that you see. Basically within the first box that it’s already contained in.

I know I keep saying this, but thank you so much for helping me out. I know you are probably busy, so if you can’t get to this I totally understand. You helped me a ton already…

Thanks!

---

<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 6, 2003, 10:38pm UTC](https://forum.kirupa.com/t/scripting-a-boundary/10688/18 "2003-01-06T22:38:18Z")

</div>

works fine for me

---

<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 6, 2003, 10:47pm UTC](https://forum.kirupa.com/t/scripting-a-boundary/10688/19 "2003-01-06T22:47:45Z")

</div>

oh my gosh! It does work for you…haha, ok, I am going to compare code.

I don’t even know how to begin to tell you how much I appreciate your help! Seriously, I know you volunteer your help and I can’t even thank you enough. I hardly ever post, (I just read mainly) but I am glad I did! THANK YOU!

---

<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 6, 2003, 10:53pm UTC](https://forum.kirupa.com/t/scripting-a-boundary/10688/20 "2003-01-06T22:53:19Z")

</div>

Welcome 🙂

all I ask in return is that if and when someone else needs help, and you are capable of and able to help them, then please do 😉

[Next page](https://forum.kirupa.com/t/scripting-a-boundary/10688.md?page=2)
