spriteGroup

  how to moving sprites together as a group and an example of inter-Behavior communication
sprites BehaviorTalk




  grouping sprites
>I have a kind of filmstrip with different pictures after eachother. I
>want them to remain on there relative possitions and when you move the
>mouse over on of the pictures they should all move up or down, depending
>on how you move the mouse.
>
>Can somebody give me some hint in the right direction with this?
>I suppose i should make a behavior and attach it to each sprite, but how
>do i send a mousemessage to all of them, so they can move together?
>
>Gratefull for your help
>
>K.
>
>

With the behavior below you can have one or several named groups of sprites 
which each move as if they were just one single sprite if dragged with the 
mouse (and set to moveable in the score) or just one sprite of the group is 
moved by lingo.
I took it from a context where the group would communicate with vertical and 
horizontal scrollers according to its rect so there may be some code left that 
is not actually needed here.

Hope it helps!


-------------------------------------------------------------
property myInited,myLOurSprites,myMagicSpell,mySprite, myLastLoc

-------
on getPropertyDescriptionList me
  set theProps to [:]
  
  set c to "Please name the group this sprite belongs to"
  set f to #symbol  
  set d to #aGroup
  set typeProps to [#comment: c, #format: f, #default: d]
  addProp theProps, #myMagicSpell, typeProps
  
  return theProps
end


on getBehaviorDescription me
  set line1 to "   This behavior makes a group of sprites move together if one of them is moved." & RETURN  
  return line1
end

----
on beginSprite me
  -- init vars 
  set mySprite = the spritenum of me
  set myLOurSprites = []
  if voidP (myMagicSpell) then set myMagicSpell = #wonko 
end

on enterframe me
  
  -- Init not done?
  if not (myInited) then 
    sendallSprites (#initialize,myLOurSprites,myMagicSpell)
    tellGroupOurSprites me
  end if
  
  -- things to be done once per group and frame
  -- the last one now will be later the first
  if getLast(myLOurSprites)= mySprite then
    -- do things for the group    
  end if
end

on exitFrame me
  set myLoc = getLoc (me)
  set myVec = myLastLoc - myLoc
  if myVec = point(0,0) then return
  set myLastLoc = myLoc
  tellGroupMove me,myVec
end

on initialize me,dL,dSpell
  if myMagicSpell <> dSpell then return
  set myInited = TRUE
  append dL,mySprite
  
  set myLastLoc = getLoc (me)
end

on setLOurSprites me,dL
  set myLOursprites = dL  
end

--------------------------------------------
on tellGroupOurSprites
  repeat with dSprite in myLOurSprites
    if dSprite = mySprite then next repeat
    sendSprite dsprite,#setLOurSprites,myLOurSprites
  end repeat
end

on tellGroupMove me,dVec
  repeat with dSprite in myLOurSprites
    if dSprite = mySprite then next repeat
    sendSprite dsprite,#moveSprite,dVec
  end repeat
end

on tellGroupJoinRects me,dRect
  repeat with dSprite in myLOurSprites
    if dSprite = mySprite then next repeat
    set dREct = sendSprite (dsprite,#JoinRects,dRect)
  end repeat
  return dRect
end

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

on joinRects me,dRect
  return union ( dRect, the rect of sprite mySprite)
end

on moveSprite me,dVec
  set dLoc = getLoc(me)-dVec 
  setLoc me, dLoc
  set myLastLoc = dLoc
end


on getLoc me,dSprite
  if voidP(dSprite) then set dSprite = mySprite
  return the loc of sprite dSprite
end

on setLoc me,dLoc
  set the loc of sprite mySprite to dLoc
end

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

Home shock + cgi Bits 'n pieces Director Lingo Shocklets Contact










pageID=l_spriteGroup