Having trouble using map() with variables shared between classes (beginner)
in
Programming Questions
•
1 year ago
Hi,
I'm learning OOP and I'm having trouble getting map() to work using variables shared between classes. Here's what's giving me trouble:
- Ball ball1;
- Ball ball2;
- Slider slider1;
- Slider slider2;
- void setup() {
- ball1 = new Ball(1,1,1,1.5);
- ball2 = new Ball(100,100,2,2.5);
- slider1 = new Slider(ball1.xSpeed,ball1.ySpeed,449,474);
- slider2 = new Slider(ball2.xSpeed,ball2.ySpeed,474,height);
- }
- void draw() {
- ball1.update();
- ball1.draw();
- ball2.update();
- ball2.draw();
- slider1.draw();
- slider2.draw();
- //etc.
- }
- void mouseDragged() {
- slider1.mouseDragged();
- slider2.mouseDragged();
- }
- class Ball {
- float x = 1;
- float y = 1;
- float xSpeed = 0;
- float ySpeed = 0;
- Ball(float tempX, float tempY, float tempXSpeed, float tempYSpeed) {
- x = tempX;
- y = tempY;
- xSpeed = tempXSpeed;
- ySpeed = tempYSpeed;
- }
- //etc.
- }
- class Slider {
- float sliderXSpeed = 0;
- float sliderYSpeed = 0;
- float sliderTop = 0;
- float sliderBottom = 0;
- Slider(float tempSliderXSpeed, float tempSliderYSpeed, float tempSliderTop, float tempSliderBottom) {
- sliderXSpeed = tempSliderXSpeed;
- sliderYSpeed = tempSliderYSpeed;
- sliderTop = tempSliderTop;
- sliderBottom = tempSliderBottom;
- }
- void draw() {
- // Draw slider
- fill(0);
- rect(0, sliderTop, width, buttonHeight);
- for (int section = 0; section < width; section = section + 10) {
- line(section, sliderTop, section, height);
- }
- fill(255);
- // Draw slider bar
- float m = map(sliderXSpeed, 0, 10, 0, width);
- rect(abs(m)-5, sliderTop, 10, buttonHeight);
- }
- void mouseDragged() {
- if (mouseX > 0 && mouseX < width && mouseY > sliderTop && mouseY < sliderBottom) {
- if (sliderXSpeed > 0 && sliderYSpeed > 0) {
- float positivemX = map(mouseX, 0, width, 0, 10);
- float positivemY = map(mouseX, 0, width, 0, 15);
- sliderXSpeed = positivemX;
- sliderYSpeed = positivemY;
- }
- if (sliderXSpeed < 0 && sliderYSpeed < 0) {
- float negativemX = map(mouseX, 0, width, 0, -10);
- float negativemY = map(mouseX, 0, width, 0, -15);
- sliderXSpeed = negativemX;
- sliderYSpeed = negativemY;
- }
- if (sliderXSpeed > 0 && sliderYSpeed < 0) {
- float positivemX = map(mouseX, 0, width, 0, 10);
- float negativemY = map(mouseX, 0, width, 0, -15);
- sliderXSpeed = positivemX;
- sliderYSpeed = negativemY;
- }
- if (sliderXSpeed < 0 && sliderYSpeed > 0) {
- float negativemX = map(mouseX, 0, width, 0, -10);
- float positivemY = map(mouseX, 0, width, 0, 15);
- sliderXSpeed = negativemX;
- sliderYSpeed = positivemY;
- }
- }
- }
- }
The variables sliderXSpeed and sliderYSpeed are using variables from another class (Ball). SliderXSpeed is working in "float m = map(sliderXSpeed, 0, 10, 0, width);" but not in mouseDragged() below it. I'm not sure if this has to do with how I'm using mouseDragged() or map() or because of how I'm assigning the variables positivemX etc. to sliderXSpeed etc.
Thanks for your help!
1