# Untitled

By: a guest on Jun 9th, 2012  |  syntax: None  |  size: 2.12 KB  |  hits: 20  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. Adobe Flash cs4 rotation math
2. var angle:Number = Math.atan2(foodTarget.y - y, foodTarget.x - x);
3.             foodLocationDegrees =Math.floor((angle  * 180 / Math.PI));
4.
5. public function moveToFood():void
6.         {
7.
8.             var dx:Number = x - _destinationX;
9.             var dy:Number = y - _destinationY;
10.
11.             trace("Food location degree relative to fish mouth "+foodLocationDegrees);
12.             var targetRotation:Number = 0;
13.             if (foodLocationDegrees > 0 && foodLocationDegrees < 90)
14.             {
15.             trace("food is on 1st quadrant of the fish mount");
16.                 this.x -= dx / 18;
17.                 this.y -= dy / 18;
18.             }else if (foodLocationDegrees > 90 && foodLocationDegrees < 180)
19.             {
20.             trace("food is on 2nd quadrant of the fish mount");
21.
22.                 this.x -= dx / 18;
23.                 this.y -= dy / 18;
24.             }else if (foodLocationDegrees > -180 && foodLocationDegrees < -90)
25.             {
26.                 trace("food is on 3nd quadrant of the fish mount");
27.
28.                 this.x -= dx / 18;
29.                 this.y -= dy / 18;
30.             }else if (foodLocationDegrees < 0 && foodLocationDegrees > -90)
31.             {
32.                 trace("food is on 4nd quadrant of the fish mount");
33.
34.                 this.x -= dx / 18;
35.                 this.y -= dy / 18;
36.             }
37.             trace("Before adding Rotation " + rotation);
38.             var number:Number = (foodLocationDegrees - (rotation - 90)) * .1;
40.             rotation += (foodLocationDegrees - (rotation - 90)) * .2;
41.
42.             trace("After adding Rotation " + rotation);
43.
44.             //removing food when both hit boxes hit
45.             if (hit.hitTestObject(foodTarget.hit))
46.             {
47.                 foodInPond--;
48.                 foodTarget.removeSelf();
49.             }
50.
51.         }
52.
53. public function moveToFood(food:Food):void
54. {
55.     var speed:Number = 6.5;
56.
57.     var a:Number = food.x - x;
58.     var b:Number = food.y - y;
59.     var ang:Number = Math.atan2(b, a);
60.
61.     rotation = ang * 180 / Math.PI;
62.
63.     x += Math.cos(ang) * speed;
64.     y += Math.sin(ang) * speed;
65. }