per object movement

edited February 2016 in Questions about Code

Hi there, This should be fairly simple but I cannot seem to make it work. I've somehow managed drawing each letter separately but I would like ascend() to apply on an x-axis per letter basis instead of the cumulative x so that only the letters on the left of the mouse should execute it. I'd appreciate it if you guys could help me out. Thanks in advance.

            PFont f;
            String s = "This has gone too far";
            int x = 30;
            int howBig = 20;
            Letter [] letters;



            void setup() {
              size (400, 200);
              frameRate(25);
              smooth();
              f = createFont("DINTBlack",20);
              textFont(f);
              letters = new Letter[s.length()];

              for (int i = 0; i<s.length(); i++) {
                char c = s.charAt(i);
                letters[i] = new Letter(x, height/2, c);
                x+=textWidth(c);

                //println(letters);
              }
            }

            void draw() {
              background(127);
              for (int i = 0; i<letters.length; i++) {
                letters[i].display();
                if (mouseX>x) {
                  letters[i].ascend();
                }
                if (mousePressed) {
                  letters[i].home();
                }
              }
            }

        class Letter {
          int x, y;
          int homeX, homeY;
          char c;
          Letter(int x_, int y_, char c_) {
            x=homeX=x_;
            y=homeY=y_;
            c=c_;
          }
          void display(){
            text(c,x,y);
          }
          void ascend(){
            if(y>0){
            y-=random(2,4);

            }else{
              y= homeY;
            }

          }
          void home(){
            x = homeX;
            y = homeY;
          }
        }


            //println(PFont.list());

Answers

  • Not quite sure what you mean but try using letters[i].x instead of x in line 30

  • voilà! that's what i was looking for. Didn't know one could pull the x like that. thank you so much!

Sign In or Register to comment.