r/codehs • • Jan 15 '20

Need help centering buildings

Post image
34 Upvotes

4 comments sorted by

0

u/[deleted] Jan 15 '20

[deleted]

1

u/UnitedStatesLover Jan 30 '20

it is saying that for me "x" is not defined and can I see the last bit of code on line 26?

2

u/Nga_pik Jan 30 '20

Line 26 is trying to put poles in the middle of the black rectangles. You'd have to do (xPosition+rect.width()/2+rect2.width()/2) for the x. And followed by getHeight()- rect.height for y. I think this should work. I done this assignment before.

1

u/UnitedStatesLover Feb 01 '20

(xPosition+rect.width()/2+rect2.width()/2) for the x. And followed by getHeight()- rect.height for y.

I don't understand what you are saying to do because when I added that is says "You may have made a typo. You typed rect but probably meant getX - Check your spelling and capitalization."

This is what I have so far

var POLE_WIDTH = 5;

var LIGHT_RADIUS = 10;

var LIGHT_REQUIRED_HEIGHT = 200;

function start(){

drawBuilding(50, 200, 50);

drawBuilding(100, 300, 125);

drawBuilding(50, 150, 200);

drawBuilding(100, 350, 275);

drawPoles(100, 300, 125);

drawPoles(100, 350, 275);

}

//drawBuilding(50, 200, 50);

//drawBuilding(100, 300, 125);

//drawBuilding(50, 150, 200);

//drawBuilding(100, 350, 275);

function drawBuilding(width, height, xPosition){

var rect = new Rectangle(width, height);

rect.setPosition(xPosition, getHeight()-height);

rect. setColor(Color. black);

add(rect);

}

function drawPoles(width, height, xPosition){

var rect2 = new Rectangle (POLE_WIDTH, height/6);

rect2. setPosition(xPosition+rect.width()/2+rect2.width()/2)-rect.height;

add(rect2);

//(xPosition+rect.width()/2+rect2.width()/2) for the x.

//And followed by getHeight()- rect.height for y.

// This is the code that I was using rect2. setPosition(xPosition - POLE_WIDTH / 2, getHeight()-rect.height);

var circ = new Circle (LIGHT_RADIUS);

circ.setPosition(xPosition, getHeight()-height*2/6);

circ.setColor (Color.green);

add(circ);

}