1 Reply Latest reply on May 9, 2013 6:42 PM by CopyPixels

    drag three or more sprites together

    anjemalo Level 1


      Please, can you tell me how to drag three or more sprites together it does not care with sprite you drag?

      have you any sample?

      many thanks to all

        • 1. Re: drag three or more sprites together

          The simple answer is to keep a list of the sprites and to use the "call()" handler to tell the group to do something.


          The actual implementation can take many forms. The approach I would use requires several scripts, but that is too complicated to explain. So, I took the code that I posted for you that lets you drag one sprite horizontally or vertically and added some code from a Radio Button script that does grouping, and created a behavior that I call "Collective Linear Drag". 


          The behavior works by assigning a group name to each set of sprites that you want to move together. You can have more than one group on a screen at one time.


          The behavior is a little different in that it uses static properties and handlers. All that means is that some properties remember their values after the program is stopped - inside the IDE - and some handlers are accessed through the script member.


          Here is all you need to know to use the behavior:


          Use a prepareMove handler to clear the behavior's memory.


          on prepareMovie

            script("Collective Linear Drag").clear() 



          There is a handler that returns the last sprite click for all the current  groups.


          ie  put script("Collective Linear Drag").getLastClicked()


          That's about it. Just paste the code below into a Behavior script and attach it to several sprites with the same group name, and drag.


          -- Collective Linear Drag 


          property  sp  -- this sprite

          property  pDirection  -- the direction to constain dragging,  #Horizontal, #Vertical

          property  pStartLoc  --  sprite location upon mouseDown

          property  pMouseStart  -- mouse location upon mouseDown

          property  pMoving  -- flag for enterFrame. True/False

          property  pGroupName  -- name of the group this sprite is part of



          -- Private Static Property

          property  pRadioGroups  -- property list of radio button group names and associated data



          On GetPropertyDescriptionList me

            Props = [:]

            Props[#pDirection] = [#default:#Horizontal, #format:#symbol, #Range:[#Horizontal, #Vertical], #comment:"Pick direction to drag"]

            Props[#pGroupName] = [#default:#Group01, #format:#symbol,  #comment:"Group Name."]

            return Props

          end GetPropertyDescriptionList



          on beginSprite me

            sp = sprite(me.spriteNum)


            if me.script.pRadioGroups[pGroupName].voidP then  -- need to add a new group name to pRadioGroups

              me.script.pRadioGroups[pGroupName] = [#Sprites:[], #LastClicked:void]

            end if


            me.script.pRadioGroups[pGroupName].Sprites.add(sp)  -- add this sprite to its group.


            pMoving = false

          end beginSprite



          on mouseDown me

            Sprites = me.script.pRadioGroups[pGroupName].Sprites  -- get list of sprites that this sprite belongs to.

            call(#startDrag, Sprites)  --  start dragging all the sprites in this group.

            me.script.pRadioGroups[pGroupName].LastClicked = sp  -- set this sprite as the last one clicked for its group

          end mouseDown



          on startDrag me

            pStartLoc = sp.loc

            pMouseStart = _mouse.mouseLoc

            pMoving = true




          on mouseUp me

            Sprites = me.script.pRadioGroups[pGroupName].Sprites

            call(#endDrag, Sprites)




          on mouseUpoutside me

            Sprites = me.script.pRadioGroups[pGroupName].Sprites

            call(#endDrag, Sprites)




          on endDrag me

            pMoving = false




          on enterFrame me

            if not pMoving then exit


            deltaMouse = _mouse.mouseLoc - pMouseStart


            if pDirection = #Horizontal then

              sp.loc = pStartLoc + point(deltaMouse[1], 0)


              sp.loc = pStartLoc + point(0, deltaMouse[2])

            end if

          end enterFrame



          ---------------  Grouping Code ------------------------------


          on new me,

            if  me.script.pRadioGroups.voidP then  -- no instance has been stored. Stored the one and only instance allowed.

              me.script.pRadioGroups = [:]  -- initialize property

            end if


            return me

          end new



          on clear me

            me.script.pRadioGroups = [:]




          on getLastClicked me  -- Returns a list of last buttons clicked/pressed for each group

            Clicked = [:]


            -- populate "Clicked" with a list of Group Name / Last clicked value pairs

            total = me.script.pRadioGroups.count

            repeat with Cnt = 1 to total

              Clicked[me.script.pRadioGroups.getPropAt(Cnt)] = me.script.pRadioGroups[Cnt].lastclicked

            end repeat


            return Clicked

          end getLastClicked