com.siemens.mp.game
Class Sprite

java.lang.Object
  extended bycom.siemens.mp.misc.NativeMem
      extended bycom.siemens.mp.game.GraphicObject
          extended bycom.siemens.mp.game.Sprite

public class Sprite
extends GraphicObject

The Sprite class implements drawable sprite objects.

Sprites are figures or elements on the screen that have the capability of moving independently of one another. A sprite consists of one or more bitmaps - frames. When a sprite has several frames, only one frame is visible on the display. The user can change the visible frame at runtime by setFrame method call, to implement some sort of animation.

When a sprite is created, the user provides a bitmap mask to select, which regions of sprite will be drawn and which ones will be transparent. Also, a collision rectangle may be set for each sprite by setCollisionRectangle method call. This rectangle may be used to check collision with specific point or other sprite.

Because the Sprite class is a subclass of GraphicObject, it has no direct drawing capability available for the user. Instead, when included into a GraphicObjectManager collection, a sprite will be drawn in proper order on a GraphicObjectManager.paint method call.

See Also:
GraphicObject, GraphicObjectManager

Example


Constructor Summary
Sprite(byte[] pixels, int pixel_offset, int width, int height, byte[] mask, int mask_offset, int numFrames)
          Creates a sprite directly from bytearrays.
Sprite(ExtendedImage pixels, ExtendedImage mask, int numFrames)
          Creates a sprite from previously created extended images.
Sprite(Image pixels, Image mask, int numFrames)
          Creates a sprite from previously created images.
 
Method Summary
 int getFrame()
          Returns currently visible frame number.
 int getXPosition()
          Returns the current x coordinate of the top-left corner of the sprite.
 int getYPosition()
          Returns the current y coordinate of the top-left corner of the sprite.
 boolean isCollidingWith(Sprite other)
          Checks if the sprite collides with another one.
 boolean isCollidingWithPos(int xpos, int ypos)
          Checks if the sprite collides with a specific point.
 void setCollisionRectangle(int x, int y, int width, int height)
          Sets the collision rectangle.
 void setFrame(int framenumber)
          Sets the frame, which will become visible when the sprite is drawn.
 void setPosition(int x, int y)
          Sets the coordinates of the top-left corner of the sprite.
 
Methods inherited from class com.siemens.mp.game.GraphicObject
getVisible, setVisible
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Sprite

public Sprite(Image pixels,
              Image mask,
              int numFrames)
       throws IllegalArgumentException
Creates a sprite from previously created images. The width of both the pixels and the mask Image parameters must be a multiple of 8. For more complete information on sprite creation, see the Sprite(byte[], int, int, int, byte[], int, int) constructor description.

Frames must be placed below each other, not side by side!

Parameters:
pixels - a standard Image with the image information; the width must be a multiple of 8
mask - a standard Image with the mask information; the width must be a multiple of 8
numFrames - the number of frames the sprite has; the minimum is 1
Throws:
IllegalArgumentException - if the width of any image is not a multiple of 8,
if there is a wrong number of frames,
if any Image with transparent color is used,
if the image and the mask do not have the same size,
if the height of the image is not divisible by the number of frames

Example


Sprite

public Sprite(ExtendedImage pixels,
              ExtendedImage mask,
              int numFrames)
       throws IllegalArgumentException
Creates a sprite from previously created extended images. The width of both the pixels and the mask ExtendedImage parameters must be a multiple of 8. For more complete information on sprite creation, see the Sprite(byte[], int, int, int, byte[], int, int) constructor description.

Frames must be placed below each other, not side by side!

Parameters:
pixels - an ExtendedImage with the image information; the width must be a multiple of 8
mask - a ExtendedImage with the mask information; the width must be a multiple of 8
numFrames - the number of frames the sprite has; the minimum is 1
Throws:
IllegalArgumentException - if the width of any image is not a multiple of 8,
if there is a wrong number of frames,
if any ExtendedImage with transparent color is used,
if the image and the mask do not have the same size,
if the height of the image is not divisible by the number of frames

Example


Sprite

public Sprite(byte[] pixels,
              int pixel_offset,
              int width,
              int height,
              byte[] mask,
              int mask_offset,
              int numFrames)
       throws IllegalArgumentException
Creates a sprite directly from bytearrays.

Both pixels and mask bytearrays contain one-bit-per-pixel (1 bpp) information. The pixels array contains bitmaps for all frames, one after another. Each bit value means: 1 - black pixel, 0 - white pixel. The mask array contains masks for all frames, one after another. Each bit value means: 1 - drawable pixel, 0 - transparent pixel. Bits in both arrays are placed line by line, frame by frame. Total number of bits in either pixels or mask array is

    width*height*numFrames
So, the size of each array currently is restricted to
    (width*height*numFrames)/8
and the sprite width must be a multiple of 8.

Parameters:
pixels - a bytearray with the image information (1 bpp), its size must be (width*height*numFrames)/8
pixel_offset - reserved, must be 0
width - the sprite width, must be a multiple of 8
height - the sprite height, must be a multiple of 8
mask - a bytearray with the mask information (1 bpp), its size must be (width*height*numFrames)/8
mask_offset - reserved, must be 0
numFrames - the number of frames the sprite has, the minimum is 1
Throws:
IllegalArgumentException - if the width is not a multiple of 8 or
if there is a wrong number of frames or
if the size of the pixelarray or maskarray is not equals (width*height*numFrames)/8
See Also:
GraphicObjectManager.createTextureBits

Example

Method Detail

setPosition

public void setPosition(int x,
                        int y)
Sets the coordinates of the top-left corner of the sprite. Coordinates are interpreted relative to the GraphicObjectManager drawing area coordinates.

Parameters:
x - the x coordinate of the top-left corner of the sprite
y - the y coordinate of the top-left corner of the sprite
See Also:
getXPosition, getYPosition, GraphicObjectManager.paint

Example


getXPosition

public int getXPosition()
Returns the current x coordinate of the top-left corner of the sprite. Coordinates are interpreted relative to the GraphicObjectManager drawing area coordinates.

Returns:
the x coordinate of the top-left corner of the sprite
See Also:
getYPosition, setPosition, GraphicObjectManager.paint

Example


getYPosition

public int getYPosition()
Returns the current y coordinate of the top-left corner of the sprite. Coordinates are interpreted relative to the GraphicObjectManager drawing area coordinates.

Returns:
the y coordinate of the top-left corner of the sprite
See Also:
getXPosition, setPosition, GraphicObjectManager.paint

Example


setCollisionRectangle

public void setCollisionRectangle(int x,
                                  int y,
                                  int width,
                                  int height)
Sets the collision rectangle. This rectangle is used to check collision with a specific point in isCollidingWithPos method call or to check collision with another sprite in isCollidingWith method call. The x and y values are relative to the sprite's own coordinates (x=0 and y=0 is the sprite's top-left corner). Negative values may be used for x and y parameters to set a collision rectangle larger than the sprite itself.

Parameters:
x - the x coordinate of the top-left corner of the sprite collision rectangle
y - the y coordinate of the top-left corner of the sprite collision rectangle
width - the width of the sprite collision rectangle
height - the height of the sprite collision rectangle
See Also:
isCollidingWith, isCollidingWithPos

Example


isCollidingWith

public boolean isCollidingWith(Sprite other)
Checks if the sprite collides with another one. A sprite collides with another sprite if their collision rectangles have at least one common point.

Parameters:
other - a sprite to check collision with
Returns:
true, if the sprite collides with the given one, false otherwise
See Also:
setCollisionRectangle, isCollidingWithPos, GraphicObjectManager.paint

Example


isCollidingWithPos

public boolean isCollidingWithPos(int xpos,
                                  int ypos)
Checks if the sprite collides with a specific point. A sprite collides with a specific point if this point is in the sprites' collision rectangle. The point's coordinates are interpreted relative to the GraphicObjectManager drawing area coordinates.

Parameters:
xpos - the x coordinate of the point
ypos - the y coordinate of the point
Returns:
true, if the sprite collides with the given point, false otherwise
See Also:
setCollisionRectangle, isCollidingWith, GraphicObjectManager.paint

Example


setFrame

public void setFrame(int framenumber)
Sets the frame, which will become visible when the sprite is drawn.

Parameters:
framenumber - the number of the frame (from zero to frameCount-1)
See Also:
getFrame, Sprite constructor, GraphicObjectManager.paint

Example


getFrame

public int getFrame()
Returns currently visible frame number.

Returns:
currently visible frame number (from zero to frameCount-1)
See Also:
setFrame, Sprite constructor, GraphicObjectManager.paint

Example



Generated on 2003-10-17For further information and updates, please visit Siemens mobile Developer Portal