Managing the RibbonsRibbonsManager 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 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); }
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. |