Copy link to clipboard
Copied
I have an object that Im currently rotating (360) degrees, I would like to give it a random rotation.
To move it in one direction, and then the other. I'm not sure the best way to do it.
Any advice/direction would be helpful
(below is the working code I have...I was experimenting with the TIMER to get it to rotate
every second based on a positive or negative random number.....I took out the timer, so you can see the code that works)
try:
var sumnum:Number;
compass_mc.addEventListener(Event.ENTER_FRAME, compass);
randomizer()
function randomizer() {
sumnum=(Math.floor(Math.random() * (30 - (-30) + 1)) + (-30));
trace(sumnum);
}
function compass(e:Event) {
compass_mc.rotation = .5*(compass_mc.rotation+sumnum);
if(Math.abs(compass_mc.rotation-sumnum)<1){
randomizer();
}
}
Copy link to clipboard
Copied
try:
var sumnum:Number;
compass_mc.addEventListener(Event.ENTER_FRAME, compass);
randomizer()
function randomizer() {
sumnum=(Math.floor(Math.random() * (30 - (-30) + 1)) + (-30));
trace(sumnum);
}
function compass(e:Event) {
compass_mc.rotation = .5*(compass_mc.rotation+sumnum);
if(Math.abs(compass_mc.rotation-sumnum)<1){
randomizer();
}
}
Copy link to clipboard
Copied
That did it! Thanks a alot!
I had tried some code myself and got it really close to what I wanted it to do
but your code if far more elegant.
I'm sadly not a good mathamatichain. so I will have to take a closer look at your code
to really see what its doing (so it makes sence to me), learn from it, and maybe adapt
it (slower rotation)
Thank you again!
Copy link to clipboard
Copied
you're welcome. if you want to adjust the easing use:
var sumnum:Number;
var easeSpeed:Number = .2; // use a number between 0 and 1compass_mc.addEventListener(Event.ENTER_FRAME, compass);
randomizer()
function randomizer() {
sumnum=(Math.floor(Math.random() * (30 - (-30) + 1)) + (-30));
trace(sumnum);
}
function compass(e:Event) {
compass_mc.rotation = easeSpeed*compass_mc.rotation+(1-easeSpeed)*sumnum;
if(Math.abs(compass_mc.rotation-sumnum)<1){
randomizer();
}
}
Copy link to clipboard
Copied
I've given the equation a look and I'm a bit confused. The first equation in compass is somewhat straight forward. The end number gives it the rotation
between -30 and 30 (the sumnum will give its direction)(?). The two things I'm unclear on is how the easing works (how dose the math slow it down or speed it up)
and why do you use the absolute vale as a test?
Thanks for time...and have a Happy New Year
Copy link to clipboard
Copied
the key line is:
compass_mc.rotation = easeSpeed*compass_mc.rotation+(1-easeSpeed)*sumnum;
which defines a sequence of compass_mc.rotation numbers, r1,r2,r3,... . this sequence converges to sumnum. (eg, take the limit of both sides so the compass_mc.rotation on the left and the right are the same number, do a little algebra and you'll find compass_mc.rotation = sumnum.)
the rate of convergence is determine by easeSpeed. if easeSpeed=0, then convergence is immediate. ie, r1=r2=...=sumnum. as easeSpeed approaches 1 (from the left), the convergence slows. ie, r2 is just slightly different from r1 and r3 is just slightly different than r2 etc.
i use Math.abs() to ensure compass_mc.rotation-sumnum are close to each other without having to worry about which is greater.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more