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, Dear god help :)
Page Index Toggle Pages: 1
Help, Dear god help :) (Read 425 times)
Help, Dear god help :)
May 8th, 2007, 9:37pm
 

 Hi all, This is probably a basic syntax issue but I am in dire need of some assistance.
 What I have been scratching my head about for the past three days involves passing global variables into a class. I think thats the right terminology?  
 For example.  Say I have a class set up to create random sized ellipses every time I hit the mouse button. done and done. But now I want to change the size of the next ellipse when I hit the '1' key. like this- I can hit the mouse button to create random sized ellipses but when I hit the '1' key, I need either the next ellipse or all ellipses from now on have a certain fixed size I assign to that key. Does not matter either way for what I need.
 I hope this makes sense.  If you need some code of mine please let me know, and I am sorry if this is a obvious solution, I'm just so frustrated  Sad
Thanks All.
Re: Help, Dear god help :)
Reply #1 - May 8th, 2007, 10:25pm
 
I got it!!!
Go me, ok heres the code:

Ball[] bBalls;
int MAX;
int ch;
float d;
color c = color(0,0,0,random(255));
int n;

void setup()
{
 size(1024, 730);
 //frameRate(25);
 background(20,10);
 smooth();

 MAX = 1000;
 ch = 0;

 bBalls = new Ball[MAX];

 for (int i=0;i<MAX;i++)
 {
   bBalls[i] = new Ball();
   bBalls[i].x = random(0, 1024);
   bBalls[i].y = random(0, 730);
   bBalls[i].vx = random(3,5);
   bBalls[i].vy = random(-4,4.25);
   bBalls[i].d = d+i;
   bBalls[i].c = color(255,0,0);
   bBalls[i].c2 = color(0,255,255);
   bBalls[i].z = i-.8 /0.08;
   bBalls[i].fil = c;
   n=i;
 }
}

void draw()
{
 for (int i=0;i<ch;i++)
 {
   bBalls[i].run();
 }
}

class Ball
{
 float x;
 float y;
 float vx;
 float vy;
 float d;
 float z;
 color fil;
 color c = color(255,0,0,random(100));
 color c2= color(0,255,255,random(100));

 void run()
 {

   if (key == '1'){
     bBalls[2].d=50;
     c = color(10,10,10,random(100));
   }
   else if (key == '2'){
     d = 10;
   }

   drawShape();
   x = x + vx;
   y = y + vy;

   if (x < 0 || x > width)
   {
     vx = -vx;  
   }
   if (y < 0 || y > height)
   {
     vy = -vy;  

   }
 }

 void drawShape()
 {
   ellipseMode(CENTER_RADIUS);
   strokeWeight(1);
   noFill();
   //fill(0,100);
   stroke(c2);
   ellipse(x, y, d, d);
   fill(fil);
   stroke(c);
   ellipse(x+z,y,d,d);

 }
}

void mousePressed()
{
 ch = ch+1;
}

---------------------------------------
The magical cure was to add the:
   
   if (key == '1'){
     bBalls[2].d=50;
   }
   else if (key == '2'){
     d = 10;

...In the void run(), the '1' key changes only the second ellipse, while the '2' key will change all of them.
Good times.
Page Index Toggle Pages: 1