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

2017/1/12

Using a String to Instantiate an object?

Dapperbrick Dapperbrick

2017/1/12

#
Is there an easy way I can instantiate a worldTile object by utilising a string such as their x/y coordinates (Creating a worldTile instance with a name like ter_12_142 would make things a lot easier for me)? Or is there a simpler method to go about this? Here's code for placing tiles in the world;
1
2
3
4
5
6
7
8
9
10
11
12
13
private void tilePlacement() {
 
  for (int y = 1; y < rows - 1; y++) {
    for (int x = 2; x < cols - (cols / 4); x++) {
 
      int w = (getColor(noise[x][y][0]));
      int isAForest = (isForest(noise[x][y][1]));
 
      addObject(new worldTile(w, isAForest), x * scl, y * scl);
 
    }
  }
}
danpost danpost

2017/1/12

#
If your tiles are placed uniformly throughout your world, then using a string would be much simpler than you are even suggesting. You can use a specific character for each type object (or to represent multiple objects, if needed for the same location) and have the position of the character within the string determine where in the world it is to be located. For example, if your world was 11 by 11 and you had walls (w), enemies (e), a player (p) and a door (d), you could use this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
String setup =
    "wwwwwwwwwww"+
    "wp        w"+
    "w w w wew w"+
    "w         w"+
    "w w w w w w"+
    "w         w"+
    "w w w w w w"+
    "w         w"+
    "w wew wew w"+
    "w        dw"+
    "w w w w w w";
 
// in constructor (or prepare method)
for (int n=0; n<setup.length(); n++)
{
    char c = setup.charAt(n);
    Actor actor = null;
    switch (c)
    {
        case 'p':  actor = new Player(); break;
        case 'w':  actor = new Wall(); break;
        case 'e':  actor = new Enemy(); break;
        case 'd':  actor = new Door(); break;
    }
    if (actor != null) addObject(actor, n%getWidth(), n/getWidth());
}
Dapperbrick Dapperbrick

2017/1/16

#
Maybe I didn't explain the context properly, because uh... Well, that wouldn't really work in this case. I do appreciate the help, though! Here's a better explanation: This game features no characters. I want the tiles themselves to interact with eachother, as we're developing a pandemic-type game where each tile has infection properties and will pass these onto the surrounding area. For example, if my map looks like this, where B2 (X) represents an infected tile, I would want it to have the capability to infect surrounding tiles A1, A2, A3, B1, B3, C1, C2, and C3. (assuming they have living properties). My first thought would be for them to interact with tiles at x+-1 or y+-1 using their class titles...
1
2
3
4
5
6
7
8
_123456
A
B x
C
D
E
F
G
I can draw my map properly, that's fine (perlin/simplex noise yo), but I can't seem to get them to interact with eachother. I wonder if using getOneObjectAtOffset and invoking a relevant infection method would work...
Dapperbrick Dapperbrick

2017/1/16

#
So. I have it working... Sort of. I can infect tiles to the south-east of a given tile. Any suggestions?
1
2
3
4
5
6
7
8
9
10
11
12
public void infectNear(){
     
    int xOffset = Greenfoot.getRandomNumber(sclX) - sclX/2 * (Greenfoot.getRandomNumber(2));
    int yOffset = Greenfoot.getRandomNumber(sclY) - sclY/2 * (Greenfoot.getRandomNumber(2));
     
    worldTile tile = (worldTile) getOneObjectAtOffset(xOffset,yOffset,worldTile.class);
    tile.infect();
}
 
public void infect(){
    infected=true;
}
danpost danpost

2017/1/16

#
Dapperbrick wrote...
I wonder if using getOneObjectAtOffset and invoking a relevant infection method would work...
Maybe using 'getNeighbours' would be better. Then choosing a random neighbor to infect.
Dapperbrick Dapperbrick

2017/1/28

#
danpost wrote...
Dapperbrick wrote...
I wonder if using getOneObjectAtOffset and invoking a relevant infection method would work...
Maybe using 'getNeighbours' would be better. Then choosing a random neighbor to infect.
I guess this reply is a tad late, but thank you! getNeighbors didn't work, for some reason, but I managed to tweak my previous values until they did. It now works!
You need to login to post a reply.