1

我是一个 openFrameworks 新手。我正在学习基本的二维绘图,到目前为止一切都很棒。我使用以下方法绘制了一个圆圈:

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

我的问题是如何给圆圈起一个变量名,以便我可以在 mousepressed 方法中进行操作?我尝试在 ofCircle 之前添加一个名称

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

但是让我在这个范围错误中没有声明'theball'。

4

2 回答 2

5

正如 razong 指出的那样,这不是 OF 的工作方式。OF(据我所知)为许多 OpenGL 的东西提供了一个方便的包装器。因此,您应该使用 OF 调用来影响当前的绘图上下文(而不是考虑带有精灵对象或其他东西的画布)。我通常将这种东西整合到我的对象中。所以假设你有这样的课程......

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
   }


};

好的,很酷,我希望这是有道理的。现在使用这种结构,您可以执行以下操作

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)
}

快乐编程。设计愉快。

于 2011-10-10T12:36:34.103 回答
4

你不能那样做。ofCircle 是一种全局绘制方法,只绘制一个圆。

您可以声明一个变量(或者对于 rgb 最好是三个 int - 因为您不能使用 ofColor 作为 ofSetColor 的参数)来存储圆的颜色并在 mousepressed 方法中对其进行修改。

在绘制圆之前,在 draw 方法中使用您的 ofSetColor 变量。

于 2011-03-01T12:27:35.133 回答