0

我试图通过这样的循环每次将一个项目分配给 mapTiles[x][i]: mapTiles[x][i] = mapCols[i]; 但它不起作用。这是我得到的错误:

incompatible types - found java.lang.String but expected MapTile

我的代码:

public class Map {

MapTile[][] mapTiles;
String imageMap;
String rawMap;

// constructor 
public Map() {
    imageMap = "Map_DragonShrine.jpg";
    rawMap = "Dragon_Shrine.map";
    mapTiles = new MapTile[34][22];
}


// methods
public void loadMapFile() {

    rawMap = file2String(rawMap);

    // array used to hold columns in a row after spliting by space
    String[] mapCols = null;
    // split map using 'bitmap' as delimiter
    String[] mapLines = rawMap.split("bitmap");  
    // assign whatever is after 'bitmap'
    rawMap = mapLines[1];
    // split string to remove comment on the bottom of the file
    mapLines = rawMap.split("#");
    // assign final map
    rawMap = mapLines[0].trim();
    mapLines = rawMap.split("\\n+");

    for(int x = 0; x < mapLines.length; x++) {
        rawMap = mapLines[x] ;
        mapCols = rawMap.split("\\s+");
        for(int i = 0; i < mapCols.length; i++) {
            mapTiles[x][i] = mapCols[i];   
        }            
    }   
}     
}

这是 mapTiles 是其对象的 MapTile 类。鉴于我所拥有的,我不明白如何通过 new MapTile(mapTiles[i]) 。我正在尝试将 2d 数组与类及其实例一起使用。

地图瓦片类:

public class MapTile {
/**Solid rock that is impassable**/
boolean SOLID, 

/** Difficult terrain. Slow to travel over*/
DIFFICULT, 

/** Statue. */
STATUE,

/** Sacred Circle.  Meelee and ranged attacks are +2 and
    damage is considered magic.  */
SACRED_CIRCLE, 

/** Summoning Circle. */
SUMMONING_CIRCLE,

/** Spike Stones. */
SPIKE_STONES,

/** Blood Rock.Melee attacks score critical hits on a natural 19 or 20. */
BLOOD_ROCK,

/** Zone of Death.Must make a morale check if hit by a melee attack. */
ZONE_OF_DEATH,

/** Haunted.-2 to all saves. */
HAUNTED,

/** Risky terrain. */
RISKY,

/** A pit or chasm. */
PIT,

/** Steep slope. */
STEEP_SLOPE,

/** Lava. */
LAVA,

/** Smoke. Blocks LOS. */
SMOKE,

/** Forest. Blocks LOS. */
FOREST,

/** Teleporter. */
TELEPORTER,

/** Waterfall. */
WATERFALL,

/** Start area A. */
START_A,

/** Start Area B. */
START_B,

/** Exit A. */
EXIT_A,

/** Exit B. */
EXIT_B,

/** Victory Area A. */
VICTORY_A,

/** Victory Area B. */
VICTORY_B,

/** Temporary wall terrain created using an Elemental Wall or other means. */
ELEMENTAL_WALL;       

public MapTile() {
}

}

4

1 回答 1

2

那是因为mapTiles是 type 的二维数组,MapTilemapCols是 type 的一维数组String。您不能将 String 值分配给MapTile类型的变量。它们incompatible types如错误消息所述。

你可能想做这样的事情

mapTiles[x][i] = new MapTile(mapCols[i]);
// where the MapTile constructor takes a String parameter.
于 2013-11-06T05:43:15.270 回答