2012-12-01 29 views
1

有段时间我一直在试图破解Flixel的Photonstorm的Power Tools'Spring'插件。我已经涉足了几个公式,但是我到现在还没有得到我想要的。在as3中弹簧存在一些定位问题

我已经设法攻入插件这样的: http://illogictree.com/blog/2010/01/1345/

这使得例子来看一下现在这个样子: hackedtool

这是很好的。它的行为非常符合我希望的感觉。除了一个问题。我想要将字符串上的球直接放在鼠标下面。任何想法如何做到这一点?

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

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; 

此外,对于松动紧弹簧,使其任何帮助更多的绳子或绳子也会有帮助!

在此先感谢!

回答

1

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

要调整的弹性,与这些值玩法:

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

一些组合会得到你在找什么。

+0

好吧,我调整了这些,感觉好一点。尽管如此,我仍然想要一根绳子或绳子。我已经看了一些,但那些需要节点和关节以及其他类似的东西。那么还有其他方法可以使用吗? – xhunterko

+0

张力将成为你想要使用的参数,所以看看什么效果更好 - 高或低。人们应该更像绳子。摩擦是导致追赶鼠标的延迟的原因,摩擦越多,效果越好,所以稍微倾斜一下,看看有什么帮助。引力不应该有太大的变化,只有在其他两个人工作的时候才能保持独立。 – Gone3d

+0

好吧。再次感谢。 – xhunterko