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 › Help with removing array elements. *Idiot Beginner
Page Index Toggle Pages: 1
Help with removing array elements. *Idiot Beginner (Read 294 times)
Help with removing array elements. *Idiot Beginner
Jan 10th, 2009, 12:01am
 
Hello, I'm new to programming and processing. Not sure if this is a silly question. I would be very grateful if anyone can helpppp?

What I am trying to do is have one wiimote adding random circles to a screen and one wiimote subtracting the random circles from the screen. I have got both wiimotes talking to processing and have one creating circles, I am stuck on trying to remove them with the other controller..... Here is the code.....





import oscP5.*; // imports OpenSoundControl libraries, these are used to allow the WiiMote to talk to Processing
import netP5.*;

WiiRemote wiiRemote; // imports WiiRemote class
WiiRemoteII wiiRemoteII; //imports second wiimote class
ArrayList balls; //imports ball class as an array


void setup()
{
 size (screen.width, screen.height); // sets window size to full screen
 frameRate(30); // sets frame rate to 30 fps
 noStroke(); // removes lines from around objects
 smooth(); // ensures object curves are smooth
 background(0); // sets background colour as black
 wiiRemote = new WiiRemote(); // creates new WiiRemote object
 wiiRemoteII = new WiiRemoteII(); // creates second remote object
 balls = new ArrayList(); // creates new ball array

}


void draw()
{
 float x = random(1,width);
 float y = random(1,height);

 float bounce2 = ((wiiRemoteII.acc.x + wiiRemoteII.acc.y + wiiRemoteII.acc.z)/3.0)-100;
 float bounce = ((wiiRemote.acc.x + wiiRemote.acc.y + wiiRemote.acc.z)/3.0)-100;



 if(bounce < 10.0 || bounce > 40.0)
 {
   balls.add(new Ball(x,y,30));
 }


 int del = int(random(balls.size()));


 for (int i = balls.size()-1; i >= 0; i--) {
   Ball ball = (Ball) balls.get(i);

   if (bounce2 < 10.0 || bounce2 > 40.0)
   {
     balls.remove(del);
   }
   println(del);
 }

 }

 void mousePressed()
 {
 background(0);
 }



class Ball
{
 float x,y;
 color c;
 float mass = 10.0;


 Ball(float inx, float iny, float inmass)
 {
   x = inx;
   y = iny;
   mass = inmass;
   
   c = color(random(255), random(255), random(255));
   fill(c);
   ellipse(x,y,mass,mass);
 }

}
Re: Help with removing array elements. *Idiot Begi
Reply #1 - Jan 10th, 2009, 1:02am
 
Would this address some of your problems?


1) You need to redraw each frame or else nothing gets
erased.
2) No need to delete everything on each loop. Also, check if array is empty or not.

Code:


// *snip*


void draw()
{
 background(0); // wipe everything

 float x = random(1,width);
 float y = random(1,height);

 float bounce2 = ((wiiRemoteII.acc.x + wiiRemoteII.acc.y + wiiRemoteII.acc.z)/3.0)-100;
 float bounce = ((wiiRemote.acc.x + wiiRemote.acc.y + wiiRemote.acc.z)/3.0)-100;




 if(bounce < 10.0 || bounce > 40.0)
 {
   balls.add(new Ball(x,y,30));
 }



 // draw all the balls
 for (int i = balls.size()-1; i >= 0; i--) {  
  Ball ball = (Ball) balls.get(i);
  ball.draw();
 }

if(balls.size() > 0){
 int del = int(random(balls.size()));
 println(del);
 if (bounce2 < 10.0 || bounce2 > 40.0)
   {
     
     balls.remove(del);
   }
 }
}

void mousePressed()
{
 background(0);
}



class Ball
{
 float x,y;
 color c;
 float mass = 10.0;


 Ball(float inx, float iny, float inmass)
 {
   x = inx;
   y = iny;
   mass = inmass;

   c = color(random(255), random(255), random(255));
   fill(c);
   ellipse(x,y,mass,mass);
 }
 
 void draw(){
  fill(c);
   ellipse(x,y,mass,mass);
 }
 

}
Re: Help with removing array elements. *Idiot Begi
Reply #2 - Jan 10th, 2009, 10:49am
 
Thats amazing, I'm such an idiot.

Thank you very much!!
Re: Help with removing array elements. *Idiot Begi
Reply #3 - Jan 10th, 2009, 5:35pm
 
You are welcome, and no, you're not Smiley

Good luck with your project!
Page Index Toggle Pages: 1