Adding a little randomness

currently I have an animation which continully repeats the letter i moving across the screen. It is reset when the i comes in contact with a MC called reset. Here is the code I am using:
onClipEvent (enterFrame) {
if (this.hitTest(_root.reset)) {
this._x = -410;
} else {
speed = .5;
this._x += speed;
}
}
Currently I have a dozen or so MCs at different sizes, alphas and y positions. I would like to modify this code so when they reappear they are randomly placed withing a y_range, randomly given alpha values within a range (ie between 10% and 35%), and random speed values (ie between .1 and 1.9)… also if possible Id like to include random size (but this is not necessary.
Please help!!! :slight_smile: Thanks
Peace

That is the power of Math.random().

for your _y position it would look like

this._y = Math.random()*400; (if the height of your movie is 400)

You can use this for all the properties that require numbers.

For your size problem, that would be _xscale and _yscale, to keep these the same, so there isn’t any oblong distortion you can do this…

mySize = Math.random()*100 //maximum size you want to go
this._xscale = mySize;
this._yscale = mySize;

If it lands on 100, your graphic will be 100 x 100

NOTE: To have non-decimal numbers you will need to do this…

Math.round(Math.random()*400);

I hope this helps at all.:slight_smile:

thank you! one question…
if I want to get random alpha values between 0 and 45% I put

this.alpha=Math.random()*45 right?, but what if I want values between 15% and 45%??? what then???

Peace

Hmm, good question, I have never done anything like that. Possibly…

if (this._alpha < 15) {
this._alpha = 15;
}

Maybe that will work:)

By The Way - you wrote it right

thanks… I’ll let you know what works!!!
Peace

PS one more question… if I place the random function inside my if else loop wont it be given new values constantly until it hits the reset MC???

basiclly I dont want any values to change until reset, then I want those values to remain until once again hitting reset…hmmm???

No, it should be given a constant value until hitting the reset clip. After it hits the reset clip it should be given new values.

I put it like this
onClipEvent (enterFrame) {
if (this.hitTest(_root.reset)) {
this._x = -410;
} else {
speed = Math.random()*2;
this._x += speed;
}
}
this makes the MC fluctuate its speed constantly

Try something like this…


onClipEvent (load) {
	speed = Math.random()*2;
}
onClipEvent (enterFrame) {
	this._x += speed;
	if (this.hitTest(_root.reset)) {
		this._x = -410;
	}
}

I just noticed everything was in a onEnterFrame, if you load the variables in the onLoad, everything should stay constant.

EDIT: This solution doesn’t solve it so that after it hits, the objects get new values. I will see what I can do about that.

ok now I think i have it

onClipEvent (enterFrame) {
if (this.hitTest(_root.reset)) {
this._x = -410;
speed = Math.random()*2
} else {
this._x += speed;
}
}

but the only way I can make this work is if I start the MC off in contact with the MC reset. I’d like to assign a beginning value that gets dumped and randomized after the first reset? is there some null command I sould use… advice? Thanks
Peace

using that will the value change every time it resets?
nevermind I got it using this code:

onClipEvent (load) {
speed = Math.random()*2;
}
onClipEvent (enterFrame) {
this._x += speed;
if (this.hitTest(_root.reset)) {
this._x = -300;
speed = Math.random()*2
}
}

now it starts with a random then EVERY time it resets it travels with a new random speed.

HAHA, I fixed it that way too, I just didn’t get a chance to post beacuse I was talking to someone.