The Java Game Framework.

Examples, tutorials, documentation and download of the Genuts Framework.
Games made with the Genuts Frameworks.
General articles around games.
Links covering all needs around game dev.
Who are we?
Terms of Service

API >> Tutorials >> Playfield Scrolling And Pre-Collision mode >> Playfield Scrolling

Playfield Scrolling

In this course, we display only a small part of playfield from the previous example and we make able the scrolling inside using the mouse.

    Drag with the mouse to move the visible area.  

We choose a small visible part without scale relation between its size and the size of the playfield. Here, the visible part size is 10 tiles horizontally and 5 tiles vertically.

First thing to know is to not use a Layout Manager for the container of the playfield, because it uses the preferred size of its components.

This explanation is only true when the playfield use a SpriteDisplayManager. Of course, we can not guarantee anything with another DisplayManager.

How does it work?

You have to remember the 3 magic words:
  • Preferred size
  • Size
  • Offset

Preferred size

The real size of the playfield.

Size

The size of the visible part of the playfield.

Offset

The top left corner coordinates of the visible part of the playfield. This is used even if the size and the preferred size have the same value (for example: you can shake easily the playfield).

Now the example

We explain now how to manipulate the scrolling; the source code is from the file ScrollingPathFinder.java

Initialization of the playfield

We have 4 things to do:
  • Set the LayoutManger of the container to null
  • Creates the playfield with its preferred size
  • Set the size of the playfield
  • Set the size of the container

This is done by:


...
    // Creates the playfield
    SpriteCollisionManager collisionManager = new SpriteCollisionManager(10);
    playfield = new PlayField(collisionManager,
			   10*Tiles.tileSize,
			   5*Tiles.tileSize);
    playfield.setPreferredSize(Tiles.tiles[0].length*Tiles.tileSize,
			    Tiles.tiles.length*Tiles.tileSize);
...
    setSize(10*Tiles.tileSize,
	   5*Tiles.tileSize);
...

Scrolling the visible part

The only thing to do is to set the offset value to another one when we want to scroll. This is done by the method setOffset(int, int) of the playfield.
Here is the source code of the MouseMotionListener and the MouseListener used to scroll the visible part of the playfield:


...
  ////////////////////////////////////////////////////
  /////////  MouseMotionListener  ////////////////////
  ////////////////////////////////////////////////////
  public void mouseDragged(MouseEvent e) {
    int x = playfield.getXOffset() - e.getX() + mouseX;
    int y = playfield.getYOffset() - e.getY() + mouseY;
    if (x < 0) {
      x = 0;
    } else if ((x + playfield.getWidth()) > playfield.getPreferredWidth()) {
      x = playfield.getPreferredWidth() - playfield.getWidth();
    }

    if (y < 0) {
      y = 0;
    } else if ((y + playfield.getHeight()) > playfield.getPreferredHeight()) {
      y = playfield.getPreferredHeight() - playfield.getHeight();
    }
    
    
    playfield.setOffset(x, y);
    mouseX = e.getX();
    mouseY = e.getY();
  }

  public void mouseMoved(MouseEvent e) {
  }

  ////////////////////////////////////////////////////
  /////////  MouseListener  //////////////////////////
  ////////////////////////////////////////////////////
  public void mouseClicked(MouseEvent e) {
  }

  public void mousePressed(MouseEvent e) {
    mouseX = e.getX();
    mouseY = e.getY();
  }

  public void mouseReleased(MouseEvent e) {
  }

  public void mouseEntered(MouseEvent e) {
  }

  public void mouseExited(MouseEvent e) {
  }
...

Now you know all the mechanism of the playfield scrolling.

It will be a good exercise to try to resolve the next course, and it is the first step for some exploration games.

<< Previous Page Next Page >>

API >> Tutorials >> Playfield Scrolling And Pre-Collision mode >> Playfield Scrolling