Articles Archive
Articles Search
Director Wiki
 

Collecting information from sprites

January 29, 2001
by Will Turnage

Dear Multimedia Handyman,

I'm creating a game where there are several floating blockades on the screen. The user can control the number of blockades to be anywhere from 5 to 50. Is there an easy way that I can get the locations of all the floating blockades? Or better yet, an easy way to get the locations of a select group of blockades?

Thanks,
Robb

Dear Robb:

There are many different ways to get information about groups of sprites. One of the most basic and common ways involves having a script that just searches through every sprite on the screen and finds out information about it. The biggest drawback to this method is that it makes it difficult to get information from a specific group of sprites. It seems that anytime you want to send a message to a certain group, you have to add if statements, or hard code sprite numbers into your code which can be quite cumbersome.

Luckily, there is a simpler way to collect data from sprites using a list and the sendAllSprites command. It starts by creating a behavior and attaching it to a button. In this behavior, you create an empty list, then send the empty list to every sprite using sendAllSprites. The behaviors that receive the command will add information to list. Then, after every sprite has added information to the list, the original handler can use the information in that list to do whatever it wants.

Confused yet? Let's look at this sample movie to help explain.

There is a behavior attached to the button labeled find the location of all dots. The code for that behavior looks like this.

on mouseUp me
  tempLocList = [:]
  sendAllSprites (#returnMyLocation, tempLocList)
  put tempLocList
end

When you click the button, it creates an empty property list. Next, it sends the message returnMyLocation to every sprite on the screen, with the empty property list as a parameter of the handler. On each of the dots on screen, there is a behavior attached which contains the following code.

on returnMyLocation me, locList
  locList.addprop (sprite (me.spriteNum).member.name, sprite (me.spriteNum).loc)
end

This handler just adds a new element to the property list. The property added contains the name of the sprite's member, and the new value added to the list is the sprite's location. So, after every sprite has processed the returnMyLocation message, then the original button can take tempLocList and do anything with it.

But what if you wanted to collect information from a specific group of sprites? That would be pretty simple too. We could just modify our previous example, to look like this:

on mouseUp me
  tempLocList = [:]
  sendAllSprites (#returnMyLocation, tempLocList, "green")
  put tempLocList
end

on returnMyLocation me, locList, dotName
   if dotName = sprite (me.spriteNum).member.name then
     locList.addprop (sprite (me.spriteNum).member.name, sprite (me.spriteNum).loc)
   end if
end

In this example, you are sending a second parameter containing a string value. The behavior on the dot checks the string to see if it is the same as the sprite member'sname (in this case 'green'). If they match, then it will add its information to the list.

Don't think that you can use this style of coding with just any variable, though, because it only works with lists. If you try sending an integer or string to all sprites and have them modify it, then it won't work. However, if you put that integer or character inside a list, and then send it to all sprites, then it will work just fine.

The possibilities here are endless. You can use this method to count the number of sprites in a particular group that are on screen, or you can even use it to send information to a select number of sprites. Have fun coding...

A sample Director 7 movie is available for download in Mac or Windows format.

Will Turnage is a multimedia programmer based in New York City. In addition to his weekly role as Director Online's Multimedia Handyman, he is also the Technology Director for Sony Music's Client Side Technologies Group. You can read more about his work at http://will.turnage.com/.

Copyright 1997-2024, Director Online. Article content copyright by respective authors.