Switch between floors in house layout by clicking button

This is the house layout of ground floor and first floor, the lights, radiators and windows turn on and off by clicking on them.

What i want to do is, when i click top left the OG button it will show the first floor and when i click the EG button it shows the ground floor but i dont know how can i do that?

currently each floor is in a seperate .pde file

Hello @lightbombs
If both floors are moved into a single pde It seems you could do this via a Boolean. If true, draw floor one to the screen, and if false, draw floor two to screen. Perhaps? And as you stated the OG / EG buttons are your target area to initiate on\off or true/false.
:nerd_face:

3 Likes

Very good answer here!

boolean drawFloorOne = true;

Remark

But is your Switch “All Windows Open” referring to the current floor or to both floors?

When it’s referring to the current floor only:

  • When you have variables for the windows, heating and lights and you join the two pde-files to one Sketch (two tabs) make sure you name e.g. the lights like floor1Light0, floor1Light1 etc.

Then evaluate drawFloorOne not only in draw() but also in the mousePressed() function

if(drawFloorOne) {
   // check switches for floor 1
   floor1Light0=....
} else 
{
   // check switches for floor 2
   floor2Light0=....
}

Chrisir

3 Likes

changed the topic category as it seems its in Processing (java) not p5.js :slight_smile:

1 Like

@micuat Since no code shown in original post, how can you tell if in p5js or processing?
Just wondering…
:nerd_face:

EDIT: oh never mind, it must be the .pde, :slight_smile:

1 Like

That’s a way but maybe you want to add more floors later. If that’s the case you could use an Enum.

Here an Example:

//you dont have to have the enum name in capital letters
FLOOR currFloor;

void setup() {
  size(600, 600);
  currFloor = FLOOR.EG;//or FLOOR.OG
}
void draw() {
  if (currFloor == FLOOR.EG) {
    //EG stuff
  } else if (currFloor == FLOOR.OG) {
    //OG stuff
  }
}
enum FLOOR {
  EG, 
    OG
};

3 Likes

Can you please elaborate? I don’t understand how additional floors can be pointed to. There is need of additional code to do this? I don’t have a JAVA background so any clarification is most appreciated. I did google and YouTube about enums but info did not completely translate to usage in your example…
:nerd_face:

1 Like

Im not sure what you mean, but you can imagine that an enum is like a class, only that it has no cunstructure and cannot “store” integers or strings.

For Example:

class myClass {
  //The class can "save" some intergers or Strings
  int myInt;
  String myString;
  float myFloat;
  PVector myPVector;//You know what I mean
  myClass() {
    //This is the cunstructure I think (im not sure because i am from germany) sry
  }
}
//Enum:
enum myEnum {
  Hello, 
    Enum, 
    OrSomethingLike, 
    That
}

//now we can create a variable

myClass myWonderfullClass;
myEnum myWonderfullEnum;

//You have to have a setup function otherwise you will get an error...
void setup() {
  //You have to initialize the class I think im not sure 
  myWonderfullClass = new myClass();

  //Enum:
  //myWonderfullEnum = the "class" type of the variable in my case: myEnum
  //and than a point so that you can get "access" to the type for Example : Hello or Enum or OrSomethingLike or That
  myWonderfullEnum = myEnum.Hello;
  //in the class its similar when you want to access a variable you can say thr name of the class in my case:
  //myWonderfullClass then a point and than the variable name so:
  //myWonderfullClass.myInt = 5;
}

I hope i could help you
:grin:

Flolo

Edit:
If you want (for Java generall) to do more with enums try this out:
Enum Types

1 Like

A boolean distinguishes between 2 floors.

If you had more than 2 floors, you could use an int variable with can be 0,1,2… to indicate many floors. (Of course you still have to make these floors)

enum is now a way to tell the int which numbers it can hold so we can’t use any others (enumerated)

  • by the way using an int to indicate a floor is similar to the concept of state / screen often seen here in the forum

Chrisir

1 Like

Thank you everyone for the help, i really appreciate it

1 Like