Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ openFrameworks中的形状操作_C++_Openframeworks - Fatal编程技术网

C++ openFrameworks中的形状操作

C++ openFrameworks中的形状操作,c++,openframeworks,C++,Openframeworks,我是个新手。我正在学习基本的2d绘图,到目前为止都很棒。我已经用下列方法画了一个圆: ofSetColor(0x333333); ofFill; ofCircle(100,650,50); 我的问题是如何给圆起一个变量名,这样我就可以在mousepressed方法中进行操作了?我试着在圆圈前面加上一个名字 theball.ofSetColor(0x333333); theball.ofFill; theball.ofCircle(100,650,50); 但是get I'theball'未在

我是个新手。我正在学习基本的2d绘图,到目前为止都很棒。我已经用下列方法画了一个圆:

ofSetColor(0x333333);
ofFill;
ofCircle(100,650,50);
我的问题是如何给圆起一个变量名,这样我就可以在mousepressed方法中进行操作了?我试着在圆圈前面加上一个名字

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

但是get I'theball'未在此范围错误中声明。

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

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


在draw方法中,在渲染圆之前,使用变量进行设置颜色的选择。

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

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


在draw方法中,在渲染圆之前,使用您的变量进行ofSetColor。

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

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)
}
快乐编程。
快乐的设计。

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

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)
}
快乐编程。 愉快的设计。

事实上,这是这种情况下的“最佳实践”,但如果他真的愿意,他可以按照rykardo在另一个答案中写的做,并创建与函数同名的方法。实际上,这是这种情况下的“最佳实践”,但如果他真的愿意,他可以像rykardo在另一个答案中写的那样,创建与函数同名的方法。