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.
IndexProgramming Questions & HelpSyntax Questions › Some questions about moving circles from beginner
Page Index Toggle Pages: 1
Some questions about moving circles from beginner (Read 599 times)
Some questions about moving circles from beginner
Feb 9th, 2010, 5:35pm
 
My questions are in the code:

//Declaring two variables to act as my x-coordinates for my two circles.
int x1=50;
int x2=750;

void setup() {
 size(800,600);
}

void draw() {
 background(50);

 noStroke();
 ellipse(x1,300,50,50);
 ellipse(x2,300,50,50);

 x1=x1+1;
 x2=x2-1;

 //I'll make an if-statement to turn the circles red when their x-coordinates are the same. Is typing in the two ellipse functions necssary?
 if (x1==x2) {
   noStroke();
   fill(125,0,0);
   ellipse(x1,300,50,50);
   ellipse(x2,300,50,50);
 }
}

/*I'm wanting a line to be created whenever the mouse is clicked. This happens, yet it disappears right after I see it on the screen. I know
this is because my draw function keeps repeating. One solution is to put my background function in to setup(), yet then my two circles leave
a trail. How do I fix this? */
void mousePressed() {
 strokeWeight(4);
 stroke(255);
 line(400,50,400,250);
}

-------------------------------------------------------------------------

  My second question is about trying to create a program that moves a ball back and forth on the screen.

int x1=50;

void setup() {
 size(800,600);
}

void draw() {
 background(75);
 
 ellipse(x1,300,80,80);
 x1=x1+3;
 
 if (x1==750) {
   x1=x1-6;
 }
}

//This seems to me like it would work. I'm sending the ball from x=50 to x=750 and trying to get it back again. How do I achieve this?

Thanks very much!
Re: Some questions about moving circles from beginner
Reply #1 - Feb 9th, 2010, 5:50pm
 
Part 1. See comments in code. Comments about the line are numbered.

Quote:
int x1=50;
int x2=750;

// 1) Define a global boolean to track if the mouse was ever pressed.
boolean mouseWasClicked = false;

// 2) When the mouse is pressed...
void mousePressed() {
  mouseWasClicked = true; // 3) Update the boolean to remember that fact.
}

void setup() {
  size(800,600);
}

void draw() {
  background(50);

  noStroke();
  ellipse(x1,300,50,50);
  ellipse(x2,300,50,50);

  x1=x1+1;
  x2=x2-1;

  if (x1==x2) {
    noStroke();
    fill(125,0,0); // The fill color will stay red until you change it again.
    ellipse(x1,300,50,50); // This will cover up both of them in the instance when they overlap.
    // ellipse(x2,300,50,50); // But there's no need to draw a second one here. They're at the same place.
    // And this block of code is only ever executed ONCE.
  }
  
  if( mouseWasClicked){ // 4) If the mouse was pressed...
    strokeWeight(4);
    stroke(255);
    line(400,50,400,250); // 5) draw the desired line.
  }
  
}

Re: Some questions about moving circles from beginner
Reply #2 - Feb 9th, 2010, 5:56pm
 
Part 2.

Quote:
int x1=50;

void setup() {
 size(800,600);
}

void draw() {
 background(75);
 
 ellipse(x1,300,80,80);
 
 x1=x1+3; // x1 starts at 50, then 53, 56, 59, 62, ...
 // eventually it gets to 746, 749, 752...
 
 if (x1==750) { // But it never EQUALS 750!
   x1=x1-6; // this line never executes, as the condition is never met.
 }

 if (x1 > 750) { // Even with the right condition,
   x1=x1-6; // this still isn't going to do what you want.
 } // Run it and see!

 // I'll post some code showing you how you can do it next. Hold on.
 
}

Re: Some questions about moving circles from beginner
Reply #3 - Feb 9th, 2010, 6:02pm
 
Part 2, working.

Quote:
int x1 = 50;
int x_step = 3; // A new variable that determines how much x1 changes.

void setup() {
  size(800,600);
}

void draw() {
  background(75);
  ellipse(x1, 300, 80, 80);
  x1 = x1 + x_step; // This line is important. Look.

  if (x1 > 750) { // when we get close to the right side...
    x_step = -3; // change direction!
  }

  if (x1 < 50) { // and when we get close to the left side...
    x_step = 3; // change direction again!
  }

}

Re: Some questions about moving circles from beginner
Reply #4 - Feb 9th, 2010, 6:22pm
 
Thanks TfGuy! I feel like a dummy about the whole x1 never being 750 thing. Haha. I'll be sure to review this code a lot until I fully grasp it  Smiley
Page Index Toggle Pages: 1