Random moving mc tutorial question

Greetings!
Having followed the tut on the random moving mc I have 1 question. I would like the mc to move inside a box I have on my stage. Do I need to do this as a seperate swf file and load it into my main movie? Adjusting the width and height in the code given does nothing. My mc keeps sailing out of the box and hanging around the left side of the screen. If I am correct the coded paramaters for the moviing mc is in reference to the size of the total movie (my main movie is like 700x500, my box is 570 by 400). I wanna keep the kids in my sandbox.
Any ideas?
Thanks!

P.S. I seem to be unable to import the above movie into another’s library. I exported the movie as a swf (of course) and then tried to import it into library of my main movie. All I was able to get were the symbols, but not the swf. Any ideas why that would be?
Thanks again.

:q:
Any idea?

Check the KirupaLab, Lost did a random thing with a much simpler random motion :slight_smile:

specify the minimum and maximum coordinates
minimumx = some value
minimumy = some value
maximumx = some value
maximumy = some value

take the difference between the min and max coordinates
differencex = maximumx - minimumx
differencey = maximumy - minimumy

generate the random coordinates
randomx = Math.round ((Math.random() * differencex) + minimumx)
randomy = Math.round ((Math.random() * differencey) + minimumy)

am i making any sense ?? :sure:

say that you have a movie 400 x 400
but you just want to move the movie clip inside a 200 x 200 area in the middle of the movie

minimumx = 100
minimumy = 100
maximumx = 300
maximumy = 300

differencex = maximumx - minimumx
differencey = maximumy - minimumy

randomx = Math.round ((Math.random() * differencex) + minimumx)
randomy = Math.round ((Math.random() * differencey) + minimumy)

the script would generate a number between 100 and 300
so the mc would be moved only inside the specified area :slight_smile:

hope it helps =)

[size=1][ edit ]
oops … seems like it took me years to write my reply :P[/size]

Much thanks for the help!
So where in the code would I add your additional script?
Do I take out the width= and the height= and replace with what you gave me?
Here is the script I have:

function getdistance(x, y, x1, y1) {
var run, rise;
run = x1-x;
rise = y1-y;
return (_root.hyp(run, rise));
}
function hyp(a, b) {
return (Math.sqrt(aa+bb));
}
MovieClip.prototype.reset = function() {
// specify the width and height of the movie
width = 500;
height = 350;
// -------------------
var dist, norm;
this.x = this._x;
this.y = this._y;
this.speed = Math.random()*4+2;
this.targx = Math.random()*width;
this.targy = Math.random()*height;
dist = _root.getdistance(this.x, this.y, this.targx, this.targy);
norm = this.speed/dist;
this.diffx = (this.targx-this.x)*norm;
this.diffy = (this.targy-this.y)*norm;
};
MovieClip.prototype.move = function() {
if (_root.getdistance(this.x, this.y, this.targx, this.targy)>this.speed) {
this.x += this.diffx;
this.y += this.diffy;
} else {
this.x = this.targx;
this.y = this.targy;
if (!this.t) {
this.t = getTimer();
}
if (getTimer()-this.t>1000) {
this.reset();
this.t = 0;
}
}
this._x = this.x;
this._y = this.y;
};

Thanks!
:cowboy:

that’s so much scripting for such a little thing :-\

MovieClip.prototype.motion = function (minx, miny, maxx, maxy) {
	newpos = function () {
		dx = maxx - minx
		dy = maxy - miny
		ranx = Math.round ((Math.random () * dx) + minx)
		rany = Math.round ((Math.random () * dy) + miny)
	}
	this.onEnterFrame = function () {
		this._x += ((ranx - this._x) * .1)
		this._y += ((rany - this._y) * .1)
	}
	newposInterval = setInterval (newpos, 1000)
}
instance.motion (minx, miny, maxx, miny)

in the last line of the code
replace instance with the instance name of your movie clip
and place there the minimum x and y values and the maximum x and y values

Here’s the code i did:

MovieClip.prototype.motion = function(minx, miny, maxx, maxy) {
newpos = function () { dx = maxx-minx;dy = maxy-miny;ranx = Math.round((Math.random()dx)+minx);rany = Math.round((Math.random()dy)+miny);};
this.onEnterFrame = function() {
this._x += ((ranx-this._x)
.1);
this._y += ((rany-this._y)
.1);
};
newposInterval = setInterval(newpos, 1000);
};
mycircle1.motion(minx=100, miny=100, maxx=300, miny=300)

Now, one of my movie clips to be moved about is called “mycircle1” as you see above. I also have instances of “mycircle2” and “mycircle3” on my stage. All have an:

onClipEvent (enterFrame) {
move();
}
action on them.

So mycircle1 is not moving at this point and I am not sure how I am going to animate each seperate clip.

I suppose everybody is getting tired of this enquire. Thanks for the patience. I still need to work this out.
Thanks.

to call the function from the timeline:

mycircle1.motion (100, 100, 300, 300)

to call the function from the mc actions:

onClipEvent (load) {
this.motion (100, 100, 300, 300)
}

Well it’s working.
Thanks for the help.
Alas the motion is too fast and I can’t seem to get each mc (all seperate mcs not instances of the same one) circle to float around independently (they seem to group together and move together).

Anyhow, I’ll tool around with it.
The original script worked well but for some reason I can’t publish it in a form that will import into another movie’s library, and the width= and height= never seem to adjust anything.

Cheers!
Eli

Ok well I wasn’t able to import the swf into my library but I was able to loadmovie the swf and load into target.

Oddly enough the circles now jump as oppose to move!

I tried adjusting the frame rate(redid fla and re-exported so that swf is 18fps and so is my main) but that didn’t do anything. Somethin is amiss here. I am working with the original script (seen above as suprabeener’s script) cause the speed was better and I was able to have each mc move independently as oppose to immediatly grouping together and moving together which was the problems I had with the shorter script posted above.

Adjusting width and height in the suprabeener script works but will send all the circles to the upper left of my movie i.e. my main movie doc is 550 x 400 my random moving circles are set width=300 height=100 and so that 300x100 area becomes the upper left of 550 x 400. This is no good cause I need to place the 300x100 random movement a specific codinates inside the main movie. Hence my desire to just loadmovie into target mc the random movement swf.

I think I need to find suprabeener to help with this problem.
Where he at?

Can’t figure out why as a fla all moves smooth but as a exported swf it’s all jumpy.
Thanks!
Eli:trout:

xmin = 200
ymin = 200
ymax = 400
xmax = 400

newxy = function () {
	dx = xmax - xmin
	dy = ymax - ymin
	ranx = Math.round ((Math.random () * dx) + xmin)
	rany = Math.round ((Math.random () * dy) + ymin)
}
newxy ()

this.onEnterFrame = function () {
	this._x += ((ranx - this._x) * .1)
	this._y += ((rany - this._y) * .1)
	if (Math.ceil (this._x) == ranx || Math.ceil (this._y) == rany) {
		newxy ()
	}
}

sorry … i’ve been completely busy :-\

if you use that one place it in the mc’s timeline :slight_smile:

:flower:
Well, with the inclusion of your code I was able to get everything moving. In addition I was able to exort and import into my main movie just fine. Gracias!

With your script my circles move in a stop and go fashion. Is there a way to have a more continuos movement?

Also how does the paramaters for the mc A.S. (200, 200, 400, 400) work with the main timeline A.S. which also has miny, minx, and maxy, maxx, parameters as well? I mean as I match up your 1st initial code with the secondary code you gave. Does one overide the other or do they work together?

Also I discovered that it is possible to have the random moving circles as buttons but is there a way to stop a circle’s movement upon mouse over? I may be getting into too deep of waters with that one but I thought I’d ask anyways.
Thanks!
:cowboy:

first … the script calls the newxy function when the mc reaches the previous x and y coordinates

if you want to make it more continuos … i guess you coud use an interval so it calls newxy after the time you want

newxy = function () {
	ranx = Math.round ((Math.random () * (xmax - xmin)) + xmin)
	rany = Math.round ((Math.random () * (ymax - ymin)) + ymin)
}
newxy ()
newxyInterval = setInterval (newxy, **millisecons**)

replace milliseconds with the time in milliseconds you want

and if you want to stop it when the mouse is over it

this.onEnterFrame = function () {
	if (!this.hitTest (_root._xmouse, _root._ymouse, true)) {
		this._x += ((ranx - this._x) * .1)
		this._y += ((rany - this._y) * .1)
	}
}

the script should be something like this:

xmin = 0
ymin = 0
ymax = 0
xmax = 0

newxy = function () {
	ranx = Math.round ((Math.random () * (xmax - xmin)) + xmin)
	rany = Math.round ((Math.random () * (ymax - ymin)) + ymin)
}
newxy ()
newxyInterval = setInterval (newxy, 1000)

this.onEnterFrame = function () {
	if (!this.hitTest (_root._xmouse, _root._ymouse, true)) {
		this._x += ((ranx - this._x) * .1)
		this._y += ((rany - this._y) * .1)
	}
}

[size=1]note: the script will change x and y corrdinates every second. like i said … change it to anything you want[/size]

Out of curiosity anyone know why Suprabreener’s script for random mc does not export properly?
Meaning I export the random mc and import into my main movie into a target mc aaaandd the fluidity of the movement is lost. All I get is a extremely choppy version like I am missing frames or something.
Thanks.

:whistle: