2011-02-25 24 views
1

我是一个openFrameworks新手。我正在学习基本的2D绘图,迄今为止都非常棒。我画用圆圈:openFrameworks的形状操作

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

我的问题是我怎么给圆一个变量名,这样我可以在方法的mousePressed操纵?我试过ofCircle

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

,但得到我theball“在此范围错误未声明前添加一个名字。

回答

4

你不能那样做。 ofclear是一种全球绘图方法,只画一个圆圈。

你可以为rgb声明一个变量(或者更好的三个int,因为你不能使用ofColor作为ofSetColor的参数),它存储圆的颜色并在mousepressed方法中修改它。

在draw方法内部,在渲染圆之前使用ofSetColor的变量。

+0

实际上这是这种情况下的“最佳实践”,但如果他真的想要,他可以做什么rykardo写在其他答案和创建具有相同的名称,如OF功能的方法。 – ben 2011-10-10 12:51:44

5

由于razong指出,这不是如何工作。 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) 
} 

快乐编程。 快乐的设计。

相关问题