Lingo Tutorial: Sprite Pool

File "naomis_colorforms.dir"

In this application you have a "pool" of items at the bottom of the screen (eyes, ears, noses, lips, moustaches). They are on channels 1 through 4. Having a pool means that you want to be able to generate dynamically several (up to 10 copies in this application) of these pool items in any combination (8 eyes and 2 noses, or 1 eye, 3 ears and 7 moustaches, for example.)

There is also an easy - though clumsy - way to implement the pool effect by having a bunch of sprites perfectly aligned on each other. You do not want to have only 2 items of each group - that would be limiting choices. You do not want to have as many as 10 of each, that would be a waste of resources.

How do you implement this "sprite pool" behavior?

One way is to have a pool 10 generic items that you can take and place on the stage on demand. That is really cool, and not difficult to implement: There is a bunch of hidden sprites (phantoms) on the right-hand-side of the screen. (Of course, they could be completely off stage and no one would know they existed.) They appear on channels 10 to 19. nextIcon keeps track of the next available phantom sprite.

 
on startMovie
  global nextIcon, maxIcons
  nextIcon = 10 -- 10 phantom shapes are in channels 10 to 19
  maxIcons = 19
end

Every time you click on a pool item, a hidden sprite comes and sticks itself under your cursor using the command

sprite(nextIcon).member = _mouse.mouseMember

Recall that mouseMember returns the cast member of the sprite that the mouse clicked on.
Once generated, you want them to move them around and place them somewhere on the screen. You can then move them around by having them follow the location of the mouse while the mouse is still down:

 
  repeat while _mouse.stillDown
    sprite(nextIcon).loc = _mouse.mouseLoc
    updateStage
  end repeat
      

Here is the whole script that produces a new item:

 
on produceChild  -- give a new image from the pool of images
  global nextIcon, maxIcons
  -- mouseMember gives you the cast member that is currently under the mouse
  sprite(nextIcon).member = _mouse.mouseMember
  
  if nextIcon > maxIcons then -- we only have no more icons
    alert "Sorry, I ran out of ink!"
  end if
  
  repeat while the stillDown
    sprite(nextIcon).loc = _mouse.mouseLoc
    updateStage
  end repeat
  
  nextIcon = nextIcon + 1
end
      
You may also want them to be repositioned later on. This is accomplished by simply having the sprites have the moveableSprite property true.

 

Maintained By: Takis Metaxas
Last Modified: November 9, 2009