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);
}

注意:最适合我的一个帖子有一个解决方案,它只是一个断开的链接,所以我无法访问它。

4

3 回答 3

0

在 Flash Pro 中,您可以简单地定位符号,使0,0坐标是您希望旋转的点:

注册点

Yahoo Astra有一个动态注册类,用于围绕一个点旋转。

这可以实现如下,围绕100,100旋转:

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

Yahoo Astra 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;
                }

        }
}
于 2012-11-05T05:10:57.210 回答
0

假设精灵的 (x, y) 位置在左上角,高级 Lua-ish 伪代码(抱歉,我不太熟悉 Actionscript)中的一般过程将是:

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

如果您的图书馆使用不同的计算位置和原点的方法,您需要调整需要翻译的数量,但要点仍然存在:首先翻译图像,使其中间位于 (0,0 ),旋转它,然后将其移回。

我希望这有帮助!:)

于 2012-11-05T04:59:09.097 回答
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;
}
于 2012-11-05T07:59:55.473 回答