ScoreAnimation

From Gardenwiki

Jump to: navigation, search

A ScoreAnimation is an easy way to add a floating text message to your BoardView class. In the Mummichog game, this is used for the animated messages that say "Match", "Triple", "Game Over", etc.

To use a ScoreAnimation, follow these steps:

1. Import com.threerings.parlor.media.ScoreAnimation, java.awt.Font, java.awt.Color, and com.samskivert.swing.Label .

2. For a simple animation that appears in one place, floats upward for a while, then fades, just add these lines wherever needed in your BoardView class.

       Label la = ScoreAnimation.createLabel(showWord,
                   Color.WHITE,
                   new Font("Arial", Font.BOLD,
                   48),
                   this.getComponent());
       ScoreAnimation sa = new ScoreAnimation(la, drLeftOffset, 
               drTopOffset, 5000L);
       addAnimation(sa);

You specify the time the animation shows by the 5000L, which indicates 5 seconds. Specify the text that will appear by setting the value of showWord. Specify the position of the text by setting drLeftOffset and drTopOffset. The lines above produce white letters, font of Arial bold, font size 48.

3. For an animation that starts in one area of the board view, and moves to a different area before fading, you may want to modify Mummichog's showPraise(int x, int y) method and just call that instead of adding multiple lines as needed. Here is the method as used by Mummichog:

   // creates a ScoreAnimation near the point (x,y)
   protected void showPraise(int x, int y) {
       String praise = "";
       if (_myobj.inaRow == 0) praise = "";
       else praise = "Match";
  
       int ptr = Math.min(_myobj.inaRow, praiseWords.length - 1);
       praise = praiseWords[ptr];
       if (ptr != _myobj.inaRow) {
           int exponent = _myobj.inaRow - ptr + 1;
           praise = praise.replaceAll("!", "");
           praise = praise + "^" + exponent + "!";
           }
          
       //  maximum fontsize to use is 48, minimum 12
       int fontsize = Math.min(12 + 4 * _myobj.inaRow, 48);
       //  adjustment moves the x location to the left
       //  so that Vegas^12 fits on the board
       int adjustment = fontsize * praise.length() / 4;
       x = Math.max(0, x - adjustment);
  
       Label la = ScoreAnimation.createLabel(praise,
                   Color.WHITE,
                   new Font("Arial", Font.BOLD,
                   fontsize),
                   this.getComponent());
       ScoreAnimation sa = new ScoreAnimation(la, x, 
               y, 2000L);
       addAnimation(sa);
   }

The first several lines determine what text should appear in the animation. You will want to modify this or just pass it as a parameter. Use any formula that makes sense to you to determine the font size.

The adjustment tweaks the starting position so that when the font size is large the entire text has a chance of fitting on the board view. Most likely it won't work very well if font size is large and the string to be displayed is long. It works just fine for font size 48 and string "Vegas^99!".

Personal tools