public class Sprite
extends java.lang.Object
The sprite attributes are:
You can find a complete game API sample named 'Scape' in the TotalCross samples folder.
Here is some sample code:
// Ball is a the Sprite bouncing on the screen. // The ball bounces on the left, the top and the bottom border and // must be hit by the racket on the right border to keep the ball inside the screen. // If the racket misses the ball, the game is over. public final class Ball extends Sprite { // random generator for the starting speeds private Random rand=new Random(); // keep a reference to the game mainclass for data access & game control. private Ping game; // other important sprite the ball sprite must know about for interaction... private Racket racket; // ball movement speed in both directions in double values for acceleration precision private double speedx,speedy; // and in integer format for quicker move computing. private int ispeedx,ispeedy; //---------------------------------------------------------------- // Ball constructor. // @param game the Ping game mainclass. // @param racket Sprite that interacts with the ball. //---------------------------------------------------------------- public Ball(Ping game,Racket racket) { // setup the sprite by loading the bitmap, defining the transparency color. // The ball element does not fill the whole bitmap, the corners are filled // with WHITE pixels. By setting WHITE as the transparency color and selecting the // DRAW_SPRITE draw mode, the bitmap white pixels are not shown and the background // is not affected by the bitmap display. The background saving feature is disabled // because the whole screen is cleared and redrawn at each frame. No valid region // (null) means, default valid region is right. Except that the miny value must be // incremented to prevent the ball hiding the s super(new Image("ball.bmp"),Color.WHITE,false,null); drawOp=Graphics.DRAW_SPRITE; // the ball may go out of the window when the user misses it doClip = true; // by default the valid area of a sprite is the screen. // reduce the playfield of the racket by the level & score display zone regionMiny+=Ping.GAME_MINY; this.game=game; this.racket=racket; // initialize the ball reinit(); } // initialization needed each time a game starts. // The racket is placed in the middle of the right border and the ball // is placed next to it. // Also defines the initial ball speed. public void reinit() { Coord pos=racket.getBallPosition(halfWidth); setPos(ballHitX=pos.x,pos.y,false); speedx=4+rand.nextFloat()*6.0f; speedy=2+rand.nextFloat()*3.0f; ispeedx=-1; ispeedy=-1; // the toward position specified in the towardPos call is not a // direction but an ending position, so speed is irrelevant. speed=1000; ... } //---------------------------------------------------------------- // Move the ball to next position depending on the current speed. //---------------------------------------------------------------- public void move() { // add the speed vector and enable position validation. // see the "Scape" sample code for more details about position validation. // this towardPos() moves the sprite toward a point, the third parameter // enables position validation which is needed for each position change to // precisely detect the racket or the borders hits. // This call could be optimized to disable the time consuming position // validation when the ball is not near the screen border. towardPos(centerX+ispeedx,centerY+ispeedy,true); }
Modifier and Type | Field and Description |
---|---|
protected Image |
background
Background image.
|
protected Graphics |
bgGfx
Background graphic context.
|
protected int |
bgX
Background restoring position.
|
protected int |
bgY
Background restoring position.
|
int |
centerX
Sprite center position.
|
int |
centerY
Sprite center position.
|
boolean |
doClip
set to false if this sprite never reaches the screen boundaries.
|
protected Graphics |
gfx
Graphics to draw at.
|
int |
height
Sprite width/height dimensions.
|
Image |
image
Sprite image.
|
protected static int |
INVALID |
protected int |
regionMaxx
Sprite valid region.
|
protected int |
regionMaxy
Sprite valid region.
|
protected int |
regionMinx
Sprite valid region.
|
protected int |
regionMiny
Sprite valid region.
|
protected boolean |
screenErased |
int |
speed
Speed in pixels used by the function towardPos.
|
protected GfxSurface |
surface
Surface to draw at.
|
int |
width
Sprite width/height dimensions.
|
Constructor and Description |
---|
Sprite(Image image,
int transColor,
boolean saveBckgd,
Rect region)
Sprite constructor.
|
Sprite(Image image,
int nrFrames,
int transColor,
boolean saveBckgd,
Rect region)
Sprite constructor.
|
Modifier and Type | Method and Description |
---|---|
boolean |
collide(Sprite s)
Test if the sprite collides with another one.
|
Coord |
getPos()
Retrieve the sprite position (actually its center).
|
Rect |
getRegion()
Retrieve the sprite valid region.
|
void |
hide()
Restore the sprite's saved background.
|
boolean |
onPositionChange()
Default position validation function.
|
boolean |
setPos(int x,
int y,
boolean doValidate)
Sets the sprite position (actually its center).
|
void |
setRegion(Rect region)
Change the sprite valid region.
|
void |
show()
Draw the sprite at it's current position using the defined drawOp.
|
boolean |
towardPos(int x,
int y,
boolean doValidate)
Moves the sprite toward the specified position.
|
public int centerX
public int centerY
public int width
public int height
protected int regionMinx
onPositionChange()
protected int regionMiny
onPositionChange()
protected int regionMaxx
onPositionChange()
protected int regionMaxy
onPositionChange()
public Image image
protected Graphics gfx
protected GfxSurface surface
public int speed
towardPos(int, int, boolean)
protected boolean screenErased
public boolean doClip
protected static final int INVALID
protected Image background
protected Graphics bgGfx
protected int bgX
protected int bgY
public Sprite(Image image, int transColor, boolean saveBckgd, Rect region) throws java.lang.IllegalArgumentException, java.lang.IllegalStateException, ImageException
image
- sprite image.transColor
- sprite's transparency color or -1 if nonesaveBckgd
- true if the background should be saved each time the sprite is drawn to restore it once the sprite
moves.region
- defines the sprite valid area.ImageException
java.lang.IllegalStateException
java.lang.IllegalArgumentException
public Sprite(Image image, int nrFrames, int transColor, boolean saveBckgd, Rect region) throws java.lang.IllegalArgumentException, java.lang.IllegalStateException, ImageException
image
- sprite image.transColor
- sprite's transparency color or -1 if nonesaveBckgd
- true if the background should be saved each time the sprite is drawn to restore it once the sprite
moves.region
- defines the sprite valid area.ImageException
java.lang.IllegalStateException
java.lang.IllegalArgumentException
public final Rect getRegion()
public final void setRegion(Rect region)
region
- new positions validity areapublic boolean onPositionChange()
towardPos(int, int, boolean)
public final boolean setPos(int x, int y, boolean doValidate)
x
- position.y
- position.doValidate
- if true the position is validated which means that the onPositionChange() function is called.onPositionChange()
public final Coord getPos()
public boolean towardPos(int x, int y, boolean doValidate)
x
- position.y
- position.doValidate
- if true the position is validated which means that the onPositionChange() function is called.onPositionChange()
public void show()
public void hide()
public boolean collide(Sprite s)
s
- sprite to test with