the type of expression must be an array type but it resolved itself to an object

for some reason i'm not allowed to access specific values of this object in my array list. the tab i'm haven the issue on is written like this:

class Ball { float yvelocity, xvelocity, friction, acceleration, xmomentum, ymomentum, angle, gravitya, gravityf, mass, radius, x, y, create; PVector yv, xv; boolean letgo, print, switchx, spawn;

Ball(Guide theguide, ArrayList balls) { x = mouseX; y=mouseY; radius=10; create=1; mass=0.1; gravitya=0.981; gravityf=mass*gravitya; letgo=false; print=true; switchx=false; friction=1; spawn=true; }

void display() { if (mousePressed && spawn==true) { create=2; x=mouseX; y=mouseY; } else { if (create==2) { create=3; } } if (create!=1) { ellipse(x, y, radius2, radius2); } }

void fall(Guide theguide) { if (create==3) { if (letgo==false) { yvelocity=-(theguide.yposa - theguide.yposb)/5; xvelocity=-(theguide.xposa - theguide.xposb)/5; letgo=true; spawn=false; } //controls the y if (y<height-radius) { y+=yvelocity; yvelocity+=gravitya; } else { y=height-radius; yvelocity=0; if (abs(xvelocity)> friction) { xvelocity-=friction*(abs(xvelocity) / xvelocity); } else { xvelocity=0; } } //controls the x if (x<=0 || x>=width) { switchx=!switchx; } if (switchx==false) { x+=xvelocity; } else { x-=xvelocity; } } }

void hit(ArrayList balls) { //momentum xmomentum=massxvelocity; ymomentum=massyvelocity; println(balls.get(0)[index].x); //this is where I'm having the issue } }

the discussion editor is being dumb so i'll just say, the line that's having the issue is println(balls.get(0)[index].x);

Answers

  • I'm not sure why you are passing things into your Ball class's constructor... You don't use them.

    balls is, I assume, an ArrayList of Balls.

    balls.get(0) is the first Ball in that ArrayList.

    If you have a Ball, myBall, what does myBall[index] get? An error. Because your Ball object is not an array.

    Maybe you wanted balls.get(index).x ?

  • edited June 2015
    class Guide {
      //
      int xposa, yposa;
      int xposb, yposb;
    }
    
    class Ball {
    
      float yvelocity, xvelocity, 
      friction, 
      acceleration, 
      xmomentum, ymomentum, 
      angle, 
      gravitya, 
      gravityf, 
      mass, 
      radius, 
      x, y, 
      create;
    
      PVector yv, xv;
    
      boolean letgo, print, switchx, spawn;
    
      ArrayList<Ball> balls;
    
      Guide theguide;  
    
      int radius2; 
    
      Ball(Guide theguideTEMP, ArrayList ballsTEMP) { 
        x = mouseX; 
        y=mouseY; 
        radius=10; 
        create=1; 
        mass=0.1; 
        gravitya=0.981; 
        gravityf=mass*gravitya; 
        letgo=false; 
        print=true; 
        switchx=false; 
        friction=1; 
        spawn=true;
    
        // new 
        balls = ballsTEMP; 
        theguide = theguideTEMP;
      }
    
      void display() { 
        if (mousePressed && spawn==true) { 
          create=2; 
          x=mouseX; 
          y=mouseY;
        } 
        else { 
          if (create==2) { 
            create=3;
          }
        } 
        if (create!=1) { 
          ellipse(x, y, radius2, radius2);
        }
      }
    
      void fall(Guide theguide) { 
        if (create==3) { 
          if (letgo==false) { 
            yvelocity=-(theguide.yposa - theguide.yposb)/5; 
            xvelocity=-(theguide.xposa - theguide.xposb)/5; 
            letgo=true; 
            spawn=false;
          } 
          //controls the y
          if (y<height-radius) { 
            y+=yvelocity; 
            yvelocity+=gravitya;
          } 
          else { 
            y=height-radius; 
            yvelocity=0; 
            if (abs(xvelocity)> friction) { 
              xvelocity-=friction*(abs(xvelocity) / xvelocity);
            } 
            else { 
              xvelocity=0;
            }
          } //controls the x
          if (x<=0 || x>=width) { 
            switchx=!switchx;
          } 
          if (switchx==false) { 
            x+=xvelocity;
          } 
          else { 
            x-=xvelocity;
          }
        }
      }
    
      void hit(int index) { 
        //momentum
        xmomentum=mass*xvelocity; 
        ymomentum=mass*yvelocity; 
    
        // println(balls.get(0)[index].x); //this is where I'm having the issue
        println(balls.get(index).x);
      }
    }
    //
    
  • we can't test your code, because you didn't post the entire code

    remember that the parameters you bring into the constructor of the class are only temporary (as has been said)

    you must copy them into the class:

        balls = ballsTEMP; 
        theguide = theguideTEMP;
    

    ;-)

  • I'll post the rest of the code between 4-5, thanks for the help guys

  • i figured it out, i was defining the array list way wrong, thanks for the help everyone, i really appreciate it

  • Great !

Sign In or Register to comment.