2 Replies Latest reply on Mar 8, 2012 8:04 AM by Babeteflonblue

    How to limit the number of sprite being dragged?


      Help needed..


      I have a sample of coding here, can anyone tell me how do i set the limit of sprite being dragged? I have set the following script to 28 sprites, but now the problem is, how should i freeze the rest of the 26 sprites and stop it from being draggable after dragging 2 sprites into the specified target location?



      property spriteNum

      property spriteLoc



      on mouseDown me

        set backingSprites = [:]

        sendAllSprites (#IdentifyBackingSprites, backingSprites)

        set dragSpriteNum  = getaProp (backingSprites, #pickAndDrag)

        set orderSpriteNum = getaProp (backingSprites, #changeOrder)

        set dropSpriteNum  = getaProp (backingSprites, #drop)



        if rollover (dragSpriteNum) then

          -- Allow this sprite to be dragged anywhere on stage

          set spriteLoc = the loc of sprite spriteNum

          set delta     = spriteLoc - [the mouseH, the mouseV]

          repeat while the mouseDown

            set the loc of sprite spriteNum = point (the mouseH, the mouseV) + delta


          end repeat

          -- The mouse has been released: where should this sprite go?

          if rollover (dropSpriteNum) then

            -- Drop sprite (and adjust its position)

            set the locV of sprite spriteNum to the bottom of sprite dropSpriteNum


            -- Return sprite to its original position

            set the loc of sprite spriteNum = spriteLoc

          end if


      end mouseDown

        • 1. Re: How to limit the number of sprite being dragged?
          Mister MUX Level 2

          No offense intended, but I am not very fond of that bit code "repeat while the mouseDown...updateStage...". While it may be working for you there are some good reasons not to use that. It also appears that you have not included all of the code in your post to make that work, so it isn't possible to give you specific suggestions on how to modify it. Instead I would like to offer you a different approach. A few assumptions made here (which of course can be changed).


          1) The target is actually another sprite (such as a box) and not a screen location

          2) Once a sprite is dropped into the target, it can not be moved again.

          3) Sprites "snap" to the center of the taget (aligning themselves in a horizontal line).

          4) Sprites dropped anywhere other than the target snap back to their original location.    

          5) Once the maximum number of sprites are dragged into the target, no other sprites can be moved.


          Here are two behaviors, one if for the target sprite, and the other is for all drag sprites. The default is to allow no more than two sprites to be dropped on the target, but that can be easily changed through the behaviors parameters window. Also with this code, there is no specific requirement for the target or sprites to appear in a particular order on the score. You might want to create a simple demo movie and try this out.


          If you have any questions or problems just reply back. HTH


          -- Behavior for all draggable sprites

          property spriteNum, pMe, pStartingLoc, pTargetSpriteNum


          on beginSprite me

            puppetSprite spriteNum, true

            pMe = sprite(spriteNum)  

            pMe.moveableSprite = true

            pMe.cursor = 280

            pMe.pStartingLoc = pMe.loc

            sendAllSprites(#updateDraggableSpriteList, spriteNum)



          on reportToTarget me, targetSpriteNum

            sendSprite(targetSpriteNum, #updateDraggableSpriteList, spriteNum)



          on setTargetSpriteNum me, targetSpriteNum

            pTargetSpriteNum = targetSpriteNum



          on mouseDown me

            if pMe.moveableSprite then pMe.locZ = the lastChannel + 1



          on mouseUp me

            if pMe.moveableSprite then 

              pMe.locZ = spriteNum

              sendSprite(pTargetSpriteNum, #spriteDropped, spriteNum)

            end if



          on setMovable me, moveableState

            pMe.moveableSprite = moveableState

            if pMe.moveableSprite then 

              pMe.cursor = 280


              pMe.cursor = 0

            end if



          on goBack me

            pMe.loc = pStartingLoc




          -- Behavior for the target sprite

          property spriteNum, pMe, pDraggableSpriteList, pDroppedSprites, pMaxDroppedSprites, pCombinedWidth


          on beginSprite me

            pMe = sprite(spriteNum)  

            pDraggableSpriteList = []

            pDroppedSprites = []

            pCombinedWidth = 0

            sendAllSprites(#reportToTarget, spriteNum)



          on updateDraggableSpriteList me, draggableSpriteNum

            if not pDraggableSpriteList.findPos(draggableSpriteNum) then 


              sendSprite(draggableSpriteNum, #setTargetSpriteNum, spriteNum)

            end if



          on spriteDropped me, spriteNumDropped

            if sprite(spriteNumDropped).intersects(pMe) then


              pCombinedWidth = pCombinedWidth + sprite(spriteNumDropped).width

              sendSprite(spriteNumDropped, #setMovable, false)

              sprite(spriteNumDropped).locZ = pMe.locZ + 1

              newLocH = ((pMe.width - pCombinedWidth) * .5) + pMe.left + (sprite(spriteNumDropped).width * .5)

              repeat with  droppedSprite in pDroppedSprites

                sprite(droppedSprite).locH = newLocH

                newLocH = newLocH + sprite(spriteNumDropped).width

                sprite(droppedSprite).locV = pMe.locV

              end repeat

              if pDroppedSprites.count = pMaxDroppedSprites then

                repeat with draggableSpriteNum in pDraggableSpriteList

                  sendSprite(draggableSpriteNum, #setMovable, false)

                end repeat

              end if


              sendSprite(spriteNumDropped, #goBack)

            end if



          on getPropertyDescriptionList

            description = [:]

            addProp description,#pMaxDroppedSprites, [#default:2, #format:#integer, #comment:"Maximum number of sprites that can be dropped on the target"]

            return description


          1 person found this helpful
          • 2. Re: How to limit the number of sprite being dragged?
            Babeteflonblue Level 1

            Thx a lot for yr help...It is better than what i had set to the previous one with few thousand lines of if else on every individual items...It sounds crazy, but it is my first time encounter and I'm have not come accross lingo script :-( And sad to say i need it for my project, it is kinda rush for me as i need to complete it within 6 weeks.


            Do you mind if i post you another question about text input? I'm trying to have a simple BMR calculator in my director, and i have no idea how am I suppose to get the value from the text input sprite in order to perform the calculation and generate the result to appear in the intended text message box? Please refer to http://www.womenandweight.com/weight-management/weight-loss/how-many-calories-per-day/ for the calculation formula.


            Full example of similar version of BMR calculator that i wish to create is like http://www.hscripts.com/scripts/JavaScript/calorie-calculator.php