Unexpected token color

First of all, sorry for my bad english. Hello i’m a French student, I’ve got a homework about a class to create balls. I’ve followed the teacher’s classes but I’m stuck with an unexpected token. You should understand my code by reading it in full. Thank you.
Here is my code :

class Balle
{
  Balle(float nouvX, float nouvY, color nouvCouleur)
    {
      x=nouvX;
      y=nouvY;
      color=nouvCouleur;
    }
}
  
  void Afficher()
  {
    circle(x, y, 40);
    fill(255, 0, 0);
  }

}

void setup()
  {
    smooth();
    size(400, 200);
    Balle maBalle = new Balle(100, 100, color(255,255,255));
  }

void draw()
  {
    background(0);
    noStroke();
    maBalle.Afficher();
  }
1 Like

That won’t work

color is not a good name for a variable because it’s the name of the type color already

Moreover you need to declare your Ball color before the constructor in the class.

Like color ballColor ;

And then in the constructor
ballColor = nouvCouleur;

I hope this helps!

And welcome to the forum!

Great to have you here with us!

When you want to read more: website, then tutorials, then objects.

Warmest regards

Chrisir

3 Likes

Hi sept,

Welcome to the forum. Don’t worry about your English. It looks just fine. I’m from Finland and there are lot’s of people on the forum who uses English as a second language.

Drawing circles or any graphical elements uses fill (color) and stroke (color and weight) attributes. Fill tells what color fills the inside of the shape and stroke tells what color is used to draw the outline.

Your function

void Afficher()
  {
    circle(x, y, 40);
    fill(255, 0, 0);
  }

first draws and then sets fill color to red (red 255, green 0 and blue 0 gives red). Fill and stroke color stay until they are changed. And you have used function noStroke() which tells processing not to draw outlines at all. It’s in effect until stroke color is set again with stroke() -function

If you want to use color given in constructor of the ball
new Balle(100, 100, color(255,255,255));
You need to change void Afficher() function so that you first set the fill color and then draw. To use color given in constructor you just use it as a parameter for fill(); By the way you set white as your balls colour in your constructor so it would draw white on white background.

You should not use variable names that are reserved, they cause all sorts of grief. So change variable color to something else “couleur” perhaps.

3 Likes

Thanks Chrisir and SomeOne for the fast answer! I’m gonna test this, thank you for your help :slight_smile:.

3 Likes