A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: limited rotation

  1. #1
    Senior Member
    Join Date
    Nov 2004
    Location
    Amsterdam
    Posts
    212

    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.

  2. #2
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    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.
    Last edited by Ralgoth; 07-19-2008 at 11:23 PM.
    Search first, asked questions later.

  3. #3
    Senior Member
    Join Date
    Nov 2004
    Location
    Amsterdam
    Posts
    212
    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 :-).

  4. #4
    Senior Member
    Join Date
    Nov 2004
    Location
    Amsterdam
    Posts
    212
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center