-
limited rotation
hi,
I have two movie clips, mc1 and mc2. I'd like to limit the rotation of mc1, depending on the rotation of mc2 (in an enterframeListener), think of a kinematic doll, that only can rotate its head 50 degress, relatively to the rotation of the body.
How would I do that?
Something like:
Code:
if(mc1.rotation >= mc2.rotation + 50) {
mc1.rotation = mc2.rotation + 50;
};
if(mc1.rotation <= mc2.rotation - 50) {
mc1.rotation = mc2.rotation - 50;
}
works fine, except when the rotation is higher than 130, because then
it passes the 180 degrees and the rotation changes into -180, which gives undesired results.
thanks,
JerryJ.
-
try something like this...
PHP Code:
// These two lines are basic 'if/then/else' statements
// if mc1.rotation>0, then rot1 = mc1.rotation, else rot1 = mc1.rotation+360
var rot1:Number = (mc1.rotation>0) ? mc1.rotation : mc1.rotation+360;
var rot2:Number = (mc2.rotation>0) ? mc2.rotation : mc2.rotation+360;
// I removed the = from the if statements, if rot1 == rot2+50, then you dont need to change it
if(rot1 > rot2 + 50) {
mc1.rotation = mc2.rotation + 50;
};
if(rot1 < rot2 - 50) {
mc1.rotation = mc2.rotation - 50;
}
Also, you might be interested in reading Foundation Actionscript 3.0 Animation: Making Things Move! by Keith Peters. It has a chapter dedicated to the kind of animation you described.
-
Thanks, also for the book tip :-).
I see what you mean but it doesn't seem to solve my problem.
If rot1 (the new rotation value) goes from say 350 to 10 (20 degrees cw), and rot2 is say 360, then the second if statement starts to be valid, and mc.rotation is set to -50, where it should be allowed to go to +50.
If you know what I mean :-).
-
mm, the only way I can get rid of the pos/neg value problem is introducing a relative value, this seems to work:
Code:
addEventListener(Event.ENTER_FRAME, enterFrameListener);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
var relRot1:Number=1000000;
var relRot2:Number=1000000;
function enterFrameListener(e:Event):void{
if(relRot1 >= relRot2 + 50) {
mc1.rotation = mc2.rotation + 50;
};
if(relRot1 <= relRot2 - 50) {
mc1.rotation = mc2.rotation - 50;
}
//trace("rot1 =" + relRot1);
//trace("rot2 =" + relRot2);
}
function keyDownListener (event : KeyboardEvent) : void {
switch (event.keyCode) {
case Keyboard.LEFT :
mc1.rotation-=5;
if(relRot1 > relRot2 -50){
relRot1-=5;
}
break;
case Keyboard.RIGHT :
mc1.rotation+=5;
if(relRot1 < relRot2 +50){
relRot1+=5;
}
break;
case Keyboard.DOWN :
mc2.rotation-=5;
if(relRot1 > relRot2 +50){
relRot1-=5;
}
relRot2-=5;
break;
case Keyboard.UP :
mc2.rotation+=5;
if(relRot1 < relRot2 -50){
relRot1+=5;
}
relRot2+=5;
break;
}
}
But I'm open to other, shorter, more efficient suggestions :-),
JerryJ.