簡體   English   中英

如何在動作腳本的中心旋轉精靈

[英]How to rotate a sprite in actionscript on its center

好吧,我已經讀了很多關於它的東西,並且它變得重復了,但是沒有一個能解決我的問題或者它太難理解了。 我試圖讓一個小精靈從上向下看,盯着鼠標,無論它在頁面上到哪里。 但是,他沒有旋轉頭部,而是在符號的中心,而是在精靈的左上角旋轉。 有什么幫助嗎?

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

注意:最適合我的一個帖子的解決方案只是一個斷開的鏈接,所以我無法訪問它。

假設子畫面的(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 ),旋轉它,然后將其移回。

我希望這有幫助! :)

在Flash Pro中,您可以簡單地定位符號,以使0,0坐標成為您希望旋轉的點:

注冊點

雅虎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;
                }

        }
}

嘗試這個?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM