FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   accessing variables in classes
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: accessing variables in classes  (Read 261 times)
magigale


accessing variables in classes
« on: May 28th, 2004, 1:42pm »

How do you access a variable, that's in a class further down the page?
 
Here's the code i'm working on
 
/*
by matt gale
www.room106.co.uk/gale
*/
 
void loop()
{
  background(0); // background
  noStroke();
  smooth();
  rotateX(TWO_PI/360*30);
  translate(100,0);
   
  if (txt==1)
  {
    fill(255);
    textMode(ALIGN_CENTER);
    text("use arrow keys to control ship",100,130);
    text("'e' key to transfer fuel from energy",100,150);
    text("touch me to continue",100,190);
    grav=40;
    fuel=100;
    energy=100;
    time=0;
  }
   
  fill(255);
  text(timeD,170,20);
  time=time+0.03;
  if (time>1 && energy>0)
  {
    timeD=timeD+1;
    time=0;
  }
   
   
   
  for (int i=0;i<10;i++)
  {
    myBalls[i].run();
  }
  for (int i=0;i<fb;i++)
  {
    myFuel[i].run();
  }
   
  fill(100,100,200);
  rect(4,4,7,102);
   
  fill(0,255,0);  // fuel, if fuel is full
  if (fuel<50)    // fuel, if fuel is less than 50
  {
    fill(255,255,0);
  }
  if (fuel<25)  // fuel, if fuel is less than 25
  {
    fill(255,0,0);
  }
 
  rect(5,5,5,fuel); //fuel
   
  fill(100,100,200);
  rect(189,4,7,102);
   
  fill(0,255,0);
  if (energy<50)
  {
    fill(255,255,0);
  }
  if (energy<25)
  {
    fill(255,0,0);
  }
   
  rect(190,5,5,energy); // energy
  translate(xPos,grav,0);   // position of triangle
   
  for (int i=0;i<fb;i++)
  {
    if(xPos > hitX/*this is where the problem is*/ -10  && xPos < hitX +10 && grav > hitY +10 && grav < hitY -10 )
    {
 fuel=fuel+50;
    }
  }
   
  fill(100,100,200);
   
  if (keyPressed == false && energy>0)
  {
    drawShip();
    xT=0;
  }
 
  if (keyPressed)
  {
    if (key == DOWN && energy>0)
    {
 drawShip();
    }
  }
     
 
  if (keyPressed)      // thrust
  {
    if (key == DOWN && energy>0)
    {
 if (fuel>0)
 {
 drawShip();
 xT=2;
 fuel=fuel-0.5;
 drawFlames();  
 }
    }
    else
    {
 xT=0;
    }
  }
   
  if (keyPressed)    // left key
  {
    if (key == LEFT && energy>0)
    {
 xPos=xPos-1;  
 rotateY(TWO_PI/360*315);
 drawShip();
    }
     
  }
   
  if (keyPressed)    // right key
  {
    if (key == RIGHT && energy>0)
    {
 xPos=xPos+1;
 rotateY(TWO_PI/360*45);
 drawShip();
    }
  }
   
  if(xPos==0)    // limit left direction
  {
    xPos=1;
  }
   
  if(xPos==200)  // limit right direction
  {
    xPos=199;
  }
   
  if (grav==160 && energy==0) // limit hight (down)
    {
 grav=159;
    }
     
  if (grav==160)
    {
 grav=159;
 energy=energy-0.5; // takes off energy if your at bottom of screen
    }
  else
    {
 grav=grav+1-xT;
    }
     
  if (grav<0)  // limit hight (up)
    {
 grav=1;
    }
   
  if (keyPressed) // energy button
  {
    if (key == 'e' && energy>0 || key == 'E' && energy>0)
    {
 drawShip();
 energy=energy-1;
 fuel++;
    }
  }
     
  if (energy<0)  // stops energy becoming less than 0
  {
    energy = 0;
  }
   
  if (fuel>100)  // stops fuel going over 100
  {
    fuel = 100;
  }
   
 if (energy==0)  // if there is no energy, then 'game over' is displayed
  {
    fill(255);
    text("game over",100-xPos,-5);
    text(timeD,100-xPos,10);
  }
   
}
 
class Ball
{
  float x;
  float y;
   
  void run()
  {
    drawStar();
     
    y=y+1;
    if (y>200)
    {
 y=0;
 x=random(-100,300);
    }
  }
 
  void drawStar()
  {
    ellipseMode(CENTER_RADIUS);
    fill(255,255,255,200);
    ellipse(x,y,2,2);
  }
}
 
 class FuelCell
{
  float hitX;
  float hitY;
   
  void run()
  {
    drawStar();
     
    hitY++;
    if (hitY>200)
    {
 hitY=0;
 hitX=random(10,190);
    }
  }
 
  void drawStar()
  {
    ellipseMode(CENTER_RADIUS);
    fill(50,50,200,200);
    ellipse(hitX,hitY,4,4);
  }
}
 
 
TomC

WWW
Re: accessing variables in classes
« Reply #1 on: May 28th, 2004, 1:49pm »

Can you try posting a shorter example, so we know what it is you're stuck on.
 
In general, Java won't mind *where* in your program something is specified, or declared, so long as it is in scope.
 
So I can do:
 
Code:

 
void setup() {
  obj.doSomething();
  println(obj.value);
}
 
Obj obj = new Obj();
 
class Obj {
  int value = 10;
  void doSomething() {
    println("it worked!");
  }  
}
 

 
Is that what you meant?
 
 
Pages: 1 

« Previous topic | Next topic »