Question

someone please help me i am stuck. I want to press up, right, left, down and move my animation in that direction so that it looks like my player is walking. But here is my problem, i have came up with a solution but Java will open the program but then close it very quickly. I know that means there is an error but i cant read the compiler error message because it does not say anything, it just tells me the line number where my error is. Someone please help me, i am very frustrated with this.

package test;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;



public class Play extends BasicGameState
{
    private SpriteSheet MoveRight; // initate a SprtieSheet
    private Animation MoveRightAni; // initate a Animation

    private SpriteSheet MoveLeft; // initate a SprtieSheet
    private Animation MoveLeftAni; // initate a Animation

    private SpriteSheet MoveUp; // initate a SprtieSheet
    private Animation MoveUpAni; // initate a Animation

    private SpriteSheet MoveDown; // initate a SprtieSheet
    private Animation MoveDownAni; // initate a Animation


    private Animation currentImage = MoveRightAni;

    private Image map; 
    public float x = 0;
    public float y = 0;




    public Play( int state){

    }


    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
        MoveRight = new SpriteSheet("test/MoveRight.png",21,24); // declare a SpriteSheet and load it into java with its dimentions
        MoveRightAni = new Animation(MoveRight, 400); // declare a Animation, loading the SpriteSheet and inputing the Animation Speed

        MoveLeft = new SpriteSheet("test/MoveLeft.png",21,24); // declare a SpriteSheet and load it into java with its dimentions
        MoveLeftAni = new Animation(MoveLeft, 400); // declare a Animation, loading the SpriteSheet and inputing the Animation Speed

        MoveUp = new SpriteSheet("test/MoveUp.png",21,24); // declare a SpriteSheet and load it into java with its dimentions
        MoveUpAni = new Animation(MoveUp, 400); // declare a Animation, loading the SpriteSheet and inputing the Animation Speed

        MoveDown = new SpriteSheet("test/MoveDown.png",21,24); // declare a SpriteSheet and load it into java with its dimentions
        MoveDownAni = new Animation(MoveDown, 400); // declare a Animation, loading the SpriteSheet and inputing the Animation Speed

        map = new Image("test/map.png");



    }


    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
        g.drawImage(map, 0, 0);
        currentImage.draw(x,y);



    }

    public void update (GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
    {
        MoveRightAni.update(delta); // this line makes sure the speed of the Animation is true
        MoveUpAni.update(delta); // this line makes sure the speed of the Animation is true
        MoveLeftAni.update(delta); // this line makes sure the speed of the Animation is true
        MoveDownAni.update(delta); // this line makes sure the speed of the Animation is true
        Input input = gc.getInput();

        if (input.isKeyDown(Input.KEY_W))
        {
            y -= 0.1 * delta;
            currentImage = MoveUpAni;

        }
        if (input.isKeyDown(Input.KEY_A))
        {
            x -= 0.1 * delta;
            currentImage = MoveLeftAni;
        }
        if (input.isKeyDown(Input.KEY_D))
        {
            x += 0.1 * delta;
            currentImage = MoveRightAni;
        }
        if (input.isKeyDown(Input.KEY_S))
        {
            y += 0.1 * delta;
            currentImage = MoveDownAni;
        }


    }
    public int getID(){
        return 0;
    }
}
Was it helpful?

Solution

Your problem is that you're trying to initialize the global variable currentImage to an animation that has not yet been initialized. With slick, you want to do anything that deals with initialization in the init() method. When programming in java, always initialize your variables before you try to use them to initialize another variable.

To fix your problem, change:

private Animation currentImage = MoveRightAni;

to:

private Animation currentImage;

and in your init() method at the very last line after map =...

currentImage = MoveRightAni;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top