Ha! … here I go replying to my own post.
I found a project that involved a custom class .h and .cpp file(s) here and started poking around a bit…
http://openframeworks.cc/forum/viewtopi-…-class#1501
…in particular in the ofCamera class to try and understand how to do classes and such - and I got a bit further, but still not quite there - I’m simply too new to C++ - I’m stumbling around at random! Not sure if this was a good example to copy, but at least it got me started.
Anyway, have a look at this project-folder, there are now two new files ofBall.h and ofBall.cpp: www.okdeluxe.co.uk/-files/-MK-ball-obje-…-lasses.zip
Basically what I’ve done is this:
Code for ofBall.h:
#ifndef _OF_BALL
#define _OF_BALL
#include "ofMain.h"
class ofBall {
private:
// vars
float x;
float y;
float speedX;
float speedY;
public:
// methods
void update();
void draw();
// constructor
ofBall();
};
#endif
Code for ofBall.cpp
#include "ofBall.h"
ofBall::ofBall(){
x = ofGetScreenWidth()/2;
y = ofGetScreenHeight()/2;
speedX = ofRandom(-5, 5);
speedY = ofRandom(-5, 5);
};
void ofBall::update(){
x += speedX;
y += speedY;
if(x < 0 || x > ofGetWidth()) speedX *= -1;
if(y < 0 || y > ofGetHeight()) speedY *= -1;
}
void ofBall::draw(){
ofSetColor(0xFFBB00);
ofCircle(x, y, 20);
string pos = ofToString(x, 0) + ", " + ofToString(y, 0) ;
ofDrawBitmapString(pos, x-30,y+40);
ofSetColor(0x00AEEF);
}
Then I’ve tried to instantiate and make calls to it from all the right places … I’ve managed to compile a version that runs, only problem is, the ofBall class has NO EFFECT what so ever…
Any ideas/suggestions/recommendations?
Cheers,
Mikkel[http://forum.openframeworks.cc/index.php/topic,363.msg1501.html#msg1501]http://forum.openframeworks.cc/index.php/topic,363.msg1501.html#msg1501[/url]](http://forum.openframeworks.cc/index.php/topic,363.msg1501.html#msg1501]http://forum.openframeworks.cc/index.php/topic,363.msg1501.html#msg1501[/url])
Basically what I’ve done is this:
Code for ofBall.h:
#ifndef _OF_BALL
#define _OF_BALL
#include "ofMain.h"
class ofBall {
private:
// vars
float x;
float y;
float speedX;
float speedY;
public:
// methods
void update();
void draw();
// constructor
ofBall();
};
#endif
Code for ofBall.cpp
#include "ofBall.h"
ofBall::ofBall(){
x = ofGetScreenWidth()/2;
y = ofGetScreenHeight()/2;
speedX = ofRandom(-5, 5);
speedY = ofRandom(-5, 5);
};
void ofBall::update(){
x += speedX;
y += speedY;
if(x < 0 || x > ofGetWidth()) speedX *= -1;
if(y < 0 || y > ofGetHeight()) speedY *= -1;
}
void ofBall::draw(){
ofSetColor(0xFFBB00);
ofCircle(x, y, 20);
string pos = ofToString(x, 0) + ", " + ofToString(y, 0) ;
ofDrawBitmapString(pos, x-30,y+40);
ofSetColor(0x00AEEF);
}
Then I’ve tried to instantiate and make calls to it from all the right places … I’ve managed to compile a version that runs, only problem is, the ofBall class has NO EFFECT what so ever…
Any ideas/suggestions/recommendations?
Cheers,
Mikkel