Home Tutorials Forums Articles Blogs Movies Library Employment Press

 ActionScript.org Forums Hour hand movement based on senocular code
 Register FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

 06-21-2005, 09:18 AM #1 voltron Registered User   Join Date: Jun 2005 Posts: 18 Hour hand movement based on senocular code Hi all, Hi Senocular, I´m trying to replicate the movement of the hour hand of an analogue clock, I am using Senoculars excellent code for the mouse to minute hand movement, but I can´t seem to get the right equation for the hour hand. The idea behind this is that I would like the user to have the ability to set the time base his/herself by moving the clock hands. the code: ActionScript Code: ```// On the minute hand on(press){     trace("minute");     dragging = true;     orig_mouse_angle = Math.atan2(_parent._ymouse-_y, _parent._xmouse-_x) * 180/Math.PI;     orig_rotation = _rotation;     _parent.hourhand.orig_rotation = _parent.hourhand._rotation;     orig_x = _parent._xmouse;     this.gotoAndStop(2); } onClipEvent(mouseMove){     if(dragging){         curr_mouse_angle = Math.atan2(_parent._ymouse-_y, _parent._xmouse-_x) * 180/Math.PI;         _rotation = orig_rotation + curr_mouse_angle - orig_mouse_angle;         _parent.hourhand._rotation = (_parent.hourhand.orig_rotation + curr_mouse_angle - orig_mouse_angle)/60;         updateAfterEvent();     } } on(release,releaseOutside){     dragging = false;     this.gotoAndStop(1);     }``` Dividing the rotation angle of the minute hand and assigning it to the hour hand does not work as expected. Any ideas? I appreciate your anticipated help. Thanks
 06-22-2005, 08:21 AM #2 voltron Registered User   Join Date: Jun 2005 Posts: 18 Anybody? Senocular?
 06-23-2005, 10:18 AM #3 voltron Registered User   Join Date: Jun 2005 Posts: 18 44 views no answer, would it be better if I posted a file?
 06-23-2005, 11:16 AM #4 senocular six eyes     Join Date: Jan 2003 Location: San Francisco, CA (USA) Posts: 7,875 Hey voltron, Sorry I didnt answer earlier but I must have missed this thread yesterday. You problem is slightly tricky and involves a common problem experienced when dealing with rotation. First off, I dont think trying to take the mouse rotations and using that with the hourhand's _rotation is going to work. One problem with that is that moving your mouse around the clock 360 degrees would mean that the position of the hour hand would be exactly where it was when you started (since the mouse rotation after 360 degrees is the same), however, what should really happen, is the hour hand should have moved 1-twelfth of a full rotation as it would have moved by 1 hour. So instead of rotating the hour hand based on its relation to the mouse, we can rotate it based on how much the minute hand rotates, or more specifically, how much it rotates each time the mouse moves when its being dragged. Each time the minute hand moves, the hour hand should move 1-twelfth of that (1/12) - and thats not 1/60. Though there are 60 minutes in an hour, the rotation of a clock is based 12 hours. So 60 minutes (one full rotation of the minute hand) equals 1 hour which is 1/12 of a circle meaning the hour hand moves 1/12 the distance of the minute hand. So first things first, we now need to know how much the minute hand moves each time its moved. We need a new variable for this. We can call it last_rotation. ActionScript Code: `last_rotation = _rotation;` It saves the rotation of the minute hand since the last time it was moved (this assignment is set after the current rotation has been figured out so it will be used to compare for finding the next rotation) so we can compare it to the current rotation and find out how much it has changed. ActionScript Code: `change_in_rotation = _rotation - last_rotation;` That change is then divided by 12 and added to the current rotation of the hour hand ActionScript Code: `_parent.hourhand._rotation += change_in_rotation/12;` But wait! Here's the tricky part. Flash only recognizes rotation as being a value from -180 to 180. Nothing higher, nothing lower. So, when you move your minute hand around the clock a bunch of times, you arent going to get 170 degrees, 175 degrees, 180, 190, 200. Once it passes 180, its going back to -180 and will start back up from there. The problem is, what happens to the change_in_rotation when you make that jump? Consider moving from 175 to -180. ActionScript Code: ```change_in_rotation = _rotation - last_rotation; // becomes change_in_rotation = -180 - 175; // which is change_in_rotation = -355;``` A change of -355 degrees is not the 5 degrees you actually moved. To correct this, a simple if-else can be used to check for large values for the change and correct it as needed by adding or subtracting 360. Here is the full script: ActionScript Code: ```// On the minute hand on(press){     trace("minute");     dragging = true;     orig_mouse_angle = Math.atan2(_parent._ymouse-_y, _parent._xmouse-_x) * 180/Math.PI;     orig_rotation = _rotation;         last_rotation = _rotation; // save the last rotation when clicked         orig_x = _parent._xmouse;     this.gotoAndStop(2); } onClipEvent(mouseMove){     if(dragging){         curr_mouse_angle = Math.atan2(_parent._ymouse-_y, _parent._xmouse-_x) * 180/Math.PI;         _rotation = orig_rotation + curr_mouse_angle - orig_mouse_angle;                 change_in_rotation = _rotation - last_rotation; // find change in rotation          // make sure change is reasonable and not a jump from - to + or vise versa         if (change_in_rotation > 180) change_in_rotation -= 360;         else if (change_in_rotation < -180) change_in_rotation += 360;         _parent.hourhand._rotation += change_in_rotation/12; // set hour rotation by 1/12 change for minute         last_rotation = _rotation; // remember the current rotation as new last rotation                 updateAfterEvent();     } } on(release,releaseOutside){      dragging = false;      this.gotoAndStop(1); }``` __________________ (6)
 06-23-2005, 01:07 PM #5 voltron Registered User   Join Date: Jun 2005 Posts: 18 WHOA!! Thats gonna take me the whole evening to read and understand!! It Works perfectly, just as expected. Senocular |5 7h3 6r347357! Thank you very very much.
 06-23-2005, 01:16 PM #6 senocular six eyes     Join Date: Jan 2003 Location: San Francisco, CA (USA) Posts: 7,875 welcome __________________ (6)

 Thread Tools Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home ActionScript Forums Group     ActionScript 3.0     ActionScript 2.0     ActionScript 1.0 (and below)     Simple Stuff (Newbies) Flash General Questions     Flash 10 General Questions     Flash 9 General Questions     Flash 8 General Questions     Other Flash General Questions Flex     Flex 2, 3 & 4     Flex 1 Extensions and Plugins     Components     JSFL - Extending Flash Desktop, Mobile and non-browser Environments     AIR (Apollo)     FlashLite / Portable Devices Development     Projectors and CDs Supporting Technologies     HTML and JavaScript     haXe     Server-Side Scripting     Flash Remoting     Flash Media Server General     Best Practices     Gaming and Game Development     Animation and Effects     Flashants Support Forum Community Boards     General Chat     Just for Kicks Challenges     Detention Flash In Action     Site Check     Cool Sites     Widgets Decommissioned     Projects and Positions CMS Forums     Announcements Board     Content Postings / Updates     Product Review Requests     CMS Technical Questions     Process Questions     Collaboration & Suggested Articles

All times are GMT. The time now is 02:09 AM.

///