keyboard functions not called in child class?

Hello there,

New to openframeworks and c++ in general. I’m coming from actionscript 3 and it takes a bit getting used to, c++ seems very outdated compared to actionscript 3. But that might just be me.

Anyway, I’m having some trouble getting keyboards event to call inside a class. I’m pretty sure I made a stupid mistake somewhere, but can’t track it since the debugger doesn’t tell me anything.

  
  
#include "ofMain.h"  
#include "testApp.h"  
#include "ofAppGlutWindow.h"  
  
//========================================================================  
int main( ){  
  
    ofAppGlutWindow window;  
	ofSetupOpenGL(&window, 800,600, 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());  
  
}  
  

  
#ifndef _TEST_APP  
#define _TEST_APP  
  
  
#include "ofMain.h"  
#include "player.h"  
  
class testApp : public ofBaseApp{  
  
	public:  
		void setup();  
		void update();  
		void draw();  
  
        //the player  
		player hero;  
  
		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);  
  
};  
  
#endif  
  

  
  
#include "testApp.h"  
#include "player.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){  
  
    //add player  
    //hero = new player();  
  
    //for smooth animation, set vertical sync if we can  
	ofSetVerticalSync(true);  
	// also, frame rate:  
	ofSetFrameRate(60);  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
  
    hero.update();  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
  
    hero.draw();  
  
    //ofSetColor(0x000000);  
    //ofTranslate(ballx, bally);  
    //ofCircle(0, 0, 20);  
}  
  
//--------------------------------------------------------------  
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){  
  
}  
  
  

  
  
#ifndef _PLAYER  
#define _PLAYER  
  
  
#include "ofMain.h"  
  
class player{  
  
	public:  
  
        float x;  
        float y;  
        float ballx;  
		float bally;  
		float velocity;  
  
        player();  
		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);  
  
};  
  
#endif  
  
  

This is where the problem occurs:

  
#include "player.h"  
  
//--------------------------------------------------------------classes have no setup, is a constructor  
player::player(){  
  
    velocity = 5;  
    std::cout << "player init" << endl;  
}  
  
//--------------------------------------------------------------  
void player::update(){  
  
}  
  
//--------------------------------------------------------------  
void player::draw(){  
  
    ofSetColor(0x000000);  
    ofTranslate(ballx, bally);  
	ofCircle(0, 0, 20);  
  
    //std::cout << "player draw" << endl;  
  
}  
  
//--------------------------------------------------------------  
void player::keyPressed(int key){  
  
//not being called!  
  
    switch(key){  
  
        case 'w':  
            bally -= velocity;  
        break;  
  
        case 's':  
            bally += velocity;  
        break;  
  
        case 'd':  
            ballx += velocity;  
        break;  
  
        case 'a':  
            ballx -= velocity;  
        break;  
    }  
  
    std::cout << "keyboard: " << key << endl;  
}  
  
//--------------------------------------------------------------  
void player::keyReleased(int key){  
  
}  
  
//--------------------------------------------------------------  
void player::mouseMoved(int x, int y ){  
  
}  
  
//--------------------------------------------------------------  
void player::mouseDragged(int x, int y, int button){  
  
}  
  
//--------------------------------------------------------------  
void player::mousePressed(int x, int y, int button){  
  
}  
  
//--------------------------------------------------------------  
void player::mouseReleased(int x, int y, int button){  
  
}  
  
//--------------------------------------------------------------  
void player::windowResized(int w, int h){  
  
}  
  
  

Thanks in advance!

hi,

you need to manually pass the keyPressed call down yourself.

so in your testApp::keyPressed(int key) function, you need to call player.keyPressed(key).

hth
d

[quote author=“damian”]hi,

you need to manually pass the keyPressed call down yourself.

so in your testApp::keyPressed(int key) function, you need to call player.keyPressed(key).

hth
d[/quote]
Thanks for the response.
Is there no way to use the function in the player class itself? Only being able to use the function in the top class seems very restricting :confused:

no, there isn’t. this is simply the way C++ works.

(about C/C++: the trade-off for these kinds of restrictions is a massive boost in performance.)

[quote author=“damian”]no, there isn’t. this is simply the way C++ works.

(about C/C++: the trade-off for these kinds of restrictions is a massive boost in performance.)[/quote]
Did some more research and found out that c++ indeed doesn’t support this sort of event handeling. I’ve found something called ofxinteractiveobjects which seem to support my idea, i’ll look into that.