Hello all, I'm having some troubles since two weeks ago and I can't solve can someone help me.
I need to access to a private variable xPosition in MovableBrick Class for that I am using encapsulation because my teacher told me to do it, she says that it's a bad practice to use public variable in OPP Programming. We're using Greenfoot as an approach to java and OPP Paradigms, then we will start using NetBeans.
Lets begin so xPosition is a variable that gives me the velocity of a movable brick and I am using a public method and returning it to be able to access it in other class as you can see in the image below.
Meanwhile in the Bear class I've created a new object from MovableBrick class that allows to access to the public methods inside the MovableBrick Class
MovableBrick xVelocityMovableBrick = new MovableBrick(); // Creates the Object
int xVel = xVelocityMovableBrick.xVelocityMovableBrick() // Will access the method xVelocityMovableBrick() that is inside the MovableBrick Class and I will get the xPosition Value and add it to the bear getX() to make it look like he is not moving when it is on the movable brick.
The main problem here is that I can only receive the first value at the beginning of the game.
The first value of xPosition is 2, the movable brick will start moving to the right then if the brick beats in something the value of xPostion is changed to -2 and if it beats again the value us changed to 2 and so on.
In the beginning when xPosition = 2 if the bear is on the Movable brick all works good but then if the brick beats the value received by the bear will be 2 and the brick will be moving with xPosition = -2 so the bear will fall and that’s not what I want, When the brick beats the method should return -2 and the bear should stay on the movable brick
I don't know if I explained everything in the right way so I'm adding 2 videos.
The not working version and how it should work, the working version has public variable.
This is how is working now:
This is how it should work:

