Question

I wrote a class that loads a series of animations under simple animation names like so:

public Animation walking;
walking= new Animation(1/10f,
        atlas.findRegion("1"),
        atlas.findRegion("2"),
        atlas.findRegion("3"),
        atlas.findRegion("4"),
        atlas.findRegion("5"),
        atlas.findRegion("6"),
        atlas.findRegion("7"), 
        atlas.findRegion("8"),
        atlas.findRegion("9"),
        atlas.findRegion("10"));
walking.setPlayMode(Animation.LOOP);

and so on for the rest. ( all of these being written into the class's constructor to be loaded when it is..

then in a game class i am trying to access those animations using an AnimatedSprite(). which is a library dermetfan wrote that takes an animation instead of a texture and behaves as a sprite. the code used to pass the animation to the sprite is like so:

animatedSprite = new AnimatedSprite(Animations.walking);

eclipse reads this as correct and all is well until run time when there is an error attempting to create the animatedSprite using the animation. now i have used this exact setup all in one class and it seems to work fine when the animation is part of the class that contains the animatedSprite using it. however when setup as i have described something breaks when passing the animation to the AnimatedSprite class.

yes i am creating an animations=new Animations(); before passing it to the AnimatedSprite as eclipse sees the Animations.walking as a valid animation. any ideas?

Was it helpful?

Solution

If you're getting a null pointer exception, it means that you don't instantiate walking before calling it. You should make a method called initAnims() in which would be in your Animations class

New Animations.class

public Animation walking;

public void initAnims() {
    walking= new Animation(1/10f,
        atlas.findRegion("1"),
        atlas.findRegion("2"),
        atlas.findRegion("3"),
        atlas.findRegion("4"),
        atlas.findRegion("5"),
        atlas.findRegion("6"),
        atlas.findRegion("7"), 
        atlas.findRegion("8"),
        atlas.findRegion("9"),
        atlas.findRegion("10"));
        walking.setPlayMode(Animation.LOOP);

//Init other anims here
}

In your main(String[] args) method you can call the initAnims(), and just then start the game itself. You could call the initAnims() method in any king of show() method as well, in your screen classes (if you have any).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top