Grouping Sprites

Often, it is helpful to have sprites group themselves together. A common example of this is using 'radio buttons' - if one button in the group is selected, then the others should be de-selected.

Consider the following example in which there are two groups of 3 radio buttons:

Each of the radio button sprites has the same behaviour attached. This behaviour has two parameters which can be assigned: (1) What group the button belongs to, and (2) whether or not the button should default to a selected state.

When the behaviour is instantiated and receives the 'beginSprite' message, it sends out a ''CollectRadioGroup' message, passing as a parameter the groupID of that button and an empty list. All the radioButtons, including the one that sent the message, respond to that message by checking whether they belong to that group. If they do belong to that group, they do two things: (1) they add themselves to the list of group members, and (2) they keep a reference to that list. This way, all the members of the group have access to a shared list of group members. This list is used to send messages to the group using Lingo's "Call" function.

property myGroupID

property myGroupList

property myState



on beginsprite me

  

  -- make sure the hilite is correctly set

  sprite(me.spriteNum).member.hilite = myState

  

  -- now collect other members of the group

  sendAllSprites(#CollectRadioGroup, [], myGroupID)

  

  -- if this button is selected, make sure the others are not

  if myState then call(#GroupSelect, myGroupList, me)

  

end



on endSprite (me)

  

  -- Delete the reference to this object from the groupList

  myGroupList.deleteOne(me)

  

end



on CollectRadioGroup (me, groupList, groupID)

  

  -- Create a references to the groupList, and add self to it

  if myGroupID = groupID then

    myGroupList = groupList

    myGroupList.add(me)

  end if

  

end



on mouseUp (me)

  

  -- inform the group that this button is clicked

  call(#GroupSelect, myGroupList, me)

  

end





on GroupSelect (me, whoIsSelected)

  

  -- a member of the group has been selected. If the selected

  -- object is this object, then hilight the button. Otherwise,

  -- turn the hilight off.

  if whoIsSelected = me then myState = 1

  else myState = 0

  sprite(me.spriteNum).member.hilite = myState

  

end





on GetPropertyDescriptionList (me)

  

  pdList = [:]

  pdList[#myGroupID] = [#Comment: "GroupID", #default: #RadioGroup, \

#Format: #symbol]

  pdList[#myState] = [#Comment: "Default State", #default: 1, \

#Format: #boolean]

  return pdList

  

end

Source Movie is here

First published 07/06/2005