ActionSprite

From Gardenwiki

Jump to: navigation, search

An Action sprite is a sprite that may be pressed to generate an ActionEvent that will trigger a method in your Controller.

Implement the ActionSprite Interface

If you already have a class that extends Sprite, have that class implement the ActionSprite interface. Here is an example of how to do this for an ImageSprite.

 public class ActionImageSprite extends ImageSprite 
   implements ActionSprite {
   public ActionImageSprite(BufferedMirage image, String action) {
       super(image);
       _action = action;
   }
   public String getActionCommand() {
       return _action;
   }
   private String _action;
 }

Add Action Method to Controller

Inside your Controller class, add the method to be triggered. You can have multiple methods or just a single one. Let's assume that we want to have two different possible action events. The methods could look like these.

   public void plusClicked() {
       // add lines that you want executed 
       // if the plus sprite is clicked
   }
   public void minusClicked() {
       // add lines that you want executed 
       // if the minus sprite is clicked
   }   
   

Use ActionSprite in BoardView

Inside your BoardView class, create one or more instances of your ActionSprite. Here is an example using ActionImageSprite.

      BufferedMirage image = ... find the image ..... ;
      _plusSprite = new ActionImageSprite(image, 
                       "plusClicked");

Notice that the String being passed matches the name of the method in Controller. After creating the instance ActionImageSprite, treat it exactly the same as a normal Sprite. When a player clicks a non-transparent part of the sprite, the correct method will be triggered in Controller.

Personal tools