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.
Page Index Toggle Pages: 1
No Global Var (Read 588 times)
No Global Var
Apr 5th, 2010, 1:10pm
 
For a project in my class we have to design a little game, but using no global variable. I'm still very new to Processing and programming in general so I can't really think of a way to do this. Since draw re-run infinitely and setup is only run once I can't find a way to make something as simple as having a square move up when a key is pressed.

A rough outline I have is that

player hero;

void setup()
{
 size(400,400);
 background(255);
 hero = new player(50,200,300);
}

void draw()
{
if ( keyPressed & (key == 'w') )
{
  hero.move();
}
}

class player {

int sSize;
int xPos;
int yPos;

player(int size_, int xPos_, int yPos_)
{
  sSize = size_;
  xPos = xPos_;
  yPos = yPos_;
  background(255);
  stroke(0);
  fill(155);
  rectMode(CENTER);
  rect(xPos,yPos,sSize,sSize);
}

void move()
{
 yPos--;
 background(255);
 stroke(0);
 fill(155);
 rectMode(CENTER);
 rect(xPos,yPos,sSize,sSize);
}

}


Is there a different way to achieve this w/o using global variable?

Also is there a way to move the square w/o having to redraw the background? like remove the previous square somehow? because the background will be a room with a random placement for the door, if draw runs the function again then the door might be in a different location.

If any clarifying needs to be done, please let me know.

Thanks!
Re: No Global Var
Reply #1 - Apr 5th, 2010, 3:19pm
 
Presumably you can have a global instance of a class  TBH - I'm not sure there's a practical alternative when working in Processing...  In which case seeing as you've already created one class you can create additional classes as appropriate then store variables as properties of these classes.  This avoids the use of global variables (e.g. booleans, strings etc - which I suspect is what you've actually been asked to avoid), whilst making them accessible between classes.

So for instance you could have a 'Level' class that stores information about the current level, such as the position of the door - which you can randomly generate when you create an instance of the class.  Re-drawing the background is the simplest way to animate things - trying to blank out just the rectangle could be done, but it's overcomplicated and you'll run into problems if it overlaps with anything else.  The level class could also include booleans to record the state of the keyboard and you could use the keyPressed() and keyReleased() methods to separate keyboard handling from the draw method.  Using booleans also has the benefit of handling multiple key-presses...
Re: No Global Var
Reply #2 - Apr 5th, 2010, 7:39pm
 
Thanks for your reply blindfish!
Fortunately the teacher allowed us to use global variable now. Indeed I think he was trying to make us avoid declaring regular primitive data type, even so I would feel weird using a global object variable if he doesn't want -any- global. Luckily he changed his mind. : )

For the second part can you clarify what you mean by attach the keyPressed & keyReleased function into the classes? I have tried, and perhaps I'm doing it wrong, but it didn't really worked out the way I anticipated.

A snippet of what I did:

player hero = new player();

void draw()
{
  hero.keyPressed();
  hero.keyReleased();
}

class player
{
  int n, s, e, w;  
 
 void keyPressed()
 {    
   //very redundent a way to reduce code possible?
   println(key);
   switch (key)
   {
     case 'w': n = 1; break;
     case 'a': w = 1; break;
     case 's': s = 1; break;
     case 'd': e = 1; break;
     case 'W': n = 3; break;
     case 'A': w = 3; break;
     case 'S': s = 3; break;
     case 'D': e = 3; break;
   }
   if (n > 0)
   {
     yPos = yPos - n*2;
   }
   if (w > 0)
   {
     xPos = xPos - w*2;
   }
   if (s > 0)
   {
     yPos = yPos + s*2;
   }
   if (e > 0)
   {
     xPos = xPos + e*2;
   }
   
   drawOut(xPos, yPos);
 }
 
 void keyReleased()
 {
   switch (key)
   {
     case 'w': n = 0; break;
     case 'a': w = 0; break;
     case 's': s = 0; break;
     case 'd': e = 0; break;
     case 'W': n = 0; break;
     case 'A': w = 0; break;
     case 'S': s = 0; break;
     case 'D': e = 0; break;
   }
 }

 void drawOut()
 {
   //draws player
  }

}
The problem I encounter is when a key is pressed the hero.keyPressed() is ran repeatedly since it's in draw. I must be implementing it wrong. What I did now it remove the keyPressed and keyReleased from the class and into a function of its own.

Is the way I did it in the example above what you were saying, but I just did it wrong? Thanks.
Page Index Toggle Pages: 1