Quantcast
Viewing all articles
Browse latest Browse all 40524

Trigger function randomly with mousePressed

If I understood your question, there are many ways to do so. Solely rely on randomness, can be "problematic". For exemple, if you do:

void ofApp::mousePressed(int x, int y, int button){
    
    if (ofRandomuf() < 0.1f) {
        // your event/function
    }

}

You are giving the user 10% change for the event to happen. (0.5f for 50%, 0.75f for 75% and so on).

But, this also means that you can press the mouse a thousand times and nothing happens... Therefor, it's better to have more control of how many inputs the user did, like so:

int numberOfInputs = 0, limitInputs = ofRandom(15, 30);

void ofApp::mousePressed(int x, int y, int button){
    
    numberOfInputs++;
    
    if ((ofRandomuf() < 0.1f) || numberOfInputs >= limitInputs) {
        
        // your event/function
        
        numberOfInputs = 0;
        limitInputs = ofRandom(15, 30);
    
    }

}

By doing this, you guarantee that no matter what, after between 15 to 30 inputs, the event will trigger. The user will still have a feeling of randomness, but you have a ceiling.

There are many other ways to approach this, but I hope this is useful to you.


Viewing all articles
Browse latest Browse all 40524

Trending Articles