Results 1 to 3 of 3
  1. #1
    Silamoth is offline Member
    Join Date
    Apr 2014
    Posts
    2
    Rep Power
    0

    Default Slick2D Animation Problem

    With Slick2D and Java, I have this program:

    Java Code:
    package com.weebly.silamoth.slick2d;
    
    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.Image;
    import org.newdawn.slick.Input;
    import org.newdawn.slick.SlickException;
    import org.newdawn.slick.SpriteSheet;
    
    public class LearningSlick extends BasicGame
    {
    	
    	private float offsetX = 0;
    	private float offsetY = 0;
    	
    	private static int width = 800;
    	private static int height = 600;
    	
    	private SpriteSheet characterSpriteSheet;
    	private Animation characterAnimation;
    	
    	private Boolean isAnimated = false;
    	
    	public LearningSlick(String title) 
    	{
    		
    		super(title);
    
    	}
    	
    	@Override
    	public void init(GameContainer container) throws SlickException
    	{
    		
    		characterSpriteSheet = new SpriteSheet("assets/images/character_spritesheet.png", 26, 65);
    		characterAnimation = new Animation(characterSpriteSheet, 200);
    		
    	}
    	
    	@Override
    	public void update(GameContainer container, int delta) throws SlickException
    	{
    		
    			Input input = container.getInput();
    			
    			if (input.isKeyDown(Input.KEY_W))
    			{
    				
    				System.out.println("W Key down");
    				offsetY -= .25;
    				
    			}
    			
    			if (input.isKeyDown(Input.KEY_A))
    			{
    				
    				System.out.println("A Key down");
    				offsetX -= .25;
    				
    			}
    			
    			if (input.isKeyDown(Input.KEY_S))
    			{
    				
    				System.out.println("S Key down");
    				offsetY += .25;
    				
    			}
    			
    			if (input.isKeyDown(Input.KEY_D))
    			{
    				
    				System.out.println("D Key down");
    				offsetX += .25;
    				isAnimated = true;
    				
    			}
    			
    			if (offsetX > width - 26)
    			{
    				
    				offsetX = width - 26;
    				
    			}
    			
    			if (offsetX < 0)
    			{
    				
    				offsetX = 0;
    				
    			}
    			
    			if (offsetY > height - 65)
    			{
    				
    				offsetY = height - 65;
    				
    			}
    			
    			if (offsetY < 0)
    			{
    				
    				offsetY = 0;
    				
    			}
    			
    			characterAnimation.update(delta);
    		
    	}
    	
    	@Override
    	public void render(GameContainer container, Graphics g) throws SlickException
    	{
    
    		g.drawString("This is a Slick2D game.", 100, 100);
    				
    		Image character = new Image("assets/images/character.png");
    		character.draw(offsetX, offsetY);
    		
    		if (isAnimated == true)
    		{
    			
    			characterAnimation.draw(offsetX, offsetY);
    			try 
    			{
    				
    				characterAnimation.wait(100);
    				
    			}catch (InterruptedException e) 
    			{
    				
    				e.printStackTrace();
    				
    			}	
    			
    			characterAnimation.stop();
    			
    		}
    		
    	}
    
    	public static void main(String[] args) throws SlickException
    	{
    		
    		
    		AppGameContainer appGameContainer = new AppGameContainer(new LearningSlick("Learning Slick"), width, height, false);
    		appGameContainer.setAlwaysRender(true);
    		appGameContainer.start();
    		
    	}
    
    }
    Whenever I run and hit hit the D key, it crashes. Why is this? Thanks!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Slick2D Animation Problem

    it crashes
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Silamoth is offline Member
    Join Date
    Apr 2014
    Posts
    2
    Rep Power
    0

    Default Re: Slick2D Animation Problem

    Sorry for not including that. Here it is:

    Java Code:
    D Key down
    Thu Apr 24 20:33:13 CDT 2014 ERROR:null
    java.lang.IllegalMonitorStateException
    	at java.lang.Object.wait(Native Method)
    	at com.weebly.silamoth.slick2d.LearningSlick.render(LearningSlick.java:130)
    	at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:688)
    	at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411)
    	at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321)
    	at com.weebly.silamoth.slick2d.LearningSlick.main(LearningSlick.java:151)
    Thu Apr 24 20:33:13 CDT 2014 ERROR:Game.render() failure - check the game code.
    org.newdawn.slick.SlickException: Game.render() failure - check the game code.
    	at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:691)
    	at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411)
    	at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321)
    	at com.weebly.silamoth.slick2d.LearningSlick.main(LearningSlick.java:151)

Similar Threads

  1. Marte Engine / slick2d game - problem with input
    By sspitfire in forum New To Java
    Replies: 1
    Last Post: 02-24-2014, 02:03 AM
  2. {FPS} Issue in my OWN Java Game ONLY+Slick2D
    By Breton in forum Java Gaming
    Replies: 0
    Last Post: 06-27-2012, 04:40 AM
  3. Animation problem
    By aubreybourke in forum Java 2D
    Replies: 11
    Last Post: 02-19-2012, 02:04 PM
  4. Problem with my animation applet
    By cblue in forum Java 2D
    Replies: 0
    Last Post: 08-24-2009, 04:24 PM
  5. Animation Delay - Thread problem
    By wererabit in forum Advanced Java
    Replies: 3
    Last Post: 04-10-2009, 10:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •