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 >> Interfacing Genuts Framework and Java API >>Controlling a sprite with the mouse

Controlling a sprite with the mouse

In this example we will see how to interface events in a sprite wrapper.

  Type a key to switch between hours mode and date mode.

Click with the mouse to switch between 12-hours and 24-hours clock display.

Things are not really different between this case and the previous one. The MouseWrapper implements MouseListener to intercept mouse events.
Here is the code which modifies the state of the TimeWrapper:


...
  public void mouseClicked(MouseEvent e) {
    TimeWrapper sprite = (TimeWrapper) getActionSprite();
    if (sprite.getDisplay() == TimeWrapper.DISPLAY_24HOURS) {
      sprite.setDisplay(TimeWrapper.DISPLAY_AM_PMHOURS);
    } else {
      sprite.setDisplay(TimeWrapper.DISPLAY_24HOURS);
    }
  }
...

The main difference is the way to associate the MouseListener with the playfield. We can incorporate it directly in the wrapper: when a new parent playfield is set for the sprite, we can associate the MouseListener with that new parent playfield (no more worry about that! ):


...
  protected void setParent(PlayField parent) {
    if (getParent() != null) {
      getParent().removeMouseListener(this);
    }
    super.setParent(parent);
    if (parent != null) {
      getParent().addMouseListener(this);
    }
  }
...

And now, in the MouseTimeApplet applet, we only to instantiate objects:


...
    // Creates the time label
    SpriteLabel label = new SpriteLabel("");
    label.setFont(new Font("SansSerif", Font.BOLD, 24));
    label.setColor(Color.gray);
    timeSprite = new TimeWrapper(label);
    Sprite sprite = new MovingWrapper(new MouseWrapper(timeSprite), 2, 2);
    playfield.addSprite(sprite);
...

Now it is in your hands, have fun.

<< Previous Page  

API >> Tutorials >> Interfacing Genuts Framework and Java API >>Controlling a sprite with the mouse