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 › moving hit-area not consistent
Page Index Toggle Pages: 1
moving hit-area not consistent (Read 293 times)
moving hit-area not consistent
Jul 15th, 2008, 11:33pm
 
I've got a simple class which makes circles and lets them float (based on one of the examples online). When clicked on a circle, I want to start a function but I just can't seem to get the hitarea consistent. Sometimes it returns values, sometimes it doesn't. Any suggestions? How can I fix this? Here is some test code. When a hit is performed, the movement should stop and you should get some feedback in the console:

import java.util.ArrayList;

// list that holds circles
ArrayList circles;
// number of circles to create initially
int circleCount = 5;
// max and min size of circles2
float maxSize = 30;
float minSize = 20;
// max speed for circles
float speed = 0.4;
float easing = 0.05;
float targetX, targetY;
boolean check = false;

void setup() {
 size(1000, 700);
 background(255);
 noStroke();
 smooth();

 // fill the circles list with circle objects
 circles = new ArrayList();
 for (int i=0; i<circleCount; i++) {
   // Orange
   color c = color( 255, 128, 0, 150);
   // add the circle to circles list
   circles.add(new Circle( 7, 7, 15, c));
 }
}

void draw() {
 background(255);

 // loop through the circle objects in circles list
 for (int i=0; i<circles.size(); i++) {
   Circle circle = (Circle) circles.get(i);
   // move circles
   if(check == false){
   circle.move();
   }else{
     println("stop movement");
   }
   // draw circles
   circle.display();
 }
 
}


class Circle {
 // x and y position, radius, x and y velocities
float x, y, r, vx, vy;

 // circle color
 color c;

 Circle(float _x, float _y, float _r, color _c) {
   x = _x;  
   y = _y;  
   r = _r;  
   c = _c;
   vx = random(-speed, speed);
   vy = random(-speed, speed);

 }

 // draw circle
 void display() {
   fill(c);
   ellipse(x, y, r*2, r*2);
 }

 // move circle
 void move() {
   // bounce against the sides
   if (x - r < 0) {
     x = r + 1;
     vx *= -1;
   }  
   else if (x + r > width) {
     x = width - r - 1;
     vx *= -1;
   }
   if (y - r < 0) {
     y = r + 1;
     vy *= -1;
   }  
   else if (y + r > height) {
     y = height - r - 1;
     vy *= -1;
   }
 
   // add velocities to position
   x += vx;
   y += vy;
   
  if (mousePressed) {
  if ((mouseX > x && mouseX < (x + (r*3))) && (mouseY > x && mouseY < (y + (r*3)))) {
  check = true;
  println("This is x " + x + " This is y" + y);

  }
 }
 
 }
}

Re:  moving hit-area not consistent
Reply #1 - Jul 16th, 2008, 4:07am
 
could it be:

[quote]
...
   
  if (mousePressed) {  
  if ((mouseX > x && mouseX < (x + (r*3))) && (mouseY > x && mouseY < (y + (r*3)))) {
  check = true;
  println("This is x " + x + " This is y" + y);

  }
 }
 
 }
}
[/code]

also, the import statement isn't necessary.
Page Index Toggle Pages: 1