1

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);

    }

}
3
  • 2
    What have you tried? Post relevant short code to begin with. BTW Welcome to stack overflow Commented Nov 25, 2012 at 4:24
  • @Aniket I have added some code.. I think it is something that I am not understanding pertaining to object references.. I hope you can help! Commented Nov 25, 2012 at 4:44
  • @MouseEvent I posted some code.. I hope it can help! Commented Nov 25, 2012 at 4:44

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That was part of my problem. I was also adding the animation to the entity in the getAnimationComponent() method, which I think was also causing problems. Thanks!
@CodyShort If this was the correct answer, please "upvote" it (click the up-pointing triangle above left; the "accept" it by clicking the checkmark.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.