一个简单,非常简单的代码示例,展示了这个问题:
#include <string>
#include <map>
static std::map<std::string, std::map<std::string, int>> defaults = {
{ std::string("Definitely a string"), { std::string("This too"), 0 }},
};
Run Code Online (Sandbox Code Playgroud)
错误? main.cpp:4:58: No matching constructor for initialization of 'std::map<std::string, std::map<std::string, int> >'
我在World类中有一个名为collidable的对象:
Set<Collidable> collidables = new HashSet<Collidable>();
Run Code Online (Sandbox Code Playgroud)
在尝试开发碰撞检测系统(用于球)时,我为X和Y做了两个for循环.
cboxX = (int) Math.floor(position.x - RADIUS);
cboxY = (int) Math.floor(position.y - RADIUS);
cboxW = Math.abs((int) Math.ceil(nextPosition.x + RADIUS) - (int) Math.floor(position.x - RADIUS));
cboxH = Math.abs((int) Math.ceil(nextPosition.y + RADIUS) - (int) Math.floor(position.y - RADIUS));
for (int x = cboxX; x <= cboxW + cboxX - 1; x++)
{
for (int y = cboxY; y <= cboxH + cboxY; y++)
{
}
}
Run Code Online (Sandbox Code Playgroud)
这里一切都很好.但是,在for循环中,我试图检查带有x和y参数的可碰撞物,但是由于我正在创建一个可碰撞的新实例(虽然参数与先前生成的参数完全相同),它总是会变错:
world.collidables.add(new Block(new Vector2(x, y)));
System.out.println(world.collidables.contains(new Block(new Vector2(x, y)))); //returns false …Run Code Online (Sandbox Code Playgroud) 我在这里有这个代码(使用lwjgl但是应该没有意义)尝试在按下esc键时暂停游戏.我使用带有键的ArrayList来跟踪按下的内容和不按下的内容.
public List<Integer> keys = new ArrayList<Integer>();
public void get() {
if (isKeyDown(KEY_ESCAPE) && !keys.contains(KEY_ESCAPE)) {
keys.add(KEY_ESCAPE);
keyEscape();
}
}
public void rem() {
if (!isKeyDown(KEY_ESCAPE) && keys.contains(KEY_ESCAPE))
keys.remove(KEY_ESCAPE);
}
private void keyEscape() {
Screen.paused ^= true;
}
Run Code Online (Sandbox Code Playgroud)
这是由循环,这不叫get()和rem()之后另一个一环,在这个顺序.这给了我一个真棒java.lang.IndexOutOfBoundsException: Index: 1, Size: 1的keys.remove(KEY_ESCAPE);时候我放手ESC的.
有谁有分享的见解?