【问题标题】:Having some positioning trouble with springs in as3as3中的弹簧有一些定位问题
【发布时间】:2012-11-19 10:33:58
【问题描述】:

一段时间以来,我一直在尝试破解 Photonstorm 的用于 Flixel 的 Power Tools 'Spring' 插件。我已经涉足了一些公式,但直到现在我还没有得到我想要的东西。

我已经设法将它破解到插件中: http://illogictree.com/blog/2010/01/1345/

这使得示例现在看起来像这样:

哪个好。它的行为与我希望它的感觉非常相似。除了一个问题。我希望绳子上的球直接位于鼠标下方。关于如何做到这一点的任何想法?

在你问之前,代码: //插件破解

 package org.flixel.plugin.photonstorm.BaseTypes 
    {
    import org.flixel.*;
    import org.flixel.plugin.photonstorm.FlxExtendedSprite;

    public class MouseSpring 
    {
        public var sprite:FlxExtendedSprite;

        /**
         * The tension of the spring, smaller numbers create springs closer to the mouse pointer
         * @default 0.1
         */
        public var tension:Number = 0.1;

        /**
         * The friction applied to the spring as it moves
         * @default 0.95
         */
        public var friction:Number = 0.95;

        /**
         * The gravity controls how far "down" the spring hangs (use a negative value for it to hang up!)
         * @default 0
         */
        public var gravity:Number = 0;

        private var retainVelocity:Boolean = false;

        private var vx:Number = 0;
        private var vy:Number = 0;

        private var dx:Number = 0;
        private var dy:Number = 0;

        private var ax:Number = 0;
        private var ay:Number = 0;

        /**
         * Adds a spring between the mouse and a Sprite.
         * 
         * @param   sprite              The FlxExtendedSprite to which this spring is attached
         * @param   retainVelocity      true to retain the velocity of the spring when the mouse is released, or false to clear it
         * @param   tension             The tension of the spring, smaller numbers create springs closer to the mouse pointer
         * @param   friction            The friction applied to the spring as it moves
         * @param   gravity             The gravity controls how far "down" the spring hangs (use a negative value for it to hang up!)
         */
        public function MouseSpring(sprite:FlxExtendedSprite, retainVelocity:Boolean = false, tension:Number = 0.1, friction:Number = 0.95, gravity:Number = 0)
        {
            this.sprite = sprite;
            this.retainVelocity = retainVelocity;
            this.tension = tension; //bounciness
            this.friction = friction; //how tight
            this.gravity = gravity; //mass
        }

        /**
         * Updates the spring physics and repositions the sprite
         */
        public function update():void
        {
            dx = FlxG.mouse.x - sprite.springX;
            dy = FlxG.mouse.y - sprite.springY;

            ax = dx * tension;
            ay = dy * tension;

            //ax = Math.cos(dx * tension);
            //ay = Math.sin(dy * tension);

            vx += ax;
            vy += ay;

            //vy += gravity;
            //vx *= friction;
            //vy *= friction;

            //This is where I've hacked it in:
            vx = ax + ( 1.0 / gravity ) * (-friction * ax - tension * ( FlxG.mouse.x - dx  ) + sprite.springX * gravity ) * FlxG.elapsed;
            vy= ay + ( 1.0 / gravity ) * ( -friction * ay - tension * ( FlxG.mouse.y - dy ) + sprite.springY * gravity ) * FlxG.elapsed;            

            sprite.x += vx;
            sprite.y += vy;
        }

        /**
         * Resets the internal spring physics
         */
        public function reset():void
        {
            vx = 0;
            vy = 0;

            dx = 0;
            dy = 0;

            ax = 0;
            ay = 0;
        }       
         }
       }

将对象应用于状态:

        ball1 = new FlxExtendedSprite(160, 120, AssetsRegistry.redPNG);
        //Argument 1:onPressed: true if the spring should only be active     when the mouse is pressed down on this sprite
        //Argument 2:retainVelocity: true to retain the velocity of the     spring when the mouse is released, or false to clear it
        //Argument 3:tension: The tension of the spring, smaller numbers     create springs closer to the mouse pointer
        //Argument 4:friction: The friction applied to the spring as it     moves
        //Argument 5:gravity: The gravity controls how far "down" the      spring hangs (use a negative value for it to hang up!)
        ball1.enableMouseSpring(false, false, 0.1, 0.95, -5);
        ball1.springOffsetX = 0;
        ball1.springOffsetY = 0;

此外,松开弹簧使其更像绳索或绳索的任何帮助也会有所帮助!

提前致谢!

【问题讨论】:

    标签: actionscript-3 flixel


    【解决方案1】:

    如果您希望它直接位于鼠标下方,请删除所有 x 计算并将其替换为舞台上的 mouseX 参数。弹簧现在只会在 y 维度上移动。

    要调整弹性,请使用以下值:

    this.tension = tension; //bounciness
    this.friction = friction; //how tight
    this.gravity = gravity; //mass
    

    一些组合会让你得到你想要的。

    【讨论】:

    • 嗯,我对这些进行了调整,感觉好多了。不过,我仍然希望获得绳索或绳索的感觉。我看过一些,但那些需要节点和关节以及其他类似的东西。那还有其他方法可以使用吗?
    • Tension 将成为您想要使用的参数,所以看看哪个效果更好 - 高或低。一个人应该更像一根绳子。摩擦是导致追赶鼠标延迟的原因,摩擦越大,效果越多,因此将其向上或向下移动以查看有什么帮助。重力不应该有太大的影响,在你让其他两个工作之前不要管它。
    猜你喜欢
    • 2020-10-09
    • 2012-05-22
    • 1970-01-01
    • 2015-08-05
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多