8.1 Create your own class

Imagine you need a moving circle in your application. To have a circle move at a fixed speed, you will need a variable to store this speed. It clearly belongs with the circle, but it isn’t part of the Circle class provided by Esenthel because not every circle needs to move. So we create our own class:

 
class movingCircle { 
  Circle c; 
  Vec2 speed; 
} 
5 
// declare an object of this class 
movingCirle mc; 
 
// some code in Init() 
10mc.c.set(0.1, Vec2(-0.4, -0.3)); 
mc.speed = Vec2(0.3, 0.5); 
 
// some code in Update() 
mc.c.pos += mc.speed * Time.d();  
15

Once you have a class movingCirle, you can create as much moving circles as you like, all able to remember their own speed. (Although the current class can be improved a lot, which you will learn in a next chapter).

Exercise
  1. Create a container for the class movingCircle. Every time you press the space bar an element should be added to the container, on a random position and with a random speed.
  2. Make sure all circles update their position in the Update function. When a circle goes outside of the screen, put it back in the middle.
  3. Expand the class movingCirle with a color. Every object has a random color, to be used to draw it on the screen.

Note
More work will be done on this exercise in the next chapter. Save it!