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

2015/2/17

2D arrays

1
2
danpost danpost

2015/3/5

#
First issue: the 'adjustToGrid' method was created to accommodate square grid-cells. You will need two separate fields, 'gridCellX' and 'gridCellY', instead of the one field, 'gridCellSize' for the dimensions (width and height) of one grid-cell. Second issue (maybe): if the size of your Background world is not at least (1050, 600), then you will have an issue. After removing the declaration of the field 'gridCellSize' and adding in 'gridCellX' and 'gridCellY', you will need to set the value of those fields in your 'createVariable' method. Then change the 'adjustToGrid' method to this:
public void adjustToGrid(Actor actor)
{
    if (actor.getWorld() != this) return;
    if (actor.getX() < gridOriginX) return;
    if (actor.getX() >= gridOriginX+gridCellX*gridWidth) return;
    if (actor.getY() < gridOriginY) return;
    if (actor.getY() >= gridOriginY+gridCellY*gridHeight) return;
    int x = gridCellX*((actor.getX()-gridOriginX+gridCellX/2)/gridCellX)+gridCellX/2+gridOriginX;
    int y = gridCellY*((actor.getY()-gridOriginY+gridCellY/2)/gridCellY)+gridCellY/2+gridOriginY;
    actor.setLocation(x, y);
}
You need to login to post a reply.
1
2