ImageSprites

=Creating the ImageSprite=

An easy way to display an image in the BoardView of a GameGardens project is to create an ImageSprite to hold this image. Here is one way to create an ImageSprite object, stored as an element of an array named tileSprites:

tileSprites[row][col] = new ImageSprite(_images[MummichogCodes.TILE_BACK]); tileSprites[row][col].setLocation(col * MummichogCodes.TILE_SIZE +        MummichogCodes.LEFT_OFFSET,        row * MummichogCodes.TILE_SIZE + MummichogCodes.TOP_OFFSET); tileSprites[row][col].setRenderOrder(1); addSprite(tileSprites[row][col]);

The ImageSprite constructor just needs to know what image to display. That image can be changed later. After creating the ImageSprite, the location is set with the (x,y) coordinates for the top-left corner of the ImageSprite (not the center, and if your sprites are of differing sizes this will make a difference!).

The render order specifies which ImageSprite goes on top when two or more images overlap. A negative number goes to the back, and a positive number to the front.

=Moving an ImageSprite=

There are several ways to move an ImageSprite object once it is created. The simplest is to just change its location. If we want to have an ImageSprite named markerSprite move directly to position (new-x, new-y), we could use this code:

markerSprite.setLocation(new-x, new-y);

When the line is executed, the image appears to instantly teleport to the new location. Usually we want the image to take a bit of time to smoothly move from one point to another. Here's a way that we can do that:

LinePath lpath = new LinePath(markerSprite.getX, markerSprite.getY,              new-x, new-y, 1000L); markerSprite.move(lpath);

This causes the markerSprite to take about one second (1000L milliseconds) to move in a straight line from wherever it is now to the new position. To use this in your BoardView class you will need to import com.threerings.media.util.LinePath.

If you would like to call a special method only once the sprite has finished its move, you can do so by setting an observer:

markerSprite.addSpriteObserver(   new PathAdapter     {      public void pathCompleted(Sprite sprite, Path path, long when)       {        calledAfterMove;      }    });

The method calledAfterMove will only be called when the sprite has arrived to its final position.

=Changing the Image=

Here is an example from Mummichog, of changing the image displayed by an ImageSprite from the back to the front:

tileSprites[row][col].setMirage(_images[_myobj.board.getImageCode(row, col)]);

If we assume a BufferedMirage variable newImage holds the image we want to change markerSprite to show, then we could use code similar to this:

markerSprite.setMirage(newImage);