Make only parts of an object move

Hello to everyone, I am new to programming and I thought that what I wanted to do would be easy to realize but I'm having difficulties. I would like to draw kind of flowers and having their "seeds" moving up and down, what's wrong in my code?

Flower[] _flowerArr={};

void setup() {
    size(800, 600);
    smooth();
    drawAll();
    background(255);
}

void draw(){
    // background(255);
    for (int i = 0; i < _flowerArr.length; ++i) {
        _flowerArr[i].drawFlower();
        _flowerArr[i].moveFlower();
        // println(frameCount);
    }
}

void drawAll(){
    for (int i = 0; i < 50; ++i) {
    Flower newFlower = new Flower(random(width));
    _flowerArr=(Flower[])append(_flowerArr, newFlower);
    }
}

//// DEFINE CLASS  /////////////////////////////////////////////////////

class Flower {
    float x1,y1,y2,ang,v;

    Flower(float a){
        x1=a;
        y1=0;
            y2=random(100, 500);
        ang=0;
        v=0;
    }

    void drawFlower(){

        line(x1, y1, x1, y2);

        for (y1 =y1; y1 < y2; y1+=random(2, 8)) { // HOW CAN I MAKE THEM MOVING ????
            float x2 = x1+random(-10, 10);
            line(x1, y1, x2, (y1+v));
            ellipse(x2, (y1+v), 10, 10);
        }
    }

    void moveFlower(){
        v=sin(radians(ang))*50;
        // v=map(sin(radians(ang)), -1, 1, -50, +50);
        ang+=5;
    }
}

Also, if I activate the background in the draw() I get basically an empty canvas, why is that? Thanks for you help.

Tagged:

Answers

  • Answer ✓

    The problem is in line 43, where you set y1 = y1 to begin the loop. If y1 is never reset it will always stay >y2. Change y1 = y1 to y1 = 0. Then you can use the background(255) line.

  • Fantastic, it was so simple, thank you!!

Sign In or Register to comment.