Flash CS4 AS3 水平影片剪辑随鼠标移动滚动

发布于 2024-07-25 01:16:11 字数 520 浏览 6 评论 0原文

我是 AS3 的新手,一直致力于用 AS3 编写的 XML 驱动的导航系统。

目前,我已导入 XML 文件的内容,并将其绘制在舞台上动态创建的根级别包含的 MovieClip 中。 这个MovieClip被称为“容器”。

我想要实现的是平滑的加速/减速效果,根据鼠标光标相对于舞台中间的位置,沿 X 轴对容器影片剪辑进行动画处理。

我的代码可以在这里找到:http://pastie.org/521432

行87 以后是我现在使用的代码,用于使影片剪辑向左滚动和向右滚动。 正确的。

我所拥有的确实有效,但很笨重,但确实有效——我只是希望它更加精致一点,并且在谷歌上留下了空白。 因为我希望即使鼠标停止移动,MovieClip 也能继续以当前相对速度滚动,所以我使用了 Timer 类的实例。

有人可以提出改进建议吗? 提前致谢。

I'm new to AS3 and have been working on an XML driven navigation system written in AS3.

At present, I've imported the contents of an XML file and plotted it inside a containing MovieClip created at root level dynamically on the stage. This MovieClip is called 'container'.

What I want to accomplish is a smooth, accelerating / decelerating effect which animates the container movieclip along the X axis depending on where the mouse cursor is in relation to the middle of the stage.

My code can be found here: http://pastie.org/521432

Line 87 onwards is the code I'm using right now to make the movieclip scroll left & right.

What I have does work but is clunky but does work - I just want it to be a little more polished and have drawn a blank with Google. Because I want the MovieClip to continue to scroll at the current relative speed even when the mouse stops moving, I used an instance of the Timer class.

Can anyone suggest improvements? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猥︴琐丶欲为 2024-08-01 01:16:11

您应该将计算和绘图方法分开。 因此,让它在 onMouseMove 处理程序中执行所有计算,但实际上在 onEnterFrame 处理程序中绘制更改。

另外我认为你的算法可以更简单,没有人会注意到。 我举了一个简单的例子来说明你可以如何做到这一点。 将此代码粘贴到名为 Main.as 的 AS3 文件中,并使其成为新 FLA 的文档类

package 
{
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class Main extends Sprite 
    {
        private const boxCount:int = 10;
        private const boxWidth:int = 45;
        private const boxMargin:int = 5;
        private const startPoint:int = 150;
        private const boxesWidth:int = boxCount * (boxWidth + boxMargin);
        private const endPoint:int = boxesWidth + startPoint;
        private const zeroPoint:int = boxesWidth / 2 + startPoint;

        private var container:MovieClip;
        private var targetX:Number;
        private var speed:Number = 0;

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            container = new MovieClip();
            addChild(container);
            container.x = 150;
            container.y = 300;
            for (var i:int = 0; i < boxCount; i++) 
            {
                container.graphics.beginFill(Math.random() * 0xFFFFFF);
                container.graphics.drawRect(i*(boxWidth+boxMargin), 0, boxWidth, boxWidth);
            }

            addEventListener(Event.ENTER_FRAME, enterFrameHandler);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
        }

        private function mouseMoveHandler(e:MouseEvent):void 
        {
            var distanceFromCenter:int = stage.mouseX - zeroPoint;
            speed = distanceFromCenter * -0.01; // Bring number into a good range, and invert it.
        }

        private function enterFrameHandler(e:Event):void 
        {
            container.x += speed;
        }
    }
}

You should separate out you calculations and your drawing methods. So have it do all the calculations in an onMouseMove handler, but actually draw the changes in an onEnterFrame handler.

Also I think your algorithm could be much simpler and nobody would notice. I made a quick example of how you might do it. paste this code into an AS3 file called Main.as and make it the document class of a new FLA.

package 
{
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class Main extends Sprite 
    {
        private const boxCount:int = 10;
        private const boxWidth:int = 45;
        private const boxMargin:int = 5;
        private const startPoint:int = 150;
        private const boxesWidth:int = boxCount * (boxWidth + boxMargin);
        private const endPoint:int = boxesWidth + startPoint;
        private const zeroPoint:int = boxesWidth / 2 + startPoint;

        private var container:MovieClip;
        private var targetX:Number;
        private var speed:Number = 0;

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            container = new MovieClip();
            addChild(container);
            container.x = 150;
            container.y = 300;
            for (var i:int = 0; i < boxCount; i++) 
            {
                container.graphics.beginFill(Math.random() * 0xFFFFFF);
                container.graphics.drawRect(i*(boxWidth+boxMargin), 0, boxWidth, boxWidth);
            }

            addEventListener(Event.ENTER_FRAME, enterFrameHandler);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
        }

        private function mouseMoveHandler(e:MouseEvent):void 
        {
            var distanceFromCenter:int = stage.mouseX - zeroPoint;
            speed = distanceFromCenter * -0.01; // Bring number into a good range, and invert it.
        }

        private function enterFrameHandler(e:Event):void 
        {
            container.x += speed;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文