More elegant solution?

edited June 2017 in Questions about Code

Hi there, do you have a more elegant solution to this?:

void show(String side){ if(side == "right"){ rect(rPx, rPy, paddleWidth, paddleHeight); rPy += yspeed; } if(side == "left"){ rect(lPx, lPy, paddleWidth, paddleHeight); lPy += yspeed; } }

Depending on the parameter the rectangels should be on the left or right side. It's working just fine, I just don't feel like it's nice code...

Tagged:

Answers

  • edited June 2017

    You say "solution to this." Solution to what? What do you want the code to do? Be specific.

    You are only showing part of your code, but it looks like you are using a combination of an argument (String side) and hard-coded global variables (rPX / lPX) to create your different pong paddles. That doesn't necessarily make sense. Is this a method of a class/object? Is it just a function? More detail would help. There are many ways of approaching related problems.

  • side == "left"

    side.equals("left"")
    

    side is a String object, comparing using == won't work. see String in the reference.

  • edited June 2017

    Probably you need to draw both rects anyway, so no use to use if in the first place

    Just draw both

  • edited June 2017

    ... comparing using == won't work.

    It's working just fine, ...

    That's factually false, and you should know better! He's even stated it's working! 3:-O
    In Java, both == and != operators use the actual value stored in a variable. ~O)

    So at side == "right", if the value stored in variable side is the same value of object "right", it evals as true, otherwise as false. :-B

    Notice that Java caches all "" String literals found in our programs.
    As long as variables are assigned from String literals, it is relatively safe to use operators == and != for comparing between them. :ar!

    Of course, we shouldn't rely on that Java's String cache feature.
    Always using String:equals() method is the safe game. ;;)

  • Im using this method in a class. I then create 2 objects of that class(left and right paddle) and call the method show(string side) within the draw() method: void draw(){ background(0); rP.show("right"); lP.show("left"); }

    rP for rightPaddle and lP for left Paddle... I know using a class and objects this way doesn't make much sense but when I simply set all variables I need as global variables and then draw the rectangles it doesn't work properly. By using the same variables the rectangles are like 1/10 the size and are both at the upper left corner. By putting all the variables in a class it just works fine for some reason...
  • your approach is very wrong

  • edited June 2017

    this is how it should look like:

    void draw(){
      background(0); 
      rP.show();
      lP.show();
    }
    

    and

    void show(){
          rect(x, y, paddleWidth, paddleHeight);
          y += yspeed;
    }
    

    The idea of a class is that each object derived from it works separately

    Please look at the tutorial about objects
    https://www.processing.org/tutorials/objects/

    Post your entire code to get real help

  • edited June 2017 Answer ✓

    This is better.

    Rule of thumb: Put as much as you can into the class but only for ONE paddle.

    Paddle lp, rp;
    
    void setup() {
      size(700, 700);
      lp=new Paddle(30);
      rp=new Paddle(width-30-30);
    }
    
    void draw() {
      background(0); 
      rp.show();
      lp.show();
    }
    
    // =========================================
    
    class Paddle {
    
      float x, y=240; 
      float paddleWidth=30, paddleHeight=130; 
      float yspeed=0;
    
      Paddle(float xTemp) { // constructor 
        x=xTemp;
      }
    
      void show() {
        rect(x, y, paddleWidth, paddleHeight);
        y += yspeed;
      }
    }//class 
    //
    
Sign In or Register to comment.