Reputation: 41
I'm currently adding values to a HashMap<String, SpriteSheetAnimation>
. I am also adding to the hashmap in the LoadFile method of my input class. When I add to the hashmap, which is part of the class GameObject that a reference is created for in the 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. 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. If I try to access them from within the FileLoader after adding them they are there though. 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)
If I run this the component is not there when i try to fetch with the .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);
}
}
Upvotes: 1
Views: 491
Reputation: 183612
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.
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.
Upvotes: 1