I create a class “Ball” . In this I created the “Ball :: keypressed” and “Ball :: keyReleased” methods. Ex…
void Ball::keyPressed(){
if(key== OF_KEY_RIGHT){
right += 5;
} else if (key== OF_KEY_LEFT){
left -= 5;
}
I would like to make them run with the keys, calling them directly from ofApp. Ex:
void ofApp::keyPressed(int key){
Ball.keypressed();
}
I have no idea how to do it. Can someone help me?
kashim
#2
try adding a ofAddListener to your class listening to the keyboard event:
add:
ofAddListener(ofEvents.keyPressed, this, &MyClass::mykeyPressed);
remove:
ofRemoveListener(ofEvents().keyPressed, this, &MyClass::mykeyPressed);
thank you. Could you explain me better? I never used a listener and I do not know how to use it.
kashim
#4
read:
http://openframeworks.cc/documentation/events/
check this example:
#include "ofMain.h"
class MyClass {
public:
void setup()
{
ofAddListener(ofEvents().keyPressed, this, &MyClass::mykeyPressed);
}
void mykeyPressed(ofKeyEventArgs& eventArgs) { ofLog()<<"MyClass:: "<< eventArgs.key; }
void exit()
{
ofRemoveListener(ofEvents().keyPressed, this, &MyClass::mykeyPressed);
}
};
class ofApp: public ofBaseApp{
public:
MyClass my;
void setup()
{
my.setup();
}
void exit()
{
my.exit();
}
void draw() {
}
};
int main( ){
ofSetupOpenGL(1024,768, OF_WINDOW);
ofRunApp( new ofApp());
}