Class question

Hi everyone

I am new in OF, I follow the tutorials in OF wiki “Ooops! = Object Oriented Programming + Classes”, those are
pretty clear to understand how create classes, but I have two questions.

  1. How can I create a class (draw the ball) from events, for example by mouse pressed or touch event on iOS ?
  2. How cam I delete the class from event, delete the ball ?

I have experience in Action Script and Lingo, and I am so confused, becouse I dont understand how
remove the class that I create.

Please help!

Thanks

Hi everyone

I am new in OF, I follow the tutorials in OF wiki “Ooops! = Object Oriented Programming + Classes”, those are
pretty clear to understand how create classes, but I have two questions.

  1. How can I create a class (draw the ball) from events, for example by mouse pressed or touch event on iOS ?
  2. How cam I delete the class from event, delete the ball ?

I have experience in Action Script and Lingo, and I am so confused, becouse I dont understand how
remove the class that I create.

Please help!

Thanks

you have to do a pointer to an object like this

  
ofBall *myBall;  

and then add to the mousePressed method something like

  
myBall = new Ball(...);  

and remove the ball on lets say keyPressed with something like

  
delete myBall;  

to create more than one Ball you have to create a vector or array of balls.
look at the and of the tutorial:
http://wiki.openframeworks.cc/index.php?title=Ooops%21-%3D-Object-Oriented-Programming-%2B-Classes#make-objects-from-your-own-Classes—part-3

Hi benben

Thanks for your answer!

I try this, but I dont understand how to use the draw() method and update() method, I am drawing the ball
in the draw() method of Ball class,

void Ball::draw()
{
ofSetColor(200, 200, 200);
ofCircle(x, y, dim);
}

then if I dont call the draw method in the draw() method of the testAPP class, the program draw noting, but if I call the draw() method of the Ball class the app crash.

void testApp::draw(){

myBall->draw();

}

I understand how pointer a class and how create a new instance of this class, but relation of draw and update method inter classes, is hard to me

it would be better if you post the complete code of testApp.h and testApp.cpp, so we can see what you did wrong.

here is an example with a vector of balls. on left click a ball is created, on right-click the last created ball is removed. (because of the pop_back())

testApp.h

  
#ifndef _TEST_APP  
#define _TEST_APP  
  
#include "ofMain.h"  
#include "ofBall.h"  
  
class testApp : public ofSimpleApp{  
  
   public:  
  
      void setup();  
      void update();  
      void draw();  
	  void mouseReleased(int x, int y, int button);  
  
  
   private:  
  
	 	vector<ofBall*> myBall;  
};  
  
#endif  
  
  

testApp.cpp

  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){  
  
	ofBackground(0,0,0);  
	ofSetFrameRate(30);  
	ofSetWindowTitle("balls - class example_4");  
  
	ofEnableSmoothing();  
	ofNoFill();  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
  
		for (int i = 0; i < myBall.size(); i++){  
			myBall[i]->update();  
		}  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
  
		for (int i = 0; i < myBall.size(); i++){  
			myBall[i]->draw();  
		}  
}  
  
void testApp::mouseReleased(int x, int y, int button)  
{  
    if(button == 0)  
    {  
        int dim = 10+(ofRandom(0,10)*10);  
        myBall.push_back(new ofBall(ofRandom(0, ofGetWidth()),ofRandom(0, ofGetHeight()),dim));  
    }  
    else if (button == 2)  
    {  
        if(myBall.size() > 0)  
        {  
            myBall.pop_back();  
        }  
    }  
}  
  

first some lingo: a class is a generic prototype, when you actually use it to create a ball or anything else you are creating an object.

first answer: if you only need to create a single object from your class, you’ll probably want to declare it globally and use your mouse pressed event to actually render it:

  
  
//in testApp.h  
Ball myBall;  
...  
//in testApp.cpp  
void testApp::draw(){  
...  
   myBall.draw(); //where Ball.draw() i.e. draws the ball at Ball.x, Ball.y  
...  
}  
...  
void testApp::mousePressed(int x, int y, int button){  
...  
   myBall.x = x;  
   myBall.y = y;  
   //this way when you press mouse myBall will be drawn at mouse coords  
...  
}  
  

if you need to handle more Ball objects you’ll probably want to learn about arrays and vectors.

second answer: again, if you need to work with a single Ball you could simply move it out of the screen, if you’re handling more you’ll want to learn about vectors and push_back() and pop_back() functions.

One quick point:

  
  
        if(myBall.size() > 0)    
        {    
            ofBall* toBeDeleted = myBall.back(); // get the last ptr  
            delete toBeDeleted; // free its memory  
            myBall.pop_back();    
        }    

I merged the two identical threads. nogram, please refrain from doubleposting in the future.

benben and naus3a post really good examples.

Check out the Basic Particle System section in the wiki page, they explain how to create particles from a Particle class.

http://wiki.openframeworks.cc/index.php?title=Basic-particle-system

Then you can move the create_particles() to touchDown() or mousePressed(), to delete the particles check they use a C++ list container, check out the docs http://www.cplusplus.com/reference/stl/list/ you can use erase, and pop methods to delete the particles

Hope that helps!

Hi guys!!

Thanks a lot for your help!

I did not know about vector and list functions, finally I understood.

I am wondering, is posible to create a whole scene with this method, or are there other way for this?
I mean, create a loading scene, instruction scene and game scene, and be able to navegate through them.