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]) を渡す方法がわかりません。クラスとそのインスタンスで2次元配列を使用することに頭を悩ませようとしています。

MapTile クラス:

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が 型の 2 次元配列であるのMapTileに対し、mapColsは 型の 1 次元配列であるため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 に答える