开发者

Shape manipulation with openFrameworks

I'm a openFrameworks newbie. I am learning basic 2d drawing which is all great so far. I have drawn a circle开发者_如何转开发 using:

ofSetColor(0x333333);
ofFill;
ofCircle(100,650,50);

My question is how do I give the circle a variable name so that I can manipulate in the mousepressed method? I tried adding a name before the ofCircle

theball.ofSetColor(0x333333);
theball.ofFill;
theball.ofCircle(100,650,50);

but get I 'theball' was not declared in this scope error.


As razong pointed out that's not how OF works. OF (to the best of my knowledge) provides a handy wrapper to a lot of OpenGL stuff. So you should use OF calls to effect the current drawing context (as opposed to thinking of a canvas with sprite objects or whatever). I usually integrate that kind of thing into my objects. So lets say you have a class like this...

class TheBall {

protected:

    ofColor col;
    ofPoint pos;

public:

    // Pass a color and position when we create ball
    TheBall(ofColor ballColor, ofPoint ballPosition) {
        col = ballColor;
        pos = ballPosition;
    }

    // Destructor
    ~TheBall();

   // Make our ball move across the screen a little when we call update
   void update() { 
       pos.x++;
       pos.y++; 
   }

   // Draw stuff
   void draw(float alpha) {
       ofEnableAlphaBlending();     // We activate the OpenGL blending with the OF call
       ofFill();                    // 
       ofSetColor(col, alpha);      // Set color to the balls color field
       ofCircle(pos.x, pos.y, 5);   // Draw command
       ofDisableAlphaBlending();    // Disable the blending again
   }


};

Ok cool, I hope that makes sense. Now with this structure you can do something like the following

testApp::setup() {

    ofColor color;
    ofPoint pos;

    color.set(255, 0, 255); // A bright gross purple
    pos.x, pos.y = 50;

    aBall = new TheBall(color, pos);

}

testApp::update() {
    aBall->update() 
}

testApp::draw() {
    float alpha = sin(ofGetElapsedTime())*255; // This will be a fun flashing effect
    aBall->draw(alpha)
}

Happy programming. Happy designing.


You can't do it that way. ofCircle is a global drawing method and draws just a circle.

You can declare a variable (or better three int for rgb - since you can't use ofColor as an argument for ofSetColor) that store the color for the circle and modify it in the mousepressed method.

Inside the draw method use your variables for ofSetColor before rendering the circle.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜