We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
newbie (Read 862 times)
newbie
Nov 20th, 2009, 4:12am
 
I am new to processing but have a bit of experience with JAVA
Trying to get my head round setup() and draw()
I understand that draw() is continually looping and therefore I would place the cod to animate somthing in there.

I have create a class that animates a ball and if I put the move() method that I have created in here then everything is greate.

I want to start the ball moving when you click the screen so I have called the move() method with an onmouseRelease() function.  Howver this is called outside of draw() and will therefore only happen once as it is out side of the loop.

how do I get this call to loop within draw()

Thanks
Re: newbie
Reply #1 - Nov 20th, 2009, 4:41am
 
You should set up a global variable to register the mouse click and check against this in draw to determine if move() should be run...
Re: newbie
Reply #2 - Nov 20th, 2009, 5:01am
 
Thanks

That makes sence
Re: newbie
Reply #3 - Nov 20th, 2009, 5:06am
 
No worries.  Here's an example (I'd finished before I saw your response) to clarify further:

Code:
// declare global variables
int counter;
boolean mouseIsPressed;  // defaults to false

void setup() {
 // size should always come first in setup:
 size(200,100);
 // assign values to variables in setup if you're
 // likely to need to reset your sketch - i.e. by calling setup() again
 counter = 0;
}

void draw() {
 // draw things in a continous loop
 // If you're animating anything you're likely to want to make a call to background
 background(0);
 
// check the value of the boolean variable
 if(mouseIsPressed) {
   println("mouse pressed!  " + counter);
   counter ++;
 }
}

void mousePressed(){
 mouseIsPressed = true;
}

//  Obviously if you want something to keep running after
// a single mouse press you won't want to do this:
void mouseReleased() {
  mouseIsPressed = false;
}
Re: newbie
Reply #4 - Nov 20th, 2009, 5:10am
 
Alternatively, since you have a class, you can add a method to call in the mousePressed function. This method will set a boolean to true. The move() function checks this boolean: if false, return immediately. If true, it just does the animation.
Re: newbie
Reply #5 - Nov 20th, 2009, 5:37am
 
This is my solution

However I will try putting the boleen in the class


Code:
Spot sp; // Declare the object
Button button1,button2;
boolean testpressed = false;

void setup() {
size(100, 100);
smooth();
noStroke();
color gray = color(204);
color white = color(255);
color black = color(0);

sp = new Spot(33, 50, 30, 0.5); // Construct the object
button1 = new Button(10, 80, 10, gray, white, black);
button2 = new Button(25, 80, 10, gray, white, black);
}

void draw() {
fill(0, 15);
rect(0, 0, width, height);
fill(255);

if (testpressed == true){
sp.display();
sp.move();
}
button1.display();
button1.update();
button2.display();
button2.update();
}

void mousePressed(){
if (button1.press()== true){
testpressed = true;
}
if (button2.press()== true){
testpressed = false;
}
}

void mouseReleased(){
button1.release();
button2.release();
}
Page Index Toggle Pages: 1