Scripting a boundary

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!

Can you post your code?

you can try this:


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 )

Senocular, you impress me :slight_smile:

*Originally posted by ilyaslamasse *
**Senocular, you impress me :slight_smile: **

:beam:

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(aa+bb));
}
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!

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

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!

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


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.

THANK YOU!!!

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.

what about it doesnt work?

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);

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

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.

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.

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

Link

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!

works fine for me

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!

Welcome :slight_smile:

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 :wink: