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

2016/5/29

How do I get the width of a particular world

jeffcia jeffcia

2016/5/29

#
I am trying to find the width of a world but keep getting an static/non-static error. How do I get the static CatWorld width in my Cat class? Thanks in advance!
1
2
3
4
5
6
7
public class Cat  extends Actor
{
    private boolean tired = false;
    private boolean hungry = false;
    private boolean bored = true;
    private boolean right = true; //let the cat start off moving right
    private int worldWidth = CatWorld.getWidth();
danpost danpost

2016/5/29

#
You are trying to get the width of a class. An object created from a World class or one created from a subclass of the World class would have one; but not the class, itself. So, you will need to do one of three things to set the appropriate value to the 'worldWidth' field -- (1) pass the World object or its width to the cat when you create the cat and have the constructor of the Cat class set the value of the field; (2) call a method that receives the passed World object or its width to the Cat object after creating it; or (3) use the 'addedToWorld' method (override it) in the Cat class to set the value of the field. The main thing to remember here is that there is no link between a newly created Cat object to any world until it is placed into one; so, before it is placed into any world, you would need to supply the information and after it is placed into the world, it has automatic access to the value with 'getWorld().getWidth()'. The easiest way (and the way I believe it should be done) is the third way. Just do the following:
1
2
3
4
5
6
7
8
// change your line 7 above to this
private int worldWidth;
 
// and add the following method to the Cat class
protected void addedToWorld(World world)
{
    worldWidth = world.getWidth();
}
Although, you could just remove the field altogether (and not add the 'addedToWorld' method) and use 'getWorld().getWidth()' when needed within the Cat class while the cat is in a world.
jeffcia jeffcia

2016/5/29

#
That did the trick, but why am I passing world in instead of CatWorld, where the cat has been placed? Thanks!!
danpost danpost

2016/5/29

#
jeffcia wrote...
That did the trick, but why am I passing world in instead of CatWorld, where the cat has been placed?
You are passing the CatWorld world where the cat was placed. It is just passed as a World object. Any CatWorld object is also a World object by extension. Also, the hidden fields 'width' and 'height' that are given to any World object are accessible by way of the 'getWidth' and 'getHeight' methods which are inherited to all subclasses of World. You can also look at it another way. The width and height of the world are not saved in fields in your CatWorld class and the methods 'getWidth' and 'getHeight' are not located in your CatWorld class either. They are all part of the World class code. So, accessing those methods does not require you to specify that the World object is any specific type of world such as a CatWorld world object.
jeffcia jeffcia

2016/5/30

#
Thanks, Dan!
You need to login to post a reply.