Beginner's Tutorial "Ooops! Object Oriented..." problem

Hello!

I am a super mega beginner with basically no programming worth noting under my belt.
I am trying to do the very informative Ooops! tutorial (from t he wiki) from here: http://wiki.openframeworks.cc/index.php-…-2B-Classes

So, I am walking through this step by step, and got to the end. For some reason, my little circles/balls are all the same size, even though I declared their dimensions to be different in my array (according to the tutorial). I can’t figure out what I am doing incorrectly. I tried to see if things are being drawn/updated somewhere else (it’s my first time working with C++ or CodeBlocks or any of this stuff)…

Here are my files in the project -

main.cpp

  
#include "ofMain.h"  
#include "testApp.h"  
#include "ofAppGlutWindow.h"  
  
//========================================================================  
int main( ){  
  
    ofAppGlutWindow window;  
	ofSetupOpenGL(&window, 1024,768, OF_WINDOW);			// <-------- setup the GL context  
  
	// this kicks off the running of my app  
	// can be OF_WINDOW or OF_FULLSCREEN  
	// pass in width and height too:  
	ofRunApp( new testApp());  
  
}  
  

testApp.h

  
  
#ifndef _TEST_APP  
#define _TEST_APP  
  
  
#include "ofMain.h"  
#include "ofBall.h"  
  
  
  
class testApp : public ofBaseApp{  
  
	public:  
		void setup();  
		void update();  
		void draw();  
  
  
        void keyPressed  (int key);  
		void keyReleased(int key);  
		void mouseMoved(int x, int y );  
		void mouseDragged(int x, int y, int button);  
		void mousePressed(int x, int y, int button);  
		void mouseReleased(int x, int y, int button);  
		void windowResized(int w, int h);  
  
  
        ofBall**myBall;  
        int nBalls;  
  
};  
  
#endif  
  

testApp.cpp

  
  
#include "testApp.h"  
  
//--------------------------------------------------------------  
  
  
  
void testApp::setup(){  
  
    nBalls = 95;  
    myBall = new ofBall*[nBalls];  
        for (int i = 0; i < nBalls; i++){  
            float x = 20+(100*i);  
            float y = 20+(100*i);  
            int dim = 10+(i*100);  
  
            myBall[i] = new ofBall(x,y,dim);  
        }  
  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
  
  
    for (int i = 0; i < nBalls; i++){  
        myBall[i]->update();  
    }  
  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
  
    for (int i = 0; i < nBalls; i++){  
        myBall[i]->draw();  
    }  
  
}  
  
//--------------------------------------------------------------  
void testApp::keyPressed(int key){  
  
}  
  
//--------------------------------------------------------------  
void testApp::keyReleased(int key){  
  
}  
  
//--------------------------------------------------------------  
void testApp::mouseMoved(int x, int y ){  
  
}  
  
//--------------------------------------------------------------  
void testApp::mouseDragged(int x, int y, int button){  
  
}  
  
//--------------------------------------------------------------  
void testApp::mousePressed(int x, int y, int button){  
  
}  
  
//--------------------------------------------------------------  
void testApp::mouseReleased(int x, int y, int button){  
  
}  
  
//--------------------------------------------------------------  
void testApp::windowResized(int w, int h){  
  
}  
  
  
  
  

ofBall.h

  
  
  
#ifndef _OF_BALL  
#define _OF_BALL  
  
#include "ofMain.h"  
  
class ofBall{  
  
  public:  
    //method  
    void update();  
    void draw();  
  
    //constructor  
    ofBall(float x, float y, int dim);  
  
	float x; // position  
	float y;  
	float speedY; // speed and direction  
	float speedX;  
    int dim; // size  
  
};  
  
  
#endif  
  
  

and ofBall.cpp

  
  
#include "ofBall.h"  
  
ofBall::ofBall(float _x, float _y, int _dim)  
  
{  
    x = ofRandom(0, ofGetWidth());  
    y = ofRandom(0, ofGetHeight());  
    speedX = ofRandom(-1,1);  
    speedY = ofRandom(-1,1);  
    dim = 20;  
}  
  
void ofBall::update(){  
  if (x < 0){  
        x = 0;  
        speedX *= -1;  
    } else if (x > ofGetWidth()) {  
        x = ofGetWidth();  
        speedX *=-1;  
        }  
  
  
    if (y < 0){  
        y = 0;  
        speedY *= -1;  
    } else if (y > ofGetHeight()) {  
        y = ofGetHeight();  
        speedY *= -1;  
        }  
  
    x+=speedX;  
    y+=speedY;  
  
}  
  
void ofBall::draw(){  
        // values for R, G, B  
	ofSetColor(120,120,120);  
	ofCircle(x, y, dim);  
  
}  
  
  
  

Thanks in advance for helping me out.
Also thanks for making the wiki/tutorials/examples, etc

—b

I think you want to copy the value of _dim in the constructor, like:

  
dim = _dim;  

as opposed to:

  
dim = 20;  

does that make sense? you are giving them all unique values through the constructor, but they are not getting stored inside the objects variables (ie ball.dim is being set to 20, not what you pass in).

hope that helps!
zach

Hmmm changing it to “dim” or “_dim” left me without any balls on the screen…

in testApp.cpp there’s this -

  
 myBall = new ofBall*[nBalls];  
        for (int i = 0; i < nBalls; i++){  
            float x = 20+(100*i);  
            float y = 20+(100*i);  
            int dim = 10+(i*10);  
  

but the “dim = 20” is in the ofBall.cpp

Which one updates the other?

Thanks for the help Zach!