14 Replies Latest reply on May 9, 2009 5:21 PM by Ned Murphy

# rotate a mc 360º in 45º 'smooth' steps with buttons left & right

Hi everyone,

I thought it should be easy.

I have a circular mc (wheel2) and this should rotate 'smooth' in steps of 45º, to the left and to the right (buttons).

The script I use on the button (right);

on(release){
var rotate:Function = function (obj:MovieClip) {
obj._rotation += 3;
if (obj._rotation == 45) {
clearInterval(interval);
}
};
var interval:Number = setInterval(rotate, 10, wheel2);
stop();
}

It moves ok 'once' then it spins 360º. Should I set a new value for my mc??

Thanks

pascal

• ###### 1. Re: rotate a mc 360º in 45º 'smooth' steps with buttons left & right

try:

on(release){
var rotate:Function = function (obj:MovieClip,rotation:Number) {
obj._rotation += 3;
if (Math.abs(obj._rotation -rotation)<1) {
clearInterval(obj.rotateI);
}
};
wheel2.rotateI = setInterval(rotate, 10, wheel2,45);
stop(); // this is probably doing nothing
}

• ###### 2. Re: rotate a mc 360º in 45º 'smooth' steps with buttons left & right

Without testing it myself, I'd say your problem lies with the following lines, the first not being a problem:

obj._rotation += 3;
if (obj._rotation == 45) {

The first line is incrementing the _rotation value, which is what you want to do, but the second is reading it.  But it only equals 45 at one location.  the other _rotation values for your 45 degree steps are 90, 135, 180, etc....

So you need to have a counter that keeps track if the number of increments adds up to 45 (15 substeps of 3) rather than checking the _rotation value.

• ###### 3. Re: rotate a mc 360º in 45º 'smooth' steps with buttons left & right

But with this script  it keeps spinning round.  no steps at all.

Any suggestion...

Thanks

Op 6-mei-2009, om 15:14 heeft kglad het volgende geschreven:

try:

>

>>

>> on(release){

>> var rotate:Function = function (obj:MovieClip,rotation:Number) {

>> obj._rotation += 3;

>> if (Math.abs(obj._rotation -rotation)<1) {

>> clearInterval(obj.rotateI);

>> }

>> };

>> wheel2.rotateI = setInterval(rotate, 10, wheel2,rotation);

>> stop(); // this is probably doing nothing

>> }

>>

>

• ###### 4. Re: rotate a mc 360º in 45º 'smooth' steps with buttons left & right

Thanks Ned,

But as a designer I'm not really into scripting, it's amazing to see/

know what's possible with it.

Could you give me a hint on the 'counter' setting...

Thanks

Pascal

Op 6-mei-2009, om 15:22 heeft Ned Murphy het volgende geschreven:

Without testing it myself, I'd say your problem lies with the

following lines, the first not being a problem:

>

obj._rotation += 3;

if (obj._rotation == 45) {

>

The first line is incrementing the _rotation value, which is what

you want to do, but the second is reading it.  But it only equals

45 at one location.  the other _rotation values for your 45 degree

steps are 90, 135, 180, etc....

>

So you need to have a counter that keeps track if the number of

increments adds up to 45 (15 substeps of 3) rather than checking

the _rotation value.

>

• ###### 5. Re: rotate a mc 360º in 45º 'smooth' steps with buttons left & right

did you copy and paste the code i suggested?

• ###### 6. Re: rotate a mc 360º in 45º 'smooth' steps with buttons left & right

Try the following:  on the timeline, have a variable:

var stepCount = 0;

on(release){

var rotate:Function = function (obj:MovieClip) {
obj._rotation += 3;

stepCount += 1;
if (stepCount == 15) {
clearInterval(interval);

stepCount = 0;
}
};
var interval:Number = setInterval(rotate, 10, wheel2);
stop();
}

Another thing you might want to do is disable your buttons until each interval is cleared.  Otherwise things could get out of control.  If you want to allow for that, then a different approach might be necessary where you keep track of where you are rotation-wise.

• ###### 7. Re: rotate a mc 360º in 45º 'smooth' steps with buttons left & right

Thanks Ned,

It works great, but as you suggested, if I click multiple times on a button it gets crazy.

How should I disable the button while rotating...

• ###### 8. Re: rotate a mc 360º in 45º 'smooth' steps with buttons left & right

Ehhh yes...not good ?

Op 6-mei-2009, om 15:45 heeft kglad het volgende geschreven:

did you copy and paste the code i suggested?

>

• ###### 9. Re: rotate a mc 360º in 45º 'smooth' steps with buttons left & right

try the code i suggested.

• ###### 10. Re: rotate a mc 360º in 45º 'smooth' steps with buttons left & right

there's no problem with the code i posted.  your object should rotate to the correct position.  use the following though if you click the same button repeatedly.

```on(release){
var rotate:Function = function (obj:MovieClip,rotation:Number) {
obj._rotation += 3;
if (Math.abs(obj._rotation -rotation)<1) {
clearInterval(obj.rotateI);
}
};
clearInterval(wheel2.rotateI);
wheel2.rotateI = setInterval(rotate, 10, wheel2,45);
stop(); // this is probably doing nothing
}
```
• ###### 11. Re: rotate a mc 360º in 45º 'smooth' steps with buttons left & right

To disable the buttons you'll need to assign instance names to them in the properties panel, let's say you name them leftBtn and rightBtn...

on(release){

leftBtn.enabled = rightBtn.enabled = false;

var rotate:Function = function (obj:MovieClip) {
obj._rotation += 3;

stepCount += 1;
if (stepCount == 15)
{
clearInterval(interval);

stepCount = 0;

leftBtn.enabled = rightBtn.enabled = true;
}
};
var interval:Number = setInterval(rotate, 10, wheel2);
}

• ###### 12. Re: rotate a mc 360º in 45º 'smooth' steps with buttons left & right

Thanks Ned,

Works like a charme now, no more crazy spinning wheel.

I'll post you the link if it's online, if you want.

Pascal

• ###### 13. Re: rotate a mc 360º in 45º 'smooth' steps with buttons left & right

Thank you for your help, I'll try your script to see if it does the same as Ned's option.

I used the script from Ned and this works fine for me. So my question is answered.

Thanks for your time and help.

Pascal

the final result can be found here for today: http://www.clownfish.nl/klanten/genzyme/oog_diagnose_wijzer.html

• ###### 14. Re: rotate a mc 360º in 45º 'smooth' steps with buttons left & right

You're welcome, Pascal.