We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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!
Answers
This doesn't make any sense:
The
mousePressed
variable is set on the same thread that thedraw()
function is called. In other words, this variable will never change during a call todraw()
, so you've created an infinite loop here.You should probably just use the
mousePressed()
andmouseReleased()
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
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?
You would need a
mousePressed()
function at the sketch level that calls themousePressed()
function of yourTree
variable.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 yourTree
class that does the correct thing every frame.Fixed it, was mistake on my part.
Fixed the order of things
Thanks again!