Wraparound Ribbons


Managing the Ribbons

RibbonsManager is mainly a router, sending move method calls and update( ) and display( ) calls to the multiple Ribbon objects under its charge. Initially, it creates the Ribbon objects, so it acts as a central storage for their GIFs and move factors.

The initialization phase is carried out in the constructor:

     // globals     private String ribImages[] = {"mountains", "houses", "trees"};     private double moveFactors[] = {0.1, 0.5, 1.0};        // applied to moveSize        // a move factor of 0 would make a ribbon stationary     private Ribbon[] ribbons;     private int numRibbons;     private int moveSize;        // standard distance for a ribbon to 'move' each tick     public RibbonsManager(int w, int h, int brickMvSz, ImagesLoader imsLd)     { moveSize = brickMvSz;             // the basic move size is the same as the bricks map       numRibbons = ribImages.length;       ribbons = new Ribbon[numRibbons];       for (int i = 0; i < numRibbons; i++)          ribbons[i] = new Ribbon(w, h, imsLd.getImage( ribImages[i] ),                                     (int) (moveFactors[i]*moveSize));     }  // end of RibbonsManager( )

The choice of GIFs is hardwired into ribImages[], and the constructor loops through the array creating a Ribbon object for each one.

The basic move size is the same as that used by the bricks layer but multiplied by a fixed moveFactors[] value to get a size suitable for each Ribbon.

A move size is the amount that a background layer moves in each animation period.


A move factor will usually be less than one, to reduce the move size for a Ribbon in comparison to the bricks layer. The Ribbons move more slowly, reinforcing the illusion that they're further back in the scene.

The other methods in RibbonsManager are routers. For example, moveRight( ) and display( ):

     public void moveRight( )     { for (int i=0; i < numRibbons; i++)         ribbons[i].moveRight( );     }     public void display(Graphics g)     /* The display order is important.        Display ribbons from the back to the front of the scene. */     { for (int i=0; i < numRibbons; i++)         ribbons[i].display(g);     }

moveLeft( ), stayStill( ), and update( ) are similar to moveRight( ).


The calls from display( ) ensure that the display of the Ribbons is carried out in a back-to-front order; in this case, mountains, houses, and then trees are displayed.



Killer Game Programming in Java
Killer Game Programming in Java
ISBN: 0596007302
EAN: 2147483647
Year: 2006
Pages: 340

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net