pass color into class

edited March 2016 in Questions about Code

hello, this is a really simple question I guess.. I would like to set color of a simple ellipse which is in a class...

here's my code; Ball myBall; color couleur; void setup() { size(600, 600); smooth(); myBall= new Ball(400, 400); } void draw() { background(0); myBall.setCouleur(255, 0, 0); myBall.display(); }

here's my class:

class Ball { float x= 0; float y = 0; int r, v, b; Ball(float _x, float _y) { x=_x; y =_y; } void display() { fill(couleur); ellipse(x, y, 20, 20); } void setCouleur(int _r, int _v, int _b) { r=_r; v=_v; b=_b; color couleur= color(r, v, b); } }

the ellipse is still black.. Any help would be appreciated ! thanks in advance. lolo

Tagged:

Answers

  • edited March 2016

    Yes, some errors

    first in the 3rd line from below

    Remove color

    Now you don't declare a new local variable with the same name but you use the one you have from line 2. this is the Variable you want to set.

    But now let's move on: when you have multiple balls later you'll notice that you can't give them different colors. This is because your color is global and not inside the class.

    Please put the color inside the class. Like x and y.

    Third Point:

    You pass x and y to the constructor. This is the natural place to pass the color too.

    To have a extra function inside the class to do this is not necessary. And you can just pass a color col_ instead of r g b of course.

    Good luck!

Sign In or Register to comment.