[英]Adding values to HashMap
I'm currently adding values to a HashMap<String, SpriteSheetAnimation>
. 我目前正在将值添加到
HashMap<String, SpriteSheetAnimation>
。 I am also adding to the hashmap in the LoadFile method of my input class. 我还将输入类的LoadFile方法添加到哈希图中。 When I add to the hashmap, which is part of the class GameObject that a reference is created for in the FileLoader.
当我添加到哈希图时,哈希图是GameObject类的一部分,在FileLoader中为其创建了引用。 I alter the hashmap, adding keys and values to it, and everything is okay.
我更改了哈希表,向其添加了键和值,一切正常。
I then proceed to add the GameObject object to an objectManager where I store all of the objects for my game. 然后,我将GameObject对象添加到objectManager,在其中存储游戏的所有对象。 When I reference the object in the ArrayList, however, the SpriteSheetAnimation value and the key of that value that I added in the file loader are no longer present.
但是,当我在ArrayList中引用该对象时,不再存在SpriteSheetAnimation值和我在文件加载器中添加的该值的键。 If I try to access them from within the FileLoader after adding them they are there though.
如果我在添加它们后尝试从FileLoader中访问它们,尽管它们在那里。 I am a little confused.
我有点困惑。 Is there possibly a scope issue going on here?
这里可能存在范围问题吗?
I've just realized something that may help you help me..(the System.out.println) 我刚刚意识到可以帮助您的一些事情。(System.out.println)
If I run this the component is not there when i try to fetch with the .toString 如果我运行此组件,则当我尝试使用.toString进行获取时,组件不存在
private void LoadControllableEntity(XMLEventReader eventReader, int x, int y)
{
entities.ControllableEntity entity = new entities.ControllableEntity(x, y);
entity.addComponent(new components.InputComponent(entity), "input");
while(eventReader.hasNext())
{
try
{
XMLEvent event = eventReader.nextEvent();
if(event.isEndElement())
{
if(event.asEndElement().getName().getLocalPart().equals("ControllableEntity"))
{
break;
}
} else if(event.isStartElement())
{
String element = (String) event.asStartElement().getName().getLocalPart();
if(element.equals("renderable"))
{
entity.addComponent(new components.Renderable(entity), "renderer");
}
else if(element.equals("animationComponent"))
{
entity.addComponent(getAnimationComponent(entity, event.asStartElement().getAttributes(), eventReader), "animation");
}
}
} catch(XMLStreamException e)
{
e.printStackTrace();
}
System.out.println(entity.getComponent("animation").toString());
managers.ObjectManager.getInstance().addObject(entity);
}
}
When I run this code though.. It can fetch the component fine(notice I've changed where I'm trying to get the component at.) 但是,当我运行此代码时..它可以很好地获取组件(注意,我更改了尝试获取组件的位置。)
private void LoadControllableEntity(XMLEventReader eventReader, int x, int y)
{
entities.ControllableEntity entity = new entities.ControllableEntity(x, y);
entity.addComponent(new components.InputComponent(entity), "input");
while(eventReader.hasNext())
{
try
{
XMLEvent event = eventReader.nextEvent();
if(event.isEndElement())
{
if(event.asEndElement().getName().getLocalPart().equals("ControllableEntity"))
{
break;
}
} else if(event.isStartElement())
{
String element = (String) event.asStartElement().getName().getLocalPart();
if(element.equals("renderable"))
{
entity.addComponent(new components.Renderable(entity), "renderer");
}
else if(element.equals("animationComponent"))
{
entity.addComponent(getAnimationComponent(entity, event.asStartElement().getAttributes(), eventReader), "animation");
System.out.println(entity.getComponent("animation").toString());
}
}
} catch(XMLStreamException e)
{
e.printStackTrace();
}
managers.ObjectManager.getInstance().addObject(entity);
}
}
The problem with your first code-snippet is that you retrieve and print the animation
entity on every pass through the loop — even before you add that entity — whereas in your second code-snippet you only retrieve and print it immediately after adding the entity, so obviously it doesn't have that problem. 第一个代码段的问题在于,即使在添加该实体之前,您在循环的每一遍都检索并打印
animation
实体,而在第二个代码段中,您仅在添加实体后立即检索并打印该animation
实体,所以很显然,它没有这个问题。
I think you want to change this: 我认为您想更改此设置:
System.out.println(entity.getComponent("animation").toString());
managers.ObjectManager.getInstance().addObject(entity);
}
to this: 对此:
}
System.out.println(entity.getComponent("animation").toString());
managers.ObjectManager.getInstance().addObject(entity);
That is, I think you want those last few steps to be performed after the while
-loop has completed, rather than doing it at the end of each iteration. 也就是说,我认为您希望在
while
-loop完成后执行最后几步,而不是在每次迭代结束时执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.