Java Reference
In-Depth Information
// create image for sprite
Image image = Image.createImage("/example_sprite.png");
// create and position sprite
guy = new Sprite(image, SPRITE_WIDTH, SPRITE_HEIGHT);
guy.setPosition(spritePositionX, spritePositionY);
// paint sprite on the screen
public void paint(Graphics g)
{
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
guy.paint(g);
} // loop through all the frames
while (running)
{
repaint();
guy.nextFrame();
...
}
Figure 2.8 A Sprite image consisting of 12 frames
In addition to various transformations such as rotation and mirroring,
the Sprite class also provides collision detection, which is essential
for games as it allows the developer to detect when the sprite collides
with another element and prevent the hero from crossing a solid wall or
obstacle. Both pixel-level and bounding-rectangle collisions are available.
LayerManager
As the name implies, the LayerManager manages a series of Layer
objects. Sprite and TiledLayer both extend Layer . More specif-
ically, a LayerManager controls the rendering of Layer objects. It
maintains an ordered list so that they are rendered according to their
z-values (in standard computer graphics terminology). We add a Layer
to the list using the method:
public void append(Layer l);
The first layer appended has index zero and the lowest z-value - that is,
it appears closest to the user (viewer). Subsequent layers have successively
Search WWH ::




Custom Search