Slick Forums

Discuss the Slick 2D Library
It is currently Sat May 18, 2013 3:25 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: animating spritesheets
PostPosted: Tue Apr 24, 2012 3:31 am 
Offline

Joined: Tue Nov 29, 2011 5:33 pm
Posts: 4
I was looking around for information about animating spritesheets , i looked on the wiki and javadoc and looked around youtube. Any good source of information on animating spritesheets, this is my sprite code.
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package game;
import org.newdawn.slick.Animation;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.SlickException;
/**
*
* @author Revix2k10
*/
public class player {
    private float playerX,playerY;
    private Animation player;
    private int dir = 0;
    public void init(GameContainer gc) throws SlickException{
        SpriteSheet sheet = new SpriteSheet("res\\playerwalk.png",15,23);
        if (dir == 0) {
            for (int frame = 0;frame < 8;frame++){
                player.addFrame(sheet.getSprite(frame,0),150);
            }
        }
        player = new Animation();
        player.setAutoUpdate(true);
       
    }
   
    public void update(GameContainer gc, int delta) throws SlickException{
        if (gc.getInput().isKeyDown(Input.KEY_A)){playerX--;dir = 3;}
        if (gc.getInput().isKeyDown(Input.KEY_D)){playerX++;dir = 2;}
        if (gc.getInput().isKeyDown(Input.KEY_W)){playerY--;dir = 1;}
        if (gc.getInput().isKeyDown(Input.KEY_S)){playerY++;dir = 0;}
    }
    public void render(GameContainer gc,Graphics g) throws SlickException{
        g.drawAnimation(player, playerX, playerX);
    }
}


I get this error
Mon Apr 23 23:31:41 EDT 2012 INFO:Starting display 768x432
Mon Apr 23 23:31:41 EDT 2012 INFO:Use Java PNG Loader = true
WARNING: Found unknown Windows version: Windows 7
Attempting to use default windows plug-in.
Loading: net.java.games.input.DirectAndRawInputEnvironmentPlugin
Failed to initialize device MotioninJoy Virtual Game Controller because of: java.io.IOException: Failed to acquire device (8007001e)
Mon Apr 23 23:31:41 EDT 2012 INFO:Found 2 controllers
Mon Apr 23 23:31:41 EDT 2012 INFO:0 : Logitech USB Headset
Mon Apr 23 23:31:41 EDT 2012 INFO:1 : G19 Gaming Keyboard
Exception in thread "main" java.lang.NullPointerException
at game.player.init(player.java:26)
at game.ZRPG.init(ZRPG.java:27)
at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:393)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:317)
at game.ZRPG.main(ZRPG.java:48)
Java Result: 1
BUILD SUCCESSFUL (total time: 5 seconds)


Top
 Profile  
 
PostPosted: Tue Apr 24, 2012 3:51 am 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1466
You're getting a NPE because you're calling "player.addFrame" before "player" is initialized...

Check AnimationTest for examples.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group