Question about object oriented and conditional programming (beginner)
in
Programming Questions
•
2 years ago
Hello, I have a question regarding object oriented programming and the need for functions to be processed simultaneously
I will paste the current code I'm working later, but I a couple issues I'm running into that I need some help with. My code is basically changing the color of rectangles in the position of WASD keys when the individual keys are pressed. But, for some reason the 'A' key corresponds to changing the color of another keys rectangle and I cannot figure out why because the coordinates for the rectangles is in the function with its corresponding key. Also, If two keys are pressed at the same time it will only recognize that the most recently pressed key is currently pressed, and I am convinced that this is not a hardware ghosting issue of my keyboard but I could be wrong. So here is my code i am sure after looking/implementing the code someone can quickly tell me what I am doing wrong. Any advice regarding these problems or regarding other issues would be greatly appreciated.
Switch switchA;
Switch switchW;
Switch switchD;
Switch switchS;
void setup()
{
size(720, 720);
switchA = new Switch('a', 'A', 10, 120, 120, 100);
switchW = new Switch('w', 'W', 140, 10, 120, 100);
switchD = new Switch('d', 'D', 270, 120, 120, 100);
switchS = new Switch('s', 'S', 140, 120, 120, 100);
}
void draw()
{
background(255);
switchA.check();
switchW.check();
switchD.check();
switchS.check();
}
class Switch
{
float key1;
float key2;
float x;
float y;
float x1;
float y1;
Switch(float tempKey1, float tempKey2, float tempX, float tempY, float tempX1, float tempY1)
{
key1 = tempKey1;
key2 = tempKey2;
x = tempX;
y = tempY;
x1 = tempX1;
y1 = tempY1;
}
void check()
{
rect(x, y, x1, y1);
fill(0, 255, 0);
if(keyPressed)
{
if (key == key1 || key == key2)
{
fill(255, 0, 0);
}
}
}
}
Thanks!
I will paste the current code I'm working later, but I a couple issues I'm running into that I need some help with. My code is basically changing the color of rectangles in the position of WASD keys when the individual keys are pressed. But, for some reason the 'A' key corresponds to changing the color of another keys rectangle and I cannot figure out why because the coordinates for the rectangles is in the function with its corresponding key. Also, If two keys are pressed at the same time it will only recognize that the most recently pressed key is currently pressed, and I am convinced that this is not a hardware ghosting issue of my keyboard but I could be wrong. So here is my code i am sure after looking/implementing the code someone can quickly tell me what I am doing wrong. Any advice regarding these problems or regarding other issues would be greatly appreciated.
Switch switchA;
Switch switchW;
Switch switchD;
Switch switchS;
void setup()
{
size(720, 720);
switchA = new Switch('a', 'A', 10, 120, 120, 100);
switchW = new Switch('w', 'W', 140, 10, 120, 100);
switchD = new Switch('d', 'D', 270, 120, 120, 100);
switchS = new Switch('s', 'S', 140, 120, 120, 100);
}
void draw()
{
background(255);
switchA.check();
switchW.check();
switchD.check();
switchS.check();
}
class Switch
{
float key1;
float key2;
float x;
float y;
float x1;
float y1;
Switch(float tempKey1, float tempKey2, float tempX, float tempY, float tempX1, float tempY1)
{
key1 = tempKey1;
key2 = tempKey2;
x = tempX;
y = tempY;
x1 = tempX1;
y1 = tempY1;
}
void check()
{
rect(x, y, x1, y1);
fill(0, 255, 0);
if(keyPressed)
{
if (key == key1 || key == key2)
{
fill(255, 0, 0);
}
}
}
}
Thanks!
1