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

2015/2/17

2D arrays

1
2
KiwiMC1482 KiwiMC1482

2015/2/17

#
So i'm doing a school project and I am wondering if it's possible to make an invisible 10x10 grid at a around a certain area and to then use a 2D array to plot actors if they are close so (pardon my bad pseudocode) but if actor is near (x,y) then go to grid??
danpost danpost

2015/2/17

#
I believe the answer is ... yes. Tell me if I am wrong. You want to, basically, embed a gridded area inside your world; an area that has a "cell size" larger than one (in a world whose cell size IS one). Also, you want any actor within the gridded area to "snap" to the center of the "cell" it resides.
KiwiMC1482 KiwiMC1482

2015/2/18

#
Yeah that's basically it
danpost danpost

2015/2/18

#
Ok. You will need a lot of things; but, an array for actors within the grid is not necessary. First, you need fields for the x and y offsets to the origin (top-left corner) of the gridded area. Then, you need fields for the dimensions (number of cells across, number of cells down and the size of a cell) of the gridded area. Next, you will need a way for your actors to get these values. Finally, your actors will need to center themselves in a cell if within the gridded area. The easiest way to accomplish the last task is probably by just calling a method in the world subclass supplying the actor itself within a parameter to the call and have the world do the necessary checks and action:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// fields
private int gridOriginX, gridOriginY; // offset to top-left corner of gridded area
private int gridWidth, gridHeight, gridCellSize; // dimensions of the gridded area
// if world subclass name is 'MyWorld', actor calls the following method with '((MyWorld)getWorld()).adjustToGrid(this);'
public void adjustToGrid(Actor actor)
{
    if (actor.getWorld() != this) return;
    if (actor.getX() < gridOriginX) return;
    if (actor.getX() >= gridOriginX+gridCellSize*gridWidth) return;
    if (actor.getY() < gridOriginY) return;
    if (actor.getY() >= gridOriginY+gridCellSize*gridHeight) return;
    int x = gridCellSize*((actor.getX()-gridOriginX+gridCellSize/2)/gridCellSize)+gridCellSize/2+gridOriginX;
    int y = gridCellSize*((actor.getY()-gridOriginY+gridCellSize/2)/gridCellSize)+gridCellSize/2+gridOriginY;
    actor.setLocation(x, y);
}
KiwiMC1482 KiwiMC1482

2015/2/18

#
I put this in world class right???
danpost danpost

2015/2/18

#
KiwiMC1482 wrote...
I put this in world class right???
Yes.
KiwiMC1482 KiwiMC1482

2015/2/25

#
Is it possible for me to have a feature which saves everything within the grid to a text file and can be loaded again at a later?
danpost danpost

2015/2/26

#
KiwiMC1482 wrote...
Is it possible for me to have a feature which saves everything within the grid to a text file and can be loaded again at a later?
Yes, but it will not work on the site (loading is no problem; but saving is not allowed due to security reasons). Greenfoot does supply a limited amount of storage for each scenario per logged on users. It is actually possible to store quite a lot of information using UserInfo storage, but it may take a bit of ciphering (or condensing) and decoding.
KiwiMC1482 KiwiMC1482

2015/2/26

#
Ahh okay, although i'm not putting it on the site as it is a project i'm working on in school so it'll just run on the greenfoot installed on the computer does that make being able to save easier?
KiwiMC1482 KiwiMC1482

2015/3/2

#
I've tried coding what you gave me for a few days and it's not working although i have no idea what i'm doing wrong. public class Background extends World { //fields private int gridOriginX, gridOriginY; //offset to top-left corner of gridded area private int gridWidth, gridHeight, gridCellSize; //dimensions of the gridded area /** * Constructor for objects of class Background. * */ public Background() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); } public void createVariables(int gridOriginX, int gridOriginY, int gridWidth ,int gridHeight , int gridCellSize) { gridOriginX = 450; gridOriginY = 350; gridWidth = 10; gridHeight = 10; gridCellSize = 1; } public void adjustToGrid(Crotchet crotchet) { if (crotchet.getWorld() != this) return; if (crotchet.getX() < gridOriginX) return; if (crotchet.getX() >= gridOriginX + gridCellSize * gridWidth) return; if (crotchet.getY() < gridOriginY) return; if (crotchet.getY() >= gridOriginY + gridCellSize * gridHeight) return; int x = gridCellSize * ((crotchet.getX() - gridOriginX + gridCellSize /2) / gridCellSize) + gridCellSize /2 + gridOriginX; int y = gridCellSize * ((crotchet.getY() - gridOriginY + gridCellSize /2) / gridCellSize) + gridCellSize /2 + gridOriginY; crotchet.setLocation(x , y); } }
danpost danpost

2015/3/2

#
Your world constructor only builds a new world canvas of size (600, 400). Nothing is being added into the world (unless you are doing that manually, which I cannot see). Your gridCellSize should be a number much larger than one (probably something like 25); but, definitely not '1' (then what is the use of 'adjustToGrid'?)
davmac davmac

2015/3/2

#
KiwiMC1482: Please use code tags around posted code: This method:
1
2
3
4
5
6
7
8
public void createVariables(int gridOriginX, int gridOriginY, int gridWidth ,int gridHeight , int gridCellSize)
{
    gridOriginX = 450;
    gridOriginY = 350;
    gridWidth = 10;
    gridHeight = 10;
    gridCellSize = 1;
}
... doesn't work. It has parameters named gridOriginX, gridOriginY, etc, and it sets their values, but it doesn't change the values of the instance variables which is probably what you really intended it to do (the parameter names are the same as the instance variable names, so the parameters shadow the instance variables). Seeing as the parameters are unused you can just remove them:
1
2
3
4
5
6
7
8
public void createVariables()
{
    gridOriginX = 450;
    gridOriginY = 350;
    gridWidth = 10;
    gridHeight = 10;
    gridCellSize = 1;
}
However, note that this method isn't called from anywhere, so it never executes (unless you are calling it from another class). You probably should have a call to this method in your constructor.
danpost danpost

2015/3/2

#
danpost wrote...
Your gridCellSize should be a number much larger than one (probably something like 25); but, definitely not '1' (then what is the use of 'adjustToGrid'?)
I said '25' without any consideration of the dimensions of the world and the location of the gridded area within that world. Upon considering them, I realize that '25' is too large and that '5' would be the maximum value to get the entire gridded area within the dimensions of the world. You may want to change your world dimensions to allow the gridded area to be a bit larger (although, I do not know the exact intent on the use of the gridded area, nor what a crotchet is, to say for sure whether that is necessary).
KiwiMC1482 KiwiMC1482

2015/3/5

#
The gridded area is going to be used so actors can be placed and snap to one of the determined squares, the grid should span over a 600 x 250 pixel area and have ten colums and ten rows sized 60 x 25 pixels. The actor i'm using is a crotchet so I thought when you put actor in
1
2
3
4
5
6
7
8
9
10
11
public void adjustToGrid(Actor actor)
{
    if (actor.getWorld() != this) return;
    if (actor.getX() < gridOriginX) return;
    if (actor.getX() >= gridOriginX+gridCellSize*gridWidth) return;
    if (actor.getY() < gridOriginY) return;
    if (actor.getY() >= gridOriginY+gridCellSize*gridHeight) return;
    int x = gridCellSize*((actor.getX()-gridOriginX+gridCellSize/2)/gridCellSize)+gridCellSize/2+gridOriginX;
    int y = gridCellSize*((actor.getY()-gridOriginY+gridCellSize/2)/gridCellSize)+gridCellSize/2+gridOriginY;
    actor.setLocation(x, y);
}
I was to put the actor name
KiwiMC1482 KiwiMC1482

2015/3/5

#
Think i may have already asked this but is there anyway I can have the option to save the grid onto the computer being used and then load it back again at a later date?
There are more replies on the next page.
1
2