Class Sprite


  • @Deprecated
    public class Sprite
    extends java.lang.Object
    Deprecated.
    This class implements a game Sprite.

    A sprite is a graphical object typically used in games that can be moved, with collision detection feature and background saving/restoring capability.

    The sprite attributes are:

  • position
    The sprite position centerX,centerY is in fact its center position.

  • size
    width/height contains respectively the image horizontal/vertical dimensions.

  • half size
    halfWidth/halfHeight contains respectively the half horizontal/vertical dimensions.

  • valid region
    The valid region of a sprite are relative to the sprite center. The valid region can be provided in the Sprite constructor or by the setRegion call. Both are totalcross.ui.gfx.Rect objects. If null is passed in the constructor the default region is computed to prevent the sprite living even partially the screen.
    Once set the region can be modified by incrementing/decrementing the values of regionMinx, regionMiny, regionMaxx and regionMaxy

  • draw operation
    The default value of drawOp is DRAW_PAINT if its value is DRAW_PAINT the image is copied "as is" to the buffer, and
    if its value is DRAW_SPRITE the transparency color specified in the constructor is used to prevent the copy of image pixels of this color in order to preserve the background.

    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);
       }