Ability to store first and last value during the start and end of event

Beginner here :)

This is is a part of code I plan to implement inside Class, so I've tried to avoid using void mousePressed(){} and instead create my own function, and yet it doesn't do what I would've expected it to do. So I tried to strip it to bare bones, and see what's up.

 
    int R=1, A=255; //Ellipse radius and Alpha value
    IntList PosX = new IntList(1), PosY = new IntList(1); //Values placeholder
    int inPosX, inPosY, CountBegin, strength;
    IntList Count = new IntList(1);//Values placeholder
    
    void setup() {
      size(360, 360);
    }
    
    void draw() {
      background(51);
      if (mousePressed) {//Added if(){} so that I will only be able to enter the loop once I press the mouse button
                         //Because otherwise I fear It'll get executed once, record the value at index(0) at the start of the program
                         //The test will get evaluated as false and it'll exit the loop
        for (int i=0; mousePressed; i=1) {
          //My understanding was that the moment the event get triggered
          //The initial value will get placed at index (0), while the rest will get constantly updated at index (1)
          //until the test get evaluated as false, and event ends, but the values will get recorded.
          
          PosX.set(i, mouseX);   PosY.set(i, mouseY);    Count.set(i, millis());
          inPosX=PosX.get(0);    inPosY=PosY.get(0);     CountBegin=Count.get(0);
          
          fill( 0, 121, 184, A);
          ellipse(inPosX, inPosY, R, R);
          R++; 
          A=constrain(A--, 0, 255);
    
          strength=millis()-CountBegin;
        }
      }
      println(CountBegin);  //And this get's me 0.00's, but even if I place it inside if(){} or for(){}
                            //and tell it to print a predefined value, it won't even execute the command.
                            //Meaning it never executes those blocks of code
    }
    

Expectation: Inside the eventual working Class object, I'd announce it's variables as placeholders, with the press of the mouse button it'll 'charge' and display the progress of it's charging, record its charge strength, call the whole function void born(){}, upon release it'll pass the strength value to another block of code and execute, in the future, void grow(){}, the strength value would indicate for either how long it'll live or how big it'll be, so I thought I'd divide it's death in void wither(){}, where it'll fade and void die(){}, where I'd remove it from program.

Reality: It just doesn't agree with me.

Seeing as I'm still stuck in the beginning stage I'm looking for help or any alternatives to perform same task of recording first instance of a value and it's last.

Thank you!

Tagged:

Answers

  • This doesn't make any sense:

    for (int i=0; mousePressed; i=1) {
    

    The mousePressed variable is set on the same thread that the draw() function is called. In other words, this variable will never change during a call to draw(), so you've created an infinite loop here.

    You should probably just use the mousePressed() and mouseReleased() functions for this. You said you wanted to use a class instead, but that doesn't make a ton of sense either: nothing is stopping you from using a class and the mouse event functions.

  • KevibWorkman, using function mousePressed() mouseReleased() was my first go-to

    class Tree {
      int inPosX=0, inPosY=0;
      int rootCount=9, branchCount=5;
      int R=1, A=255;
      int strength=0, lifeLenght, CountBegin=0;
    
      void mousePressed() {
        inPosX=mouseX;
        inPosY=mouseY;
        CountBegin=millis();
      }
    
      void born() {
        fill( 0, 121, 184, A);
        ellipse(inPosX, inPosY, R, R);
        R++; 
        A=constrain(A--, 0, 255);
      }
    
      void mouseReleased() {
        strength=millis()-CountBegin;
      }
      void grow() {
      }
      void wither() {
      }
      void die() {
      }
    }
    

    But I wasn't successful in making it work from main sketch. At least it doesn't crash this time around. It gives me whatever value I assign to CountBegin at the start of the Class, so mousePressed(){} inside Class never even occurs, so how do I call it properly?

    Tree tree;
    
    void setup() {
      size(360, 360);
    }
    
    void mousePressed() {
      tree = new Tree();
    }
    
    void draw() {
      if (mousePressed) {
        tree.born();
        println(tree.CountBegin);
      }
      background(51);
    }
    
  • You would need a mousePressed() function at the sketch level that calls the mousePressed() function of your Tree variable.

  • void mousePressed() {
      tree = new Tree();
      tree.mousePressed();
    }
    
    void mouseReleased(){
    tree.mouseReleased();
    println(tree.strength);
    }
    

    It get's updated, why did I think this was impossible... :') Just one last question if I may, what should I use to draw and change shape while the mouse is pressed? If using while(mousePressed){tree.born} would just give me an infinitive loop.

  • From the sketch-level draw() function, you can call a function in your Tree class that does the correct thing every frame.

  • Fixed it, was mistake on my part.

    void draw() {
      background(51);
      if (mousePressed) {
        tree.born();
      }
    }
    

    Fixed the order of things

      void born() {
        A--;
        A=constrain(A, 0, 255);
        fill( 0, 121, 184, A);
        ellipse(inPosX, inPosY, R, R);
        R++; 
      }
    

    Thanks again!

Sign In or Register to comment.