2
package  {
    import flash.display.MovieClip;
    import flash.events.Event;
    public class Bullet extends MovieClip {
        private var mc:MovieClip;
        public function Bullet() {

            mc = new MovieClip();
            mc.graphics.beginFill(0);
            mc.graphics.drawRect(120, 120, 40, 40);
            mc.graphics.endFill();
            addChild(mc);
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }

        private function onEnterFrame(e:Event):void{

            mc.rotation += 10;

        }

    }

}

我怎样才能在不让他在舞台上移动的情况下进行圆圈的旋转,只是停留在他之前的位置并旋转,而不是在任何地方移动是可能的?

如果您尝试此代码,您会看到圆圈在舞台上旋转和移动,所以我不想要,我该如何更改?

4

2 回答 2

1

通过围绕要旋转的对象中心的注册点旋转。

Yahoo Astra有一个动态注册类来处理这样的事情。

这可以实现为:

package
{
    import com.yahoo.astra.utils.DynamicRegistration;

    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.geom.Point;

    public class Bullet extends MovieClip
    {
        private var mc:MovieClip;
        private var currentRotation:uint = 0;

        public function Bullet()
        {
            mc = new MovieClip();
            mc.graphics.beginFill(0);
            mc.graphics.drawRect(120, 120, 40, 40);
            mc.graphics.endFill();
            addChild(mc);
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }

        private function onEnterFrame(e:Event):void
        {
            currentRotation = (currentRotation + 10) % 360;
            DynamicRegistration.rotate(mc, new Point(140, 140), currentRotation);
        }

    }
}

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-07-05T02:24:51.423 回答
1

默认情况下,注册点(或对象旋转的点)设置为(0, 0)即显示对象的左上角。

当你这样做

mc.graphics.drawRect(120, 120, 40, 40);

绘制矩形时,其左上角位于坐标处(120, 120),其中心位于(140, 140)

您可以简单地绘制矩形,使其中心位于(0, 0)并在其父级中移动此影片剪辑(以便最终结果相同)

mc.graphics.drawRect(-20, -20, 40, 40);
mc.x=140;
mc.y=140;

然后,mc.rotation+=10将围绕其中心旋转矩形。

于 2012-07-05T05:03:48.600 回答