我需要针对我正在制作的游戏的建议。我有 16 个精灵,可以在运行时定位在 43 个位置。对于每个位置,我创建了一个字符串,其中包含将放置在该位置的精灵。如何将字符串转换为精灵名称,以便稍后向该精灵添加操作?如果我想添加 if 语句来检查这 16 个精灵中的哪一个位于每个位置,将需要近 700 个 if。有没有办法将字符串转换为精灵?
你有精灵编号吗?
如果是,那么简单的解决方案是通过精灵计数对字符串哈希码进行取模。不幸的是,您需要保留一个映射来存储哪个字符串映射到哪个精灵。
示例:
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;