2011-02-25 62 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) 
} 

快樂編程。 快樂的設計。