java - 如何将字符串转换为精灵?

java - How to convert a string to a sprite?

我需要关于我正在制作的游戏的建议。我有 16 个精灵,可以在运行时定位在 43 个位置。对于每个位置,我制作了一个字符串,其中包含将定位在该位置的精灵。如何将字符串转换为精灵名称,以便稍后向该精灵添加动作?如果我想添加 if 语句来检查这 16 个精灵中的哪个在每个位置将需要近 700 个 ifs..有没有办法将字符串转换为精灵?

你有 sprite 的编号吗?

如果是,那么简单的解决方案是通过精灵计数获得字符串哈希码 mod。不幸的是,您需要保留一张地图来存储哪个字符串映射到哪个精灵。

示例:

int spriteCount = 16:
Map strMap = new HashMap();//add type
String str = incomintStr;
int hashStr = incomingStr.hashCode();
int location = hashStr % spriteCount;//a number less than the remaining counts
map.put(str,location);
sprite[location].put(str); 
spriteCount = spriteCount -1;