nako
YaBB Newbies
Offline
Posts: 2
Re: drag and release
Reply #1 - Feb 27th , 2006, 7:39pm
sorry guys, managed to work it out. I am posting the code in case it is useful for someone. ----- int arena = 420; int arenatop = 90; int arenaleft = 90; int num = 2; Unit[] units = new Unit[num]; void setup() { size(800, 600); noStroke(); noSmooth(); units[0] = new Unit(arena+arenaleft-40, arenatop+40, units, 0); units[1] = new Unit(arena+arenaleft-40, arenatop+80, units, 1); framerate(15); } void draw() { background(100); stroke(150); fill(0); rect(arenatop, arenaleft, arena, arena); noStroke(); for(int i=0; i<num; i++) { units[i].update(); units[i].draw(); } } void mousePressed() { for(int i=0; i<num; i++) { units[i].pressed(); } } void mouseReleased() { for(int i=0; i<num; i++) { units[i].released(); } } class Unit { // Screen values float xpos, ypos; float old_xpos, old_ypos; float vely, velx; int size = 20; boolean over = false; boolean move = false; Unit[] friends; int me; // Constructor Unit(float x, float y, Unit[] others, int id) { xpos = x; ypos = y; old_xpos = x; old_ypos = y; friends = others; me = id; } void update() { if(move) { old_xpos = xpos; old_ypos = ypos; ypos = mouseY; xpos = mouseX; velx = (xpos - old_xpos)/2; vely = (ypos - old_ypos)/2; } ypos += vely; xpos += velx; if( ypos + size/2 > (arena+arenatop) ) { ypos = (arena+arenatop) - size/2; vely *= -1; } if( ypos < arenatop+size/2 ) { ypos = arenatop+size/2; vely *= -1; } if( xpos + size/2 > (arena+arenaleft) ) { xpos = (arena+arenaleft) - size/2; velx *= -1; } if( xpos < arenaleft+size/2 ) { xpos = arenaleft+size/2; velx *= -1; } if((over() || move) && !otherOver() ) { over = true; } else { over = false; } } // Test to see if mouse is over this spring boolean over() { float disX = xpos - mouseX; float disY = ypos - mouseY; if(sqrt(sq(disX) + sq(disY)) < size/2 ) { return true; } else { return false; } } // Make sure no other units are active boolean otherOver() { for(int i=0; i<num; i++) { if(i != me) { if (friends[i].over == true) { return true; } } } return false; } void draw() { if(over) { fill(153); } else { fill(255, 255, 255, 50); } ellipse(xpos, ypos, size, size); } void pressed() { if(over) { move = true; } else { move = false; } } void released() { move = false; } } ----