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

2014/11/5

Defining an instance constant?

CKnox CKnox

2014/11/5

#
Hi I just started a new project from pembinatrails in class called Worm, and for the background we have to create cells. I'm really confused on how to do this because I don't know what it means when it asks me to define an instance constant. Here's what it says: "In the Ground class, define an instance constant called CELL_SIZE and set it to 20. Pass this constant to the Worm and Food constructor...Draw vertical and horizontal lines on the world to outline the cells." The Ground class is the world subclass and Food and Worm are Actor subclasses.
Super_Hippo Super_Hippo

2014/11/5

#
I think constant means it can't be changed from anywhere. To achieve this, use the 'final' keyword. If the cellsize should be what the cellsize of a world is, then you can do this:
1
2
3
4
5
6
public static final int CELL_SIZE = 20;
 
public Ground()
{
    super(/*x*/,/*y*/,CELL_SIZE);
}
Then you can access this variable from everywhere with 'Gound.CELL_SIZE'. Is that what you want or do you only want lines on the background without actually have these cells? Then I would suggest to create one of those cells and set it as the background. I did that in my Tetris4D game. I created a grey rectangle and draw a white rectangle, which is a bit smaller than the other, above it. Then I set this as the background and the whole world is filled with these cells. If you have a background image which shouldn't repeat every cell, then you could use a for-loop to create all the lines.
danpost danpost

2014/11/5

#
@Super_Hippo -- no. As 'static', the field is no longer bound to an object, but to the class. In other words, it is no longer an instance field.
1
private final int CELL_SIZE = 20;
As defined here, it is an instance field that cannot be changed -- hence, an instance constant. I also made it private, so other classes or objects cannot directly access the value of the field (the requirement was to pass it to the objects that needed it). Another requirement was to 'draw' the lines on the world, which probably means that Actor objects are not involved in producing them.
Super_Hippo Super_Hippo

2014/11/6

#
The reason why I made it 'static' was that it can be used in the creating of the world. If the variable CELL_SIZE has nothing to do with the cell size in the world constructor, I pointed out (or let's say I tried) what to do then in the last paragraph.
Alwin_Gerrits Alwin_Gerrits

2014/11/6

#
Probably would have been better to let CKnox try with a final int first then let him answere the question if he gets an error himself right? I was told not to make it static if you can prevent it :/...
Super_Hippo Super_Hippo

2014/11/6

#
You can't use a non-static int when creating your start world, that's why I suggested making in static. But if the actual world cell size stays at 1 (or whatever it was before), then this isn't needed anyway.
Alwin_Gerrits Alwin_Gerrits

2014/11/6

#
My idea was more like: If it doesn't work, fix it. If you don't know if it works or not, try it.
CKnox CKnox

2014/11/12

#
Thanks for responding guys, I will try using the above codes and see if they work. Also sorry for the late reply.
CKnox CKnox

2014/11/12

#
Update: When I tried Super_Hippo's suggestion it onoly made the world bigger, but didn't create the visible cells. And when I tried danpost's suggestion it didn't compile, it had an error saying it cannot reference Cell_Size before the supertype constructor is called. There also seems to be confusion about what I'm trying to do, which is basically something similar to snake. So the cells I'm trying to make are supposed to be similar to like in a snake game.
danpost danpost

2014/11/12

#
I do not think you were coding my way properly. My suggestion was this:
1
2
3
4
5
6
7
8
public class Ground extends World
{
    private final int CELL_SIZE = 20;
 
    public Ground()
    {
        super(/* x */, /* y */, 1);
    // etc.
CKnox CKnox

2014/11/12

#
Thankyou for your help and clearing that up I will continue working with it to make sure it works.
CKnox CKnox

2014/11/25

#
I couldn't get the image to show what it is but this link ( http://www.pembinatrails.ca/fortrichmondcollegiate/compsci/greenfoot/worm/default.htm ) is the project I'm trying to do. The cells are supposed to look like that but they aren't showing. I was able to get the Food and Worm images but not the cells.
danpost danpost

2014/11/25

#
You need to draw the lines on the world background to produce the edges of the cells (horizontal and vertical lines gapped by the value of CELL_SIZE pixels). The default background image of the world has a default drawing color of WHITE, so it will need to be changed before drawing the lines.
You need to login to post a reply.