2 Replies Latest reply on Oct 25, 2006 7:51 AM by Newsgroup_User

    'elastic' dragging

    Level 7
      Hi all,

      I have a few images on the stage that i want the user to be able to drag,
      but when the let go, the image 'twangs' back to its original location as if
      it is attached by a piece of elastic!!
      At the moment I have a behavoir script which makes the image simply slide
      back to its original position with an ease in and ease out curve (see
      below). What I would like it to do is slightly over shoot, then spring
      back, over shooting again even less untill it stops. (like I said, its
      attached by elastic and springs to rest)
      Anyone know how to do this or point me in the right direction?

      What I have so far is......

      --------------------------------------------------------------------------
      property oldH, newH
      property oldV, newV
      property dragTrig, retSpeed
      property sp
      property p,oldp

      global zlevel

      ------------------------
      on beginSprite me
      sp = sprite(me.spritenum)
      oldH = sp.locH
      oldV = sp.locV
      dragTrig = 0
      retSpeed = 50
      p = 0
      end


      ------------------------
      on mouseDown me
      sp.locZ = zlevel
      zlevel = zlevel + 1
      dragTrig = 0
      p = 0
      oldp = 0
      end


      ------------------------
      on mouseUp me
      startTimer
      newH = sp.locH
      newV = sp.locV
      dragTrig = 1
      end


      ------------------------
      on exitFrame me

      if dragTrig = 1 then
      sp.loch = p * (oldH - newH) + newH
      sp.locv = p * (oldV - newV) + newV

      --p = p + .050
      p = eTime(retSpeed)
      p = (1 - cos(p * pi)) / 2.0

      if oldp > p then
      sp.loch = oldH
      sp.locv = oldV
      p = 0
      dragTrig = 0
      else
      oldp = p
      end if

      end if

      end


      -----------------------
      on eTime(max)
      return (the timer mod max) / float(max)
      end

      ---------------------------------------------------------------------

      (thought it might be usefull for someone as is anyway!)


      Any help appreciated.


      Tim


        • 1. Re: 'elastic' dragging
          duckets Level 1

          How about something like this? (attached below)

          enjoy!

          - Ben
          • 2. Re: 'elastic' dragging
            Level 7
            Now that is bang on the nail!

            Fantastic!!

            Cheers Ben :)


            "duckets" <webforumsuser@macromedia.com> wrote in message
            news:ehnq1r$dim$1@forums.macromedia.com...
            > How about something like this? (attached below)
            >
            > enjoy!
            >
            > - Ben
            >
            >
            > --------------------------------------------------------------------------
            > property oldH,oldV
            > property h,v,hm,vm
            > property sp
            > property mode,dragOffset
            > property springDampen,springStiffness
            >
            > ------------------------
            > on beginSprite me
            > sp = sprite(me.spritenum)
            > oldH = sp.locH
            > oldV = sp.locV
            >
            > hm = 0
            > vm = 0
            > h = 0
            > v = 0
            >
            > mode = #stopped
            >
            > springStiffness = 0.1 -- (lower = softer spring)
            > springDampen = 0.9 -- (lower = more dampening)
            >
            > end
            >
            >
            > ------------------------
            > on mouseDown me
            > mode = #dragging
            > dragOffset = the mouseloc-sp.loc
            > end
            >
            >
            > ------------------------
            > on exitFrame me
            >
            > case mode of
            > #dragging:
            >
            > sp.loc = the mouseLoc - dragOffset
            > if not the mouseDown then
            > dragging = false
            > h = sp.loch
            > v = sp.locv
            > hm = 0
            > vm = 0
            > mode = #springing
            > end if
            >
            > #springing:
            >
            > hm = hm + (oldH-h)*springStiffness
            > vm = vm + (oldV-v)*springStiffness
            >
            > h = h + hm
            > v = v + vm
            >
            > hm = hm * springDampen
            > vm = vm * springDampen
            >
            > sp.loc = point(h,v)
            >
            > if integer(h)=oldH and integer(v)=oldV and abs(hm)<0.1 and
            > abs(vm)<0.1
            > then
            > mode = #stopped
            > end if
            >
            > end case
            >
            > end
            >