This site requires JavaScript, please enable it in your browser!
Greenfoot back
Fábio
Fábio wrote ...

2015/3/17

Encapsulation only returns the value of a private variable one time????

Fábio Fábio

2015/3/17

#
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:
danpost danpost

2015/3/17

#
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
This is the problem. You are creating a new MovableBrick object that has initial values for its fields. The 'xPosition' field will be '2' and remain '2' until it is placed into the world and 'beats in something'. You need to get the 'xPosition' value from the MovableBrick object that is already in the world -- the one that the bear is actually on. You can use 'getOneObjectAtOffset' to determine if on one and cast the Actor object to a MovableBrick object to call the method on it:
1
2
3
4
5
6
Actor actor = getOneObjectAtOffset(0, getImage().getWidth()/2+1, MovableBrick.class);
if (actor != null)
{
    MovableBrick brick = (MovableBrick) actor;
    int brickXvel = brick.xVelocityMovableBrick();
    // etc.
I need to access to a private variable xPosition in MovableBrick Class
Maybe this is an issue. The field 'xPosition' is not in the class itself -- that is, no actual field is created for the class. Each instance created from the class will receive a field with that name to hold that type of data. So, each MovableBrick object you create has a separate field with that name which holds its own value for that field. That may be why you are getting a different result than what you might have thought.
You need to login to post a reply.