In the CubeChaser
class, the added CubeChaserControl
classes mark individual cubes, and the AppState
object identifies and transforms them when certain conditions are met. Combining both AppState
objects and controls can be quite powerful! You often use AppState
objects to work with certain subsets of spatials that all carry one specific control.
A control can add custom methods to its spatial, and the AppState
object can call these methods to make individual spatials do the AppState
object's bidding. As an example, let's make the scared cubes identify themselves by name, and count how often cubes get scared in total.
Add the following custom method to the
CubeChaserControl
class:public String hello(){ return "Hello, my name is "+spatial.getName(); }
In the
CubeChaserState
class, create a simple counter variable and a public accessor for it.private int counter=0; public int getCounter() { return counter; }