CommandSprite
From Gardenwiki
A Command sprite is a sprite that may be pressed to generate an ActionEvent that will trigger a method in your Controller, passing an argument. It is a bit more complicated than an ActionSprite due to the need to pass the argument.
Implement the CommandSprite Interface
If you already have a class that extends Sprite, have that class implement the CommandSprite interface. Here is an example of how to do this for an ImageSprite, using a simple integer argument (suitable for the ID of a DSet.Entry, key of a HashTable, or index of an Array).
public class CommandImageSprite extends ImageSprite
implements CommandSprite {
public CommandImageSprite(BufferedMirage image, String action,
int key) {
super(image);
_action = action;
_key = key;
}
public String getActionCommand() {
return _action;
}
public Object getCommandArgument() {
return _key;
}
private String _action;
private int _key;
}
Note that although the key and _key variables are ints, the getCommandArgument method returns an Object. This is important.
Add Action Method to Controller
Inside your Controller class, add the method to be triggered. You can have multiple methods or just a single one. Let's assume that we want to have two different possible action events, one for spools and one for beads. The methods could look like these.
public void spoolClicked(Object source, int spoolID) {
// add lines that you want executed
// if a spool sprite is clicked
}
public void beadClicked(Object source, int beadID) {
// add lines that you want executed
// if a bead sprite is clicked
}
The source Object is(?) your BoardView container, not the sprite that was clicked. Optionally, you may add "handle" in front of the name of the method; it doesn't seem to make any difference.
Use CommandSprite in BoardView
Inside your BoardView class, create one or more instances of your CommandSprite. Here is an example using CommandImageSprite.
BufferedMirage image = ... find the image ..... ;
int key = thisSpool.spoolId;
CommandImageSprite sprite =
new CommandImageSprite(image,
"spoolClicked", key);
Notice that the String being passed matches the name of the method in Controller. After creating the instance CommandImageSprite, treat it exactly the same as a normal Sprite. When a player clicks a non-transparent part of the sprite, the correct method will be triggered in Controller, passing the key for the sprite clicked.

