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 › case x - variable modes ...
Page Index Toggle Pages: 1
case x - variable modes ... (Read 556 times)
case x - variable modes ...
Nov 7th, 2006, 5:47am
 
Hi all,

I am working with the 'case'. I have 3 different cases, or 3 different modes that are activated in different ways. In case 3 the graphic changes when the mouse is over it. But if case 2 was active before the mouse was over it, then it should go back to case 2, and if case 1 was active before the mouse is over it, then case 1 should be active when the mouse leaves. How does that work?

Can I have a variable case number in the }else{ ? How do I define a variable case number?

Thanks a lot for your help!


for(int i = 0; i < myGraphic.length; i++){ // here the graphic class is activated
   if (myGraphic[i].over()) {
     myGraphic[i].mode=3;
     selected = i;
   }
   else {
     myGraphic[i].mode = 0; // can this be a variable?, so that the mode that was last activated can be active?
   }
 }
Re: case x - variable modes ...
Reply #1 - Nov 7th, 2006, 3:19pm
 
I think what you need is just another variable, called "lastMode" or something, this way, when you change to mode #3, you can save the previous mode and restore it, i.e.:

Code:

for(int i = 0; i < myGraphic.length; i++){
if (myGraphic[i].over()) {
myGraphic[i].lastMode = myGraphic[i].mode;
myGraphic[i].mode=3;
selected = i;
} else {
myGraphic[i].mode = myGraphic[i].lastMode;
}
}


It's better practice to access the data of objects using methods, so ultimately you might want to rewrite you code to look something like:

Code:

for(int i = 0; i < myGraphic.length; i++){
if (myGraphic[i].over()) {
myGraphic[i].saveMode();
myGraphic[i].setMode(3);
selected = i;
} else {
myGraphic[i].restoreLastMode();
}
}
Page Index Toggle Pages: 1