Altering some demo code - get 'unexpected token' error when moving float value, don't understand why

edited December 2014 in Questions about Code

First: hello! (first post)

So, this is using some example code that I've changed about a bit.

Here's the original code that doesn't give this error [1]:

Here's the code that does [2] :

Basically I've moved these lines :

float x = random(100);
fill(x+30,x+100,x+120);

From within void update() to outside (but still within class HLine)

I don't understand because I'm still declaring the variable within it's scope - so shouldn't it pick up on it the same way that it did when this was within the function void update() ?

What I'm expecting this to do is set the colour for each object randomly and permanently.

If I have it within the void update() function it changes the colour with each refresh, rather than just having a random colour for the duration of the objects life.

Would be great to hear some advice, also if there is anything relating to this post (and this forum) that I should be made aware of please say.

cheers.


[1] Code without error

// Declare and construct two objects (h1, h2) from the class HLine 
HLine h1 = new HLine(20, 4.750); 
HLine h2 = new HLine(60, 4.5); 

void setup() 
{
  size(400, 400, P2D);
  frameRate(5);
}

void draw() { 
  background(255);
  h1.update(); 
  h2.update();  
} 

class HLine { 
  float ypos, speed; 
  // what does HLine return ? 
  HLine (float y, float s) {  
    ypos = y; 
    speed = s; 
  } 
  void update() { 
    ypos += speed; 
    if (ypos + ypos > height) { 
      ypos = 0; 
    } 
    strokeWeight(5);
    rect(0, ypos, width, ypos); 
    float x = random(100);
    fill(x+30,x+100,x+120);

  } 

} 

[2] Code that errors (Note line 34 and 35):

// Declare and construct two objects (h1, h2) from the class HLine 
HLine h1 = new HLine(20, 4.750); 
HLine h2 = new HLine(60, 4.5); 

void setup() 
{
  size(400, 400, P2D);
  frameRate(5);
}

void draw() { 
  background(255);
  h1.update(); 
  h2.update();  
} 

class HLine { 
  float ypos, speed; 
  // what does HLine return ? 
  HLine (float y, float s) {  
    ypos = y; 
    speed = s; 
  } 
  void update() { 
    ypos += speed; 
    if (ypos + ypos > height) { 
      ypos = 0; 
    } 
    strokeWeight(5);
    rect(0, ypos, width, ypos); 


  } 
      float x = random(100);
    fill(x+30,x+100,x+120);

} 

Answers

  • Indeed, moving x out like you did is OK, it creates a new field, with permanent value.
    But the fill() call cannot exist out of a method: it is not a field declaration nor a method declaration.
    Beside, it cannot set "permanently" the fill color, you have to call the function each time you want to draw your rectangle (as fill() can be called somewhere else, eg. in another instance of the class!).
    So, move back the fill() call to where it was. Uh, not, to a line before the call to rect(), actually.

  • edited December 2014

    what does HLine return ?

    HLine is the constructor of the class, hence the same name. It's not a method or function, hence no void or other type before the name.

    It returns if you like an object from that class by using the new command in line 2 and 3.

    ;-)

  • edited December 2014

    you can have a color as property of the class when after line 18 you insert

    color myColor;

    in the constructor say

        float x = random(100);  // random
        myColor = color(x+30,x+100,x+120); // store the value 
    

    then a call to

    fill (myColor);

    later (after line 29) is stable.....

  • edited December 2014

    @PhiLho - cheers (not sure how to quote / reply on this forum)

    OK, I think I kind of follow what you're saying - I can't just have a function in a class without it being within a method of that class.

    @Chrisir - I was referring to the HLine on line 20 (Of code block [1] ) :

    HLine (float y, float s) {
        ypos = y;
        speed = s;
    }
    

    that part really.

    It takes as input the y position and the speed as float values. Then it 'constructs' (instantiates?) an object of type HLine (I think that's what you're saying)

    Correct me on anything wrong

    Thanks for the replies - I didn't get a notification for some reason?

    edit -> updated notification settings!

  • edited December 2014

    I answered 2 questions:

    • what does HLine return

    • how can I achieve a random color that stays stable / constant once it's chosen. That was your problem which made you put 2 lines outside a method.

    ;-)

  • I didn't follow Chrisir , re this post

    I don't know what you mean about

    later (after line 29) is stable....

    When what's 'stable'?

    You're saying that the best place to put the color property is in the Constructor section which makes sense as it also stores the speed and the y position.

    I think maybe I'm not really following the layout and stuff idk.

    I'm going to break down the class as I understand / see it, hopefully this will highlight any glaring misunderstandings :


    In that class (using the example [1] "Code without error") there is line :

    2 and 3 -> Instantiate new HLine objects with the given values.

    5 -> Sets the size of the window as well as the frame rate for refreshing. Without the frame rate set there's just a fixed speed everything runs at.

    11 -> The draw() function is called each time the screen is refreshed, it colors the background (white) and calls the .update() method on both of the created objects.

    17 -> This just starts the class and it's name

    18 -> Initialises the variables ypos and speed as floats (no value needs to be assigned initially though). Here is where all variables that are going to be used within the class are initialised. Any variables initialised here will be accessible throughout all further class methods.

    20 -> This is the constructor that dictates what information is passed to the object at it's creation. Values are taken here and then assigned to their relative variables within the class. The values are passed in. The Constructor returns an object HLine that stores the values of ypos and speed

    22 -> This is the update function that draws the HLine onto the screen. Each time the screen is refreshed the HLine function is called using HLine_Object_Name.update(), it will forget the last one and draw a screen in the new place.

    25 -> By adding the speed to the ypos the y position of the HLine object is incremented each time the screen is refreshed, giving the appearance of movement as the HLine object goes across the y axis.

    26 -> resets the y position if it reaches the value of height - this keeps the object within the viewable area.

    29 -> sets the line thickness.

    30 -> draws a rectangle with the given info. x position is fixed, y position changes by the value of speed each frame, rectangle width fills screen (width) and the height is set to ypos which makes the shapes grow as they get closer to the bottom.

    31 -> sets random number to use with color

    32 -> fills in the shape with the random color. (it doesn't make a difference whether these are before or after the rectangle being drawn on line 30? I thought it might)

  • edited December 2014

    When what's 'stable'?

    your initial issue (why you moved the 2 lines in the 1st place) was that the color didn't stay the same ("stable", this is what I meant).

    To make it random and then stay the same:

    in the class before the constructor there are the properties (variables) for the class

    class HLine {
      float ypos, speed;  // properties (variables) for the class
    

    add

    color myColor;

    here.

    Rest as stated above:

    • set the color myColor in the constructor,

    • use it in fill() before rect()

    ;-)

  • edited December 2014

    your assesment about the class is good.

    A few remarks (being pedant)

    The draw() function is called each time the screen is refreshed,

    hm... better, the draw() function runs again and again (60 times peer second). Everything that is drawn on the screen is stored first and the real screen is updated only once at the end of draw().

    This is the update function that draws the HLine onto the screen.

    Good. Better still: This is the update method that draws the objects instantiated from the class HLine onto the screen.

    Each time the screen is refreshed the HLine function is called using HLine_Object_Name.update()

    hm... each time draw() runs and calls Object_Name.update the update-method of the class HLine is called. (don't say "the HLine function"... it's a class...)

    32 -> fills in the shape with the random color. (it doesn't make a difference whether these are before or after the rectangle being drawn on line 30? I thought it might)

    it does. So fill() should be before rect(). Otherwise you'll get each draw() cycle the color from the last cycle.

    • What you should try: Since you have 2 objects both should fill only like half a screen so you see there are 2 rects (from 2 objects). And then they should have each a different color (see above), which is initially random but then constant / fixed / stable during the sketch runs.

    ;-)

  • Answer ✓

    here the original code

    // Declare and construct two objects (h1, h2) from the class HLine 
    HLine h1 = new HLine(20, 4.750); 
    HLine h2 = new HLine(60, 4.5); 
    
    // ---------------------------------------------------
    // Main functions 
    
    void setup() {
      size(400, 400, P2D);
      frameRate(5);
    } // func 
    
    void draw() { 
      background(255);
      h1.update(); 
      h2.update();
    } // func  
    
    // =========================================
    // classes 
    
    class HLine {
    
      float ypos, speed;  // properties 
    
      // constructor HLine returns an object of that class
      HLine (float y, float s) {  
        ypos = y; 
        speed = s;
      } // constr 
    
      void update() { 
        ypos += speed; 
        if (ypos + ypos > height) { 
          ypos = 0;
        } // if  
        strokeWeight(5);
        float x = random(100);
        fill(x+30, x+100, x+120);
        rect(0, ypos, width, ypos);
      } // method
    } // class 
    //
    
  • Thanks for that Chrisir -> I'm going to go through the tutorials a bit more and (hopefully) post a sharper question next time :)

  • there is a OOP tutorial

  • If this is the one that you're referring to then I've seen it, cheers though. I'm going to work through them again and try a few things out.

    If there are any other resources you suggest I'm all ears, thanks Chris :)

Sign In or Register to comment.