Using width outside main file

I’m starting here, trying to program a simple pong, but there is a problem when I change the value of initial Player object position.
I want to give the object (a racket) an initial position based on the window’s size, like y = width/2
But since I did not use size(); in this class Player the program uses this width as 100.
How can I fix this?
Do I have to give the size of the window as parameter? It doesn’t sound efficient.

1 Like

Maybe you want to show your code here

but in general:

When you have a function setup() (which you should have), size() should be the first command in it.

All use of width and height would work only AFTER size().

This means you can‘t say y=width before setup or before size

Instead before setup say float y; (making y a global variable) and inside setup but after size say y=width; without float.

Also you can’t instantiate an object with new before setup or size when the constructor makes use of width or height.

Apart from this: you should never use width etc as a variable name. Instead say widthPaddle or so

1 Like

You can use size only once in setup and never in a class.

width is known in all classes unless you make a variable of the same name which you should not do