2012-11-05 66 views
0

好吧,所以我讀了很多東西,它重複了一遍,但沒有一個能解決我的問題,也沒有解決我難以理解的問題。我試圖讓一個男人的精靈,從上到下看着他,盯着頁面上的任何一個地方。然而,他不是在頭部旋轉,而是在符號的中心,他在他精靈的左上角旋轉。任何幫助?如何在其中心的動作中旋轉一個精靈

stage.addEventListener("mouseMove", eyesFollow); 

function eyesFollow(e:MouseEvent):void { 
    var a = mouseY - man_walking.y; 
    var b = mouseX - man_walking.x; 
    var radians = Math.atan2(a,b); 
    var degrees = ((180/Math.PI)*radians)+90; 
    man_walking.rotation = (degrees); 
} 

注:在此的一個職位,最適合我的有一個解決方案,它只是一個破碎的鏈接,所以我不能訪問它。

+1

精靈的位置是由左上角還是中心的座標決定的? IE,如果你在(0,0)處畫他,他是居中在(0,0)還是他的左上角在(0,0)? – GraphicsMuncher

回答

0

假設精靈的(x,y)位置在左上角,高級Lua-ish僞代碼中的一般過程(抱歉,我不太熟練的操作腳本)就如:

function rotate() 
    -- ...find how much you want to rotate... 

    -- Translate the middle point to (0, 0) 
    man.translate(-(man.x + man.width/2), -(man.y + man.height/2)) 

    -- Rotate it there. 
    man.rotate(someTranslation) 

    -- Lastly, move it back to where it was 
    man.translate((man.x + man.width/2), (man.y + man.height/2)) 

end 

如果你的庫使用計算位置和來源的不同的方法,你需要調整你需要翻譯ammount的,但問題表示沒有少:第一翻譯圖像的中間位於(0,0)處,旋轉它,然後將其移回。

我希望這有助於! :)

0

在Flash Pro中,你可以簡單地定位符號,以便0,0座標點你要旋轉:

registration-point

Yahoo AstraDynamic Registration類的東西像圍繞一個點旋轉。

這可以被實現爲以下,左右旋轉100,100

DynamicRegistration.rotate(man_walking, new Point(100, 100), degrees); 

雅虎雅特DynamicRegistration類:

/* 
Copyright (c) 2008 Yahoo! Inc. All rights reserved. 
The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license 
*/ 
package com.yahoo.astra.utils 
{ 
     import flash.geom.Point; 
     import flash.display.DisplayObject; 

     /** 
     * Allows you to manipulate display objects based on a registration point other 
     * than the standard (0,0). 
     * 
     * @author Josh Tynjala 
     */ 
     public class DynamicRegistration 
     { 
       /** 
       * Moves a <code>DisplayObject</code> to a new position (x,y) based on a registration point. The 
       * true position of the object will be (x - registration.x, y - registration.y). 
       * 
       * @param  target       the DisplayObject to move 
       * @param  registration   the registration point of the DisplayObject 
       * @param  x          the new x position, in pixels 
       * @param  y          the new y position, in pixels 
       */ 
       public static function move(target:DisplayObject, registration:Point, x:Number = 0, y:Number = 0):void 
       { 
         //generate the location of the registration point in the parent 
         registration = target.localToGlobal(registration); 
         registration = target.parent.globalToLocal(registration); 

         //move the target and offset by the registration point 
         target.x += x - registration.x; 
         target.y += y - registration.y; 
       } 

       /** 
       * Rotates a <code>DisplayObject</code> based on a registration point. 
       * 
       * @param  target       the DisplayObject to move 
       * @param  registration   the registration point of the DisplayObject 
       * @param  rotation      the new rotation angle 
       */ 
       public static function rotate(target:DisplayObject, registration:Point, degrees:Number = 0):void 
       { 
         changePropertyOnRegistrationPoint(target, registration, "rotation", degrees); 
       } 

       /** 
       * Scales a <code>DisplayObject</code> based on a registration point. 
       * 
       * @param  target       the DisplayObject to move 
       * @param  registration   the registration point of the DisplayObject 
       * @param  scaleX       the new x scaling factor 
       * @param  scaleY       the new y scaling factor 
       */ 
       public static function scale(target:DisplayObject, registration:Point, scaleX:Number = 0, scaleY:Number = 0):void 
       { 
         changePropertyOnRegistrationPoint(target, registration, "scaleX", scaleX); 
         changePropertyOnRegistrationPoint(target, registration, "scaleY", scaleY); 
       } 

       /** 
       * @private 
       * Alters an arbitary property based on the registration point. 
       * 
       * @param  target       the DisplayObject to move 
       * @param  registration   the registration point of the DisplayObject 
       * @param  propertyName   the property to change 
       * @param  value       the new value of the property to change 
       */ 
       private static function changePropertyOnRegistrationPoint(target:DisplayObject, registration:Point, propertyName:String, value:Number):void 
       { 
         //generate the location of the registration point in the parent 
         var a:Point = registration.clone(); 
         a = target.localToGlobal(a); 
         a = target.parent.globalToLocal(a); 

         target[propertyName] = value; 

         //after the property change, regenerate the location of the registration 
         //point in the parent 
         var b:Point = registration.clone(); 
         b = target.localToGlobal(b); 
         b = target.parent.globalToLocal(b); 

         //move the target based on the difference to make it appear the change 
         //happened based on the registration point 
         target.x -= b.x - a.x; 
         target.y -= b.y - a.y; 
       } 

     } 
} 
0

試試這個?

stage.addEventListener("mouseMove", eyesFollow); 
var cw:Number = man_walking.width/2, 
      ch:Number = man_walking.height/2; 

function eyesFollow(e:MouseEvent):void { 
    var a = mouseY - man_walking.y; 
    var b = mouseX - man_walking.x; 
    var radians = Math.atan2(a,b); 
    var degrees = ((180/Math.PI)*radians)+90; 
    var A:Point=man_walking.parent.globalToLocal(man_walking.localToGlobal(new Point(cw,ch))); 
    man_walking.rotation = (degrees); 
    var B:Point=man_walking.parent.globalToLocal(man_walking.localToGlobal(new Point(cw,ch))); 
    man_walking.y+=A.y - B.y; 
    man_walking.x+=A.x - B.x; 
}