|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--superwaba.ext.xplat.game.Sprite
Class implementing a game Sprite.
A sprite is a graphical object typicaly used in games that can be moved,
with collision detection feature and background saving/restoring capability.
The sprite attributes are:
You can find a complete game API sample named 'Ping' in the SW examples 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 score. 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 "Ping" 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); }
Field Summary | |
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. |
int |
drawOp
The sprite drawOp. |
protected Graphics |
gfx
Graphics to draw at. |
protected int |
halfHeight
Sprite half width/height dimensions. |
protected int |
halfWidth
Sprite half width/height dimensions. |
int |
height
Sprite width/height dimensions. |
protected 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 ISurface |
surface
Surface to draw at. |
protected Color |
transColor
cached image transparency color |
int |
width
Sprite width/height dimensions. |
Constructor Summary | |
protected |
Sprite(Image image,
Color transColor,
boolean saveBckgd,
Rect region)
Sprite constructor. |
Method Summary | |
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. |
Methods inherited from class java.lang.Object |
equals,
getClass,
hashCode,
notify,
toString,
wait,
wait |
Field Detail |
public int centerX
public int centerY
public int width
public int height
protected int halfWidth
protected int halfHeight
protected int regionMinx
onPositionChange()
protected int regionMiny
onPositionChange()
protected int regionMaxx
onPositionChange()
protected int regionMaxy
onPositionChange()
protected Image image
protected Graphics gfx
protected ISurface surface
public int drawOp
Graphics
public int speed
towardPos(int, int, boolean)
protected Color transColor
protected boolean screenErased
public boolean doClip
protected static final int INVALID
protected Image background
protected Graphics bgGfx
protected int bgX
protected int bgY
Constructor Detail |
protected Sprite(Image image, Color transColor, boolean saveBckgd, Rect region)
image
- sprite image.transColor
- sprite's transparency color or null 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.Method Detail |
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
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |