Class independence

Hey folks, this is my first post. I’m seasoned ActionScript dev who’s fed up with the poor performance of the Flash Player. I’m excited to be learning openFrameworks.

What seemed like a simple task is proving to be hair-pulling.

I’ve written a simple class called “circle”. The class draws a circle (who knew!) and scales the radius to a max setting and then scales to a min setting and repeats the process. When it reaches its max size, it’s Y position as increased by 10. Just a basic circle that scales up and down and moves.

I have 4 instances of the circle declared in my base class. The setup, update and draw methods call each circle instance’s setup, update and draw methods accordingly.

When I run the code, the circles scale up and down at the same pace. However, they do not move at the same pace. They alternate. So when they all reach their max size the first time, only circle 1 moves its Y position. The second time, circle 2 moves, the third time, circle 3 and so on.

I’m pretty confused. I expected them all to move together. This was suppose to be any easy task. What am I missing? OOP is not something new to me. I’ve done this 100’s of times in ActionScript.

Circle Class:

  
#include "circleBrush.h"  
  
float rmin = 20;  
float rmax = 120;  
float speed = 1;  
  
CircleBrush::CircleBrush(){  
      
}  
  
void CircleBrush::setup(){  
    color = ofRandom(255);  
    radius = 60;  
}  
  
void CircleBrush::update(){  
    radius+= speed;  
      
    if(radius >= rmax){  
        speed *= -1;  
        y += 20;  
    }  
      
    if(radius <= rmin){  
        speed *= -1;  
        y += 20;  
    }  
      
}  
  
void CircleBrush::draw(){  
    ofSetColor(color);  
    ofCircle(x, y, radius);  
}  
  
void CircleBrush::moveTo(int _x, int _y){  
    x = _x;  
    y = _y;  
}  

Base Class:

  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){  
    ofSetFrameRate(30);  
    ofEnableAlphaBlending();  
      
    circle1.setup();  
    circle2.setup();  
    circle3.setup();  
    circle4.setup();  
      
    circle1.moveTo(100, 100);  
    circle2.moveTo(300, 100);  
    circle3.moveTo(500, 100);  
    circle4.moveTo(700, 100);  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
      
   circle1.update();  
   circle2.update();  
   circle3.update();  
   circle4.update();  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
    circle1.draw();  
    circle2.draw();  
    circle3.draw();  
    circle4.draw();  
      
}  

Take a look at your code, all circle instances share the global var speed. Make it a class property and it should work as intended.

HTH,
Stephan

Thanks Stephan. That did it.

I wasn’t aware that variables defined in the .cpp file were considered global. I assumed they’d be private to that instance rather than the class as a whole.