Where to use ofSleepMillis() and how to delete objects from an array

This is the code in my loop:

void loop() {
    for ( int i = 0; i < anzahl; i = i+1 ) {
        verschiebeZiel(presents[i]);
        presents[i]->move();
        presents[i]->draw();

        float distX = abs(grantelbarts[i]->getX() - presents[i]->getX());
        float distY = abs(grantelbarts[i]->getY() - presents[i]->getY());
        float distanz = sqrt(distX*distX + distY*distY);
        if ( distanz < 20){
            presents[i]->dissapear();


            float rnd = ofRandom(1);
            if (rnd < 0.3) {
                ofDrawBitmapString("Meins!",(ofGetWindowWidth()/2)-30,(ofGetWindowHeight()/2)-50);
                ofSleepMillis(1000);
            } else if (rnd > 0.3 && rnd < 0.6) {
                ofDrawBitmapString("Ha-ha-ha!",(ofGetWindowWidth()/2)-30,(ofGetWindowHeight()/2)-50);
                ofSleepMillis(1000);
            } else if (rnd > 0.6) {
                ofDrawBitmapString("Ich liebe Geschenke!",(ofGetWindowWidth()/2)-40,(ofGetWindowHeight()/2)-50);
                ofSleepMillis(1000);
            }
            

        }


    }

    for ( int i = 0; i < anzahl; i = i+1 ) {
        grantelbarts[i]->draw();
        ofSetColor(255, 0, 0);
        ofDrawBitmapString("Boese Weihnachten!",(ofGetWindowWidth()/2)-65,(ofGetWindowHeight()/2)-100);

    }


}`
  1. Problem
    Where ‘presents[i]->dissapear();’ only gives the presents a position far away (x=100000, y=100000). How would i implement a “delete” item from array into here?

  2. Problem
    Even though the text in the BitStrings is practically before the “ofSleepMillis”, the program sleeps first, and then the text is shown. Why?

Thanks for all answers.

 if ( distanz < 20){
            presents[i]->dissapear();
// delete the object (assumes you allocated it with **new)
            delete presents[i];
// remove it from the vector
            presents.erase(presents.begin()+i);
                // now i will be wrong and you should decrement 1, or do a reverse loop and start at end of vec

Rendering to the screen only happens when ::draw(){} finishes. So after 1second only you will see the text. Use a different strategy, mark a member boolean before the for cycle, if presents are gone mark it true, after the draw cycle (so before your for loop), call your text, or something of the likes

1 Like

thanks, will try that !