Can't seem to find what I'm doing wrong with this code

edited September 2014 in Questions about Code
void setup(){
size(1000,1000);
background(255)
int  bearWidth=1/7*width;
int bearHeight=1/10*height;
int bearmiddleX=bearWidth*3+bearWidth/2;
int bearheadwidth=bearwidth/2;
int bearheadheight=bearheight*1/4;
int bearheadY=bearHeight*3+bearheadheight;}

//middlebear
void draw()
{
middlebear();}


void middlebear() {
  fill(random(255),252,random(255));
  strokeWeight(5);
  ellipse(bearmiddleX,7/10*height,bearWidth,bearHeight);}

Answers

  • For starters: you forgot a semicolon, and case matters. A variable named bearWidth and a variable named bearwidth are two different variables.

  • Variables declared inside a function or another {} block are scoped/confined inside it!
    Since all your variables were declared inside setup(), they only exist there! 3:-O

  • edited September 2014

    That semicolon after background. Line 3.

    If you are going to say, bearWidth, make sure you use the same case with it in other places.

    And everything is in setup. You have not really done anything. Put a } in line 10.

    Hope that helps.

  • thanks!! Am I aloud to do: int bearmiddleX=bearWidth*3+bearWidth/2; ?

  • Of course! Because bearWidth is defined. As evidenced by the int you made.

  • edited September 2014

    I see! I also don't quite understand why I should put a } in line 10 when it exists in line 9. There still are a few problems(I can press play but only the default gray screen comes up).

            int bearWidth=1/7*width;
            int bearHeight=1/10*height;
            int bearmiddleX=bearWidth*3+bearWidth/2;
            int bearheadwidth=bearWidth/2;
            int bearheadheight=bearHeight*1/4;
            int bearheadY=bearHeight*3+bearheadheight;
    
            void setup(){
            size(500,500);
            background(255);}
    
    
            //middlebear
            void draw()
            {
            middlebear();}
    
    
            void middlebear() {
              fill(random(255),252,random(255));
              strokeWeight(5);
              ellipse(bearmiddleX,7/10*height,bearWidth,bearHeight);}
    
  • edited September 2014

    When typing in inside Processing's IDE, use CTRL+T all the time in order to auto-format your code!
    It greatly aids us in spotting any misplaced {} and much more! *-:)

  • edited September 2014

    Like this?

    int bearWidth=1/7*width;
    int bearHeight=1/10*height;
    int bearmiddleX=bearWidth*3+bearWidth/2;
    int bearheadwidth=bearWidth/2;
    int bearheadheight=bearHeight*1/4;
    int bearheadY=bearHeight*3+bearheadheight;
    
    void setup() {
      size(500, 500);
      background(255);
    }
    
    
    //middlebear
    void draw()
    {
      middlebear();
    }
    
    
    void middlebear() {
      fill(random(255), 252, random(255));
      strokeWeight(5);
      ellipse(bearmiddleX, 7/10*height, bearWidth, bearHeight);
    }
    
  • You don't need to put the bracket in line 10 anymore. How about you put what you drew in middle near in draw. Don't complicate it too much for now. Don't make a void middlebear function for now if you can help it. I would do that if I'm going to draw more than one thing.

    Good luck again.

  • And your last bracket is not with your function, its kind of off to the outside.

  • Glad I could help!

  • I don't quite understand what you mean by what I drew in middle. Do you mean the //middle bear should be inside the draw? And I'm going to be drawing a few more bears so I need the function void middlebear.

  • Okay, I got you don't worry.

    Do this:

    int bearWidth=1/7*width;
    int bearHeight=1/10*height;
    int bearmiddleX=bearWidth*3+bearWidth/2;
    int bearheadwidth=bearWidth/2;
    int bearheadheight=bearHeight*1/4;
    int bearheadY=bearHeight*3+bearheadheight;
    
    void setup() {
      size(500, 500);
      background(255);
    }
    
    
    //middlebear
    void draw()
    {
      //this next function will draw the middle bear
    
    
      fill(random(255), 252, random(255));
      strokeWeight(5);
      ellipse(bearmiddleX, 7/10*height, bearWidth, bearHeight);
    
    
    //drawing the next bear....
    }
    

    That is what I would do. I mean, sometimes putting it out there could be a little confusing, so I thought this could help.

  • //middle won't affect the code. Processing does not read it. It's just a note for you, the user, so that you know what it is.

    :)

  • Wow, your very helpful! thanks for the help, unfortunately the code still seems to be not working. Nonetheless, I learnt tons of helpful tips from you! I'm really glad you were online when you posted.:)

    I think the problem is due to the parameters of the ellipse(when drawing middle bear) to be incorrect. I'll post if I solved the solution. p.s has this stuff every happened to you?

  • edited September 2014

    Thanks. And has it happens to me? Of course! Remember this, the smallest mistakes can mess you up. I'll continue working on your code to see if I canvas fix it! Hold it, what is 7/10 the height? Maybe try putting in the actual numerical value and see what you get.

  • edited September 2014
    Line 1 and line 2
    have:
        int bearWidth=1/7*width;
        int bearHeight=1/10*height;
    
    but ellipse has:
    
    ellipse(bearmiddleX, 7/10*height, bearWidth, bearHeight);`
    
    they are capitalized :(
    
  • Yeah sorry forget about that next post please. I misread your code. Heh heh, see? Everyone makes mistakes! :)

  • edited September 2014

    In Java when both operands are whole values in a division, the fraction part is removed from the result!!! :-&
    At least, 1 of the operands gotta be a fraction values for a fractional division to happen: 7.0/10*height

  • edited September 2014

    Yeah about that processing has some trouble with fractions, I tried it once, and it didn't work. Try the numerical value so that would be:

    500*.7 which equals: 350

  • You included a decimal point in 7.0 would that mean the data type would then become a float? since float deals with decimal numbers?

  • edited September 2014

    You included a decimal point in 7.0 would that mean the data type would then become a float?

    That's right. It's a common logical bug in Java forgetting the division dual modes: integer and fractional! :-B

  • I don't really know. Just try the 350 for now. It could work. For float, I would say it would only be a float if you wrote

    float= 7.0;

    Or something like that.

  • edited September 2014

    still no go :(

    float bearWidth=0.7*width;
    float bearHeight=0.10*height;
    float bearmiddleX=bearWidth*3.0+bearWidth*0.5
    float bearheadwidth=bearWidth*0.5;
    float bearheadheight=bearHeight*0.25;
    float bearheadY=bearHeight*3.0+bearheadheight;
    
    void setup() {
      size(500, 500);
      background(255);
    }
    
    
    //middlebear
    void draw()
    {
      //this next function will draw the middle bear
    
    
      fill(random(255),252,random(255));
      strokeWeight(5);
      ellipse(bearmiddleX,7/10*height,bearWidth, bearHeight);}
    
  • edited September 2014

    @TechWiz777, Java coerces a whole value to become fractional when iterating w/ another fractional value! :>
    Therefore, in this expression 7.0/10*height, the (int) 10 is coerced to become (float) 10.0 due to the other (float) 7.0 operand for the / operation! :-B
    Since its result is a float, the height's value is also coerced to become a float type for the * multiplication!

  • @GotoLoop That makes sense. Just wondering, do these types of problem occur frequently in processing :(

  • edited September 2014

    Got nothing to do w/ Processing, which is merely a framework library atop Java!
    Any programming language got its own rules for arithmetic expressions!
    Common strategy among them all is coerce any whole value to fractional when the latter is present.
    Since integer operations are generally faster than fractional 1s, values should continue at their integer state until fractional is actually needed! ~O)

  • Yeah, I'm 10000% sure it the ellipse that's the culprit. I cropped out a large chunk of code only to leave variables and the ellipse and It did not seem to play.

    I'll post again if I've got it figured.

  • edited September 2014

    I'm 10000% sure it the ellipse() that's the culprit.

    OK! Let's make a tiny experiment. Type in these lines and see for yourself:

    println(7/10   * height);
    println(7.0/10 * height);
    
  • Why can't we try 350?

  • float bearWidth=0.7*width;
    float bearHeight=0.10*height;
    

    doesn't work in the code above, because it is evaluated before setup() is called, so size() hasn't be called yet, so width and height are still 0.

    Do:

    float bearWidth;
    float bearHeight;
    
    void setup()
    {
      size(500, 500);
      background(255);
    
      bearWidth = 0.7 * width;
      bearHeight = 0.10 * height;
    }
    

    In the first lines, the variables are declared (and get a default value of zero), in setup they are initialized to their value. Notice we don't repeat their type, otherwise it will make different local variables.

  • edited September 2014

    Oh! Forgotten to notice that other bug b/c once I've spotted 7/10, I stopped looking for other bugs! #-o
    Ah! And of course, .7 replaces 7.0/10.0 much more fittingly! 3:-O

Sign In or Register to comment.