Skip navigation
Babeteflonblue
Currently Being Moderated

How to limit the number of sprite being dragged?

Feb 26, 2012 7:02 AM

Tags: #lingo #director #sprite #draggable

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

      updateStage

    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

    else

      -- Return sprite to its original position

      set the loc of sprite spriteNum = spriteLoc

    end if

 

end mouseDown

 
Replies
  • Currently Being Moderated
    Mar 6, 2012 9:48 AM   in reply to Babeteflonblue

    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)

    end

     

    on reportToTarget me, targetSpriteNum

      sendSprite(targetSpriteNum, #updateDraggableSpriteList, spriteNum)

    end

     

    on setTargetSpriteNum me, targetSpriteNum

      pTargetSpriteNum = targetSpriteNum

    end

     

    on mouseDown me

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

    end

     

    on mouseUp me

      if pMe.moveableSprite then 

        pMe.locZ = spriteNum

        sendSprite(pTargetSpriteNum, #spriteDropped, spriteNum)

      end if

    end

     

    on setMovable me, moveableState

      pMe.moveableSprite = moveableState

      if pMe.moveableSprite then 

        pMe.cursor = 280

      else

        pMe.cursor = 0

      end if

    end

     

    on goBack me

      pMe.loc = pStartingLoc

    end

     

     

    -- 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)

    end

     

    on updateDraggableSpriteList me, draggableSpriteNum

      if not pDraggableSpriteList.findPos(draggableSpriteNum) then 

        pDraggableSpriteList.add(draggableSpriteNum)

        sendSprite(draggableSpriteNum, #setTargetSpriteNum, spriteNum)

      end if

    end

     

    on spriteDropped me, spriteNumDropped

      if sprite(spriteNumDropped).intersects(pMe) then

        pDroppedSprites.add(spriteNumDropped)

        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

      else

        sendSprite(spriteNumDropped, #goBack)

      end if

    end

     

    on getPropertyDescriptionList

      description = [:]

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

      return description

    end

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points