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

2014/7/19

creating a tileable map

1
2
davemib123 davemib123

2014/7/19

#
Im trying to create a map for my hero to walk in. So far I have 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class Map
{
    private static final int TileWidth = 16;
    private static final int TileHeight = 16;
 
    String[] Map1 =
        {
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A",
            "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A"
        };
 
    public String[] getMap()
    {
        return Map1;
    }
}
How do I get this to appear into my World class? Link to scenario is here: http://www.greenfoot.org/scenarios/11862
lordhershey lordhershey

2014/7/19

#
Is the background going to be made of actors, or do you intend on drawing directly to the background image?
davemib123 davemib123

2014/7/19

#
lordhershey wrote...
Is the background going to be made of actors, or do you intend on drawing directly to the background image?
They will be actors - well that's the plan. I'm cutting up different sprite images to use as part of the background. Each tile will be 16 x 16 pixels.
lordhershey lordhershey

2014/7/19

#
The map above is it a terrain map or a map that shows in image position to cut out?
davemib123 davemib123

2014/7/19

#
the images i'll be cutting out and placing as actors. The map class would define where each of the actors would fit - if that makes sense?
danpost danpost

2014/7/19

#
You might want to check out my MapWorld Superclass scenario on how to do this.
lordhershey lordhershey

2014/7/19

#
I was trying to remember who had made a tile set thing , but no need now. Looks like you got it.
davemib123 davemib123

2014/7/19

#
so i had a go at it. I tried using String arrays at first and this worked using:
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Town here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Town extends World
{       
    private static final int TileWidth = 16;
    private static final int TileHeight = 16;
 
    private static final String[] TownMap =
        {
            "AAAAAAAAAA",
            "AAAAAAAAAA"
        };
 
    /**
     * Constructor for objects of class Town.
     *
     */
    public Town()
    {   
        // Create a new world with 640x400 cells with a cell size of 1x1 pixels.
        super(640, 480, 1);
        Greenfoot.start();
        Greenfoot.setSpeed(50);
        WorldVisitor.startSequence(this);
        addObject (new Hero(), 318, 222);
        setPaintOrder(Hero.class, Grass.class);
        createMap();
    }
 
    private void createMap()
    {
        for (int rowNumber = 0; rowNumber < TownMap.length; rowNumber++)
        {
            makeMapRow(rowNumber);
        }
    }
 
    /**
     * Add a row of tiles to the world.
     * @param y The row number in the town map
     */
    private void makeMapRow(int y)
    {
        String mapRow = TownMap[y];
        int firstTileHeightBuffer = TileHeight / 2;
        int firstTileWidthBuffer = TileWidth / 2;
 
        for (int x = 0; x < mapRow.length(); x++)
        {
            int tileXLocation = firstTileWidthBuffer + (TileWidth * x);
            int tileYLocation = firstTileHeightBuffer + (TileHeight * y);
            char tileType = mapRow.charAt(x);
            if (tileType == 'A')
            {
                addObject(new leftTrunking(), tileXLocation, tileYLocation);
            }
            else if (tileType == '1')
            {
                addObject(new Trees("smallCombined_BottomLeft"), tileXLocation, tileYLocation);
            }
            else if (tileType != ' ')
            {
                System.out.println("Wrong tile type: " + tileType);
            }
        }
    }
}
but its limited to single characters. I've then tried to read danposts code in MapWorld Superclass. So far I have implemented this part but I am stuck at getting the Int array to check what number is shown in the map array? The scenario is uploaded here: http://www.greenfoot.org/scenarios/11862
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Town here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Town extends World
{       
    private static final int TileWidth = 16;
    private static final int TileHeight = 16;
 
    private static int[][] TownMap =
        {
            { 0, 0, 0, 0, 0 },
            { 0, 0, 0, 0, 0 },
            { 0, 0, 0, 0, 0 },
            { 0, 0, 0, 0, 0 },
            { 0, 0, 0, 0, 0 },
            { 0, 0, 0, 0, 0 },
            { 0, 0, 0, 0, 0 },
            { 0, 0, 0, 0, 0 },
            { 0, 0, 0, 0, 0 },
            { 0, 0, 0, 0, 0 }
        };
 
    /**
     * Constructor for objects of class Town.
     *
     */
    public Town()
    {   
        // Create a new world with 640x400 cells with a cell size of 1x1 pixels.
        super(640, 480, 1);
        Greenfoot.start();
        Greenfoot.setSpeed(50);
        WorldVisitor.startSequence(this);
        addObject (new Hero(), 318, 222);
        setPaintOrder(Hero.class, Grass.class);
        createMap();
    }
 
    private void createMap()
    {
        for (int rowNumber = 0; rowNumber < TownMap.length; rowNumber++)
        {
            makeMapRow();
        }
    }
 
    private void makeMapRow()
    {
        int firstTileHeightBuffer = TileHeight / 2;
        int firstTileWidthBuffer = TileWidth / 2;
 
        for(int x=0; x<TownMap[0].length; x++)
        {
            int tileXLocation = firstTileWidthBuffer + (TileWidth * x);
            for(int y=0; y<TownMap.length; y++)
            {
                int tileYLocation = firstTileHeightBuffer + (TileHeight * y);
                if (TownMap[x][y] == 0)
                     addObject(new leftTrunking(), tileXLocation, tileYLocation);
            }
        }
    }
}
danpost danpost

2014/7/19

#
Looks like you have too many loops. If 'makeMapRow' is only to create one row of actors, then it should only have one loop. You are already interating through 'TownMap.length' within the 'createMap' method that calls the 'makeMapRow' method. I think you need to either pass the row number to the 'makeMapRow' method or just code it all in the 'createMap' method.
davemib123 davemib123

2014/7/19

#
yea cool, got it working. Thanks for the tip danpost :D
davemib123 davemib123

2014/7/19

#
it works, but the objects show up wrongly it is like the whole array is put on its side. This is the createMap method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
private void createMap()
   {
       int firstTileHeightBuffer = TileHeight / 2;
       int firstTileWidthBuffer = TileWidth / 2;
 
       for(int x=0; x<TownMap.length; x++)
       {
           int tileXLocation = firstTileWidthBuffer + (TileWidth * x);
           for(int y=0; y<TownMap[0].length; y++)
           {
               int tileYLocation = firstTileHeightBuffer + (TileHeight * y);
               if (TownMap[x][y] == 0)
               {
                   addObject(new leftTrunking(), tileXLocation, tileYLocation);
               }
               else if (TownMap[x][y] == 1)
               {
                   addObject(new Trees("smallCombined_BottomLeft"), tileXLocation, tileYLocation);
               }
           }
       }
   }
How do I flip it around?
danpost danpost

2014/7/19

#
1
2
3
4
//Change
TownMap[x][y]
//on lines 12 and 16 to
TownMap[y][x]
davemib123 davemib123

2014/7/19

#
I get this:
1
2
3
java.lang.ArrayIndexOutOfBoundsException: 6
    at Town.createMap(Town.java:56)
    at Town.<init>(Town.java:42)
i've tried adjusting the loop so instead of declaring the x first, declare the y first. But still get the same error
danpost danpost

2014/7/20

#
Oh, yeah. You need to switch 'x's with 'y's in lines 6 through 11 (including the variable 'X's and 'Y's).
davemib123 davemib123

2014/7/20

#
great. that works wonderfully :)
There are more replies on the next page.
1
2