7 Replies - 1588 Views - Last Post: 22 August 2012 - 07:48 AM Rate Topic: -----

#1 Randor  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 106
  • Joined: 12-October 10

Slick Animation problem

Posted 19 August 2012 - 07:16 PM

Hello,

I am tryng to play with Java's slick, I have the following code:


Space
package javaGame;

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

public class Space extends BasicGameState{
	
	Animation Explosion,Ship;
	Image worldMap;
	float xpos = 480 ,ypos = 500;
	int[] duration = {75,75,75,75,75};
	int[] ShipDur = {142,142};
	
	public Space(int state) {
		
	}

	@Override
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {

		worldMap = new Image("res/space.png");
		Image[] ShipImg = {new Image("res/shipsall1.gif"),new Image("res/shipsall1.gif")};
		Image[] BlowUp = {new Image("res/Explosion/exp1.png"),
							 new Image("res/Explosion/exp2.png"),
							 new Image("res/Explosion/exp3.png"),
							 new Image("res/Explosion/exp4.png"),
							 new Image("res/Explosion/exp5.png")
		};
		Explosion = new Animation(BlowUp,duration,true);
		Ship = new Animation(ShipImg,ShipDur,false);
		
	}

	@Override
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		worldMap.draw(0,0);
		Ship.draw(xpos,ypos);
		g.drawString("Bobs X Pos: "+xpos+"\nBobs Y Pos: "+ypos, 400, 20);

	}

	@Override
	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
    	Input input = gc.getInput();
    	
		//up
	     if(input.isKeyDown(Input.KEY_UP))    { 
	    	 ypos -=  delta * .15f;
	    	 if(ypos<80) { ypos += delta * .15f; }
	     }
	     
	     //down
	     if(input.isKeyDown(Input.KEY_DOWN))  { 
	    	 ypos +=  delta * .15f;
	    	 if(ypos>635) { ypos -= delta * .15f; }
	     }
	     
	     //left
	     if(input.isKeyDown(Input.KEY_LEFT))  { 
	    	 xpos -= delta * .15f;
	    	 if(xpos<5) { xpos += delta * .15f; }
	     }
	     
	     //right
	     if(input.isKeyDown(Input.KEY_RIGHT)) {
	    	 xpos += delta * .15f;
	    	 if(xpos>940) { xpos -= delta * .15f; }
	     }
	     
	     if(input.isKeyDown(Input.KEY_R)){ Ship = Explosion; } 
	     
		
	}

	@Override
	public int getID() {
		// TODO Auto-generated method stub
		return 2;
	}

}



The problem I am having is trying to get the ship animation to show 1 animation of the "Explosion" If I set the animation to false when I press R (Which is my dirty way of testing the code) it only shows the first frame of the animation code, and if I set the animation to true it shows the animation but loops endlessly through it, is there a way to have the animation run just 1 time?

Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Slick Animation problem

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Slick Animation problem

Posted 19 August 2012 - 07:27 PM

We have a lot of unknown here:

- what is the class BasicGameState your class extends ?
- what is the class Animation ?

Seems we are far away basic/standard Java Swing GUI
Was This Post Helpful? 1
  • +
  • -

#3 Randor  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 106
  • Joined: 12-October 10

Re: Slick Animation problem

Posted 19 August 2012 - 07:29 PM

Sorry, after looking forever, I posted this question then 2 minutes later finally found the answer..

it is
Explosion = setlooping(false);

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Slick Animation problem

Posted 19 August 2012 - 07:34 PM

good for you if setlooping() returns an Animation object from whatever it belongs to

and respect Java conventions. A variable name starts by a lower case character. That will help you understanding your code.
Explosion = should be
explosion =
Was This Post Helpful? 1
  • +
  • -

#5 Randor  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 106
  • Joined: 12-October 10

Re: Slick Animation problem

Posted 19 August 2012 - 07:42 PM

View Postpbl, on 19 August 2012 - 09:27 PM, said:

We have a lot of unknown here:

- what is the class BasicGameState your class extends ?
- what is the class Animation ?

Seems we are far away basic/standard Java Swing GUI


Hi PBL,

This is from the "slick" import library, here: Slick

I did figure it out but this is pretty kool library..

View Postpbl, on 19 August 2012 - 09:34 PM, said:

good for you if setlooping() returns an Animation object from whatever it belongs to

and respect Java conventions. A variable name starts by a lower case character. That will help you understanding your code.
Explosion = should be
explosion =


Thank you, will try to remember that, I tend to get excited with my caps sometimes..

Thank you for the heads up though.
Was This Post Helpful? 0
  • +
  • -

#6 mrQubeMaster  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-August 12

Re: Slick Animation problem

Posted 22 August 2012 - 06:34 AM

View PostRandor, on 19 August 2012 - 07:29 PM, said:

Sorry, after looking forever, I posted this question then 2 minutes later finally found the answer..

it is
Explosion = setlooping(false);


i am having the same problem with my animation and i don't understand how you fixed it.
can you explain how you did it or where the code needs to go.


here is the code. this is not all the coding for the game but this is where i have troubles
package javagame;

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

public class play extends BasicGameState{

   Animation bucky, movingUp, movingDown, movingLeft, movingRight; //4 animations, bucky will be set to one
   Image worldMap;
   boolean quit = false;
   int[] duration = {100,200,100,200}; //duration or length of the frame
   float buckyPositionX = 0; //bucky will start at coordinates 0,0
   float buckyPositionY = 0;
   float shiftX = buckyPositionX + 320; //this will shift the screen so bucky appears in middle
   float shiftY = buckyPositionY + 160; //half the length and half the width of the screen



   public play(int state){
   }
   
   public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
      worldMap = new Image("res/world.png");
      Image[] walkUp = {new Image("res/sam_Back.png"), new Image("res/sam_Back1.png"), new Image("res/sam_Back.png"), new Image("res/sam_Back2.png")}; //these are the images to be used in the "walkUp" animation
      Image[] walkDown = {new Image("res/sam_front.png"), new Image("res/sam_front1.png"), new Image("res/sam_front.png"), new Image("res/sam_front2.png")};
      Image[] walkLeft = {new Image("res/buckysLeft.png"), new Image("res/buckysLeft.png"), new Image("res/buckysLeft.png"), new Image("res/sam_Back.png")};
      Image[] walkRight = {new Image("res/buckysRight.png"), new Image("res/buckysRight.png"), new Image("res/buckysRight.png"), new Image("res/sam_Back.png")};
      
      movingUp = new Animation(walkUp, duration, true); //each animation takes array of images, duration for each image, and autoUpdate (just set to false)
      movingDown = new Animation(walkDown, duration, true);
      movingLeft = new Animation(walkLeft, duration, true);
      movingRight = new Animation(walkRight, duration, true);
      bucky = movingDown; //by default as soon as game loads, bucky will be facing down


   }
   
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
      worldMap.draw(buckyPositionX,buckyPositionY); //draw the map at 0,0 to start
      bucky.draw(shiftX,shiftY); //draw bucky at 320, 160 (center of the screen)
      g.drawString("Buckys X: "+buckyPositionX+"\nBuckys Y: "+buckyPositionY, 400, 20); //indicator to see where bucky is in his world
      
      //when they press escape
      if(quit==true){
         g.drawString("Resume (R)", 250, 100);
         g.drawString("Main Menu (M)", 250, 150);
         g.drawString("Quit Game (Q)", 250, 200);
         if(quit==false){
            g.clear();
         }
      }
   }
   
   public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
      Input input = gc.getInput();
      
      //during the game if the user hits the up arrow...
      if(input.isKeyDown(Input.KEY_UP)){ 
    	 movingUp=setlooping(false);
         bucky = movingUp; //change bucky to up image
         buckyPositionY += delta * .1f; //increase the Y coordinates of bucky (move him up)
         if(buckyPositionY>162){
            buckyPositionY -= delta * .1f; //dont let him keep going up if he reaches the top
         }
      }
      if(input.isKeyDown(Input.KEY_DOWN)){
         bucky = movingDown;
         buckyPositionY -= delta * .1f;
         if(buckyPositionY<-600){
            buckyPositionY += delta * .1f;
         }
      }
      if(input.isKeyDown(Input.KEY_LEFT)){
         bucky = movingLeft;
         buckyPositionX += delta * .1f;
         if(buckyPositionX>324){
            buckyPositionX -= delta * .1f;
         }
      }
      if(input.isKeyDown(Input.KEY_RIGHT)){
         bucky = movingRight;
         buckyPositionX -= delta * .1f;
         if(buckyPositionX<-840){
            buckyPositionX += delta * .1f;
         }
      }
      
      //escape
      if(input.isKeyDown(Input.KEY_ESCAPE)){
         quit = true;
      }      
      
      //when they hit escape
      if(quit==true){
         if(input.isKeyDown(Input.KEY_R)){
            quit = false;
         }
         if(input.isKeyDown(Input.KEY_M)){
            sbg.enterState(2);
            try{
               Thread.sleep(250);
            }catch(InterruptedException e){
               e.printStackTrace();
            }
         }
         if(input.isKeyDown(Input.KEY_Q)){
            System.exit(0);
         }
      }
   }
   
   private Animation setlooping(boolean B)/> {
	// TODO Auto-generated method stub
	return null;
}

public int getID(){
      return 3;
   }
}



Was This Post Helpful? 0
  • +
  • -

#7 Randor  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 106
  • Joined: 12-October 10

Re: Slick Animation problem

Posted 22 August 2012 - 07:25 AM

View PostmrQubeMaster, on 22 August 2012 - 08:34 AM, said:

View PostRandor, on 19 August 2012 - 07:29 PM, said:

Sorry, after looking forever, I posted this question then 2 minutes later finally found the answer..

it is
Explosion = setlooping(false);


i am having the same problem with my animation and i don't understand how you fixed it.
can you explain how you did it or where the code needs to go.


here is the code. this is not all the coding for the game but this is where i have troubles
package javagame;

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

public class play extends BasicGameState{

   Animation bucky, movingUp, movingDown, movingLeft, movingRight; //4 animations, bucky will be set to one
   Image worldMap;
   boolean quit = false;
   int[] duration = {100,200,100,200}; //duration or length of the frame
   float buckyPositionX = 0; //bucky will start at coordinates 0,0
   float buckyPositionY = 0;
   float shiftX = buckyPositionX + 320; //this will shift the screen so bucky appears in middle
   float shiftY = buckyPositionY + 160; //half the length and half the width of the screen



   public play(int state){
   }
   
   public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
      worldMap = new Image("res/world.png");
      Image[] walkUp = {new Image("res/sam_Back.png"), new Image("res/sam_Back1.png"), new Image("res/sam_Back.png"), new Image("res/sam_Back2.png")}; //these are the images to be used in the "walkUp" animation
      Image[] walkDown = {new Image("res/sam_front.png"), new Image("res/sam_front1.png"), new Image("res/sam_front.png"), new Image("res/sam_front2.png")};
      Image[] walkLeft = {new Image("res/buckysLeft.png"), new Image("res/buckysLeft.png"), new Image("res/buckysLeft.png"), new Image("res/sam_Back.png")};
      Image[] walkRight = {new Image("res/buckysRight.png"), new Image("res/buckysRight.png"), new Image("res/buckysRight.png"), new Image("res/sam_Back.png")};
      
      movingUp = new Animation(walkUp, duration, true); //each animation takes array of images, duration for each image, and autoUpdate (just set to false)
      movingDown = new Animation(walkDown, duration, true);
      movingLeft = new Animation(walkLeft, duration, true);
      movingRight = new Animation(walkRight, duration, true);
      bucky = movingDown; //by default as soon as game loads, bucky will be facing down


   }
   
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
      worldMap.draw(buckyPositionX,buckyPositionY); //draw the map at 0,0 to start
      bucky.draw(shiftX,shiftY); //draw bucky at 320, 160 (center of the screen)
      g.drawString("Buckys X: "+buckyPositionX+"\nBuckys Y: "+buckyPositionY, 400, 20); //indicator to see where bucky is in his world
      
      //when they press escape
      if(quit==true){
         g.drawString("Resume (R)", 250, 100);
         g.drawString("Main Menu (M)", 250, 150);
         g.drawString("Quit Game (Q)", 250, 200);
         if(quit==false){
            g.clear();
         }
      }
   }
   
   public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
      Input input = gc.getInput();
      
      //during the game if the user hits the up arrow...
      if(input.isKeyDown(Input.KEY_UP)){ 
    	 movingUp=setlooping(false);
         bucky = movingUp; //change bucky to up image
         buckyPositionY += delta * .1f; //increase the Y coordinates of bucky (move him up)
         if(buckyPositionY>162){
            buckyPositionY -= delta * .1f; //dont let him keep going up if he reaches the top
         }
      }
      if(input.isKeyDown(Input.KEY_DOWN)){
         bucky = movingDown;
         buckyPositionY -= delta * .1f;
         if(buckyPositionY<-600){
            buckyPositionY += delta * .1f;
         }
      }
      if(input.isKeyDown(Input.KEY_LEFT)){
         bucky = movingLeft;
         buckyPositionX += delta * .1f;
         if(buckyPositionX>324){
            buckyPositionX -= delta * .1f;
         }
      }
      if(input.isKeyDown(Input.KEY_RIGHT)){
         bucky = movingRight;
         buckyPositionX -= delta * .1f;
         if(buckyPositionX<-840){
            buckyPositionX += delta * .1f;
         }
      }
      
      //escape
      if(input.isKeyDown(Input.KEY_ESCAPE)){
         quit = true;
      }      
      
      //when they hit escape
      if(quit==true){
         if(input.isKeyDown(Input.KEY_R)){
            quit = false;
         }
         if(input.isKeyDown(Input.KEY_M)){
            sbg.enterState(2);
            try{
               Thread.sleep(250);
            }catch(InterruptedException e){
               e.printStackTrace();
            }
         }
         if(input.isKeyDown(Input.KEY_Q)){
            System.exit(0);
         }
      }
   }
   
   private Animation setlooping(boolean B)/> {
	// TODO Auto-generated method stub
	return null;
}

public int getID(){
      return 3;
   }
}






Try This:

      movingUp = new Animation(walkUp, duration, true); //each animation takes array of images, duration for each image, and autoUpdate (just set to false)
movingUp.setLooping(false);  

030       movingDown = new Animation(walkDown, duration, true);
movingDown.setLooping(false);  

031       movingLeft = new Animation(walkLeft, duration, true);
movingLeft.setLooping(false);  

032       movingRight = new Animation(walkRight, duration, true); 
movingRight.setLooping(false); 



That should make the animation run just once, however.. if you plan on reusing it, you will need to restart it each time with:

      if(input.isKeyDown(Input.KEY_DOWN)){
         bucky = movingDown;
         movingDown.restart();
         buckyPositionY -= delta * .1f;
         if(buckyPositionY<-600){
            buckyPositionY += delta * .1f;
         }
      }





I took the setLooping out of your actionevent handler(movingdown) you wont need it to set each time so once in init should be fine. Now just do the other 3 directions the same way and it SHOULD work.. but no guarentees as im just learning mysel..

Goodluck.

This post has been edited by Randor: 22 August 2012 - 07:29 AM

Was This Post Helpful? 0
  • +
  • -

#8 mrQubeMaster  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-August 12

Re: Slick Animation problem

Posted 22 August 2012 - 07:48 AM

View PostRandor, on 22 August 2012 - 07:25 AM, said:

View PostmrQubeMaster, on 22 August 2012 - 08:34 AM, said:

View PostRandor, on 19 August 2012 - 07:29 PM, said:

Sorry, after looking forever, I posted this question then 2 minutes later finally found the answer..

it is
Explosion = setlooping(false);


i am having the same problem with my animation and i don't understand how you fixed it.
can you explain how you did it or where the code needs to go.


here is the code. this is not all the coding for the game but this is where i have troubles
package javagame;

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

public class play extends BasicGameState{

   Animation bucky, movingUp, movingDown, movingLeft, movingRight; //4 animations, bucky will be set to one
   Image worldMap;
   boolean quit = false;
   int[] duration = {100,200,100,200}; //duration or length of the frame
   float buckyPositionX = 0; //bucky will start at coordinates 0,0
   float buckyPositionY = 0;
   float shiftX = buckyPositionX + 320; //this will shift the screen so bucky appears in middle
   float shiftY = buckyPositionY + 160; //half the length and half the width of the screen



   public play(int state){
   }
   
   public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
      worldMap = new Image("res/world.png");
      Image[] walkUp = {new Image("res/sam_Back.png"), new Image("res/sam_Back1.png"), new Image("res/sam_Back.png"), new Image("res/sam_Back2.png")}; //these are the images to be used in the "walkUp" animation
      Image[] walkDown = {new Image("res/sam_front.png"), new Image("res/sam_front1.png"), new Image("res/sam_front.png"), new Image("res/sam_front2.png")};
      Image[] walkLeft = {new Image("res/buckysLeft.png"), new Image("res/buckysLeft.png"), new Image("res/buckysLeft.png"), new Image("res/sam_Back.png")};
      Image[] walkRight = {new Image("res/buckysRight.png"), new Image("res/buckysRight.png"), new Image("res/buckysRight.png"), new Image("res/sam_Back.png")};
      
      movingUp = new Animation(walkUp, duration, true); //each animation takes array of images, duration for each image, and autoUpdate (just set to false)
      movingDown = new Animation(walkDown, duration, true);
      movingLeft = new Animation(walkLeft, duration, true);
      movingRight = new Animation(walkRight, duration, true);
      bucky = movingDown; //by default as soon as game loads, bucky will be facing down


   }
   
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
      worldMap.draw(buckyPositionX,buckyPositionY); //draw the map at 0,0 to start
      bucky.draw(shiftX,shiftY); //draw bucky at 320, 160 (center of the screen)
      g.drawString("Buckys X: "+buckyPositionX+"\nBuckys Y: "+buckyPositionY, 400, 20); //indicator to see where bucky is in his world
      
      //when they press escape
      if(quit==true){
         g.drawString("Resume (R)", 250, 100);
         g.drawString("Main Menu (M)", 250, 150);
         g.drawString("Quit Game (Q)", 250, 200);
         if(quit==false){
            g.clear();
         }
      }
   }
   
   public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
      Input input = gc.getInput();
      
      //during the game if the user hits the up arrow...
      if(input.isKeyDown(Input.KEY_UP)){ 
    	 movingUp=setlooping(false);
         bucky = movingUp; //change bucky to up image
         buckyPositionY += delta * .1f; //increase the Y coordinates of bucky (move him up)
         if(buckyPositionY>162){
            buckyPositionY -= delta * .1f; //dont let him keep going up if he reaches the top
         }
      }
      if(input.isKeyDown(Input.KEY_DOWN)){
         bucky = movingDown;
         buckyPositionY -= delta * .1f;
         if(buckyPositionY<-600){
            buckyPositionY += delta * .1f;
         }
      }
      if(input.isKeyDown(Input.KEY_LEFT)){
         bucky = movingLeft;
         buckyPositionX += delta * .1f;
         if(buckyPositionX>324){
            buckyPositionX -= delta * .1f;
         }
      }
      if(input.isKeyDown(Input.KEY_RIGHT)){
         bucky = movingRight;
         buckyPositionX -= delta * .1f;
         if(buckyPositionX<-840){
            buckyPositionX += delta * .1f;
         }
      }
      
      //escape
      if(input.isKeyDown(Input.KEY_ESCAPE)){
         quit = true;
      }      
      
      //when they hit escape
      if(quit==true){
         if(input.isKeyDown(Input.KEY_R)){
            quit = false;
         }
         if(input.isKeyDown(Input.KEY_M)){
            sbg.enterState(2);
            try{
               Thread.sleep(250);
            }catch(InterruptedException e){
               e.printStackTrace();
            }
         }
         if(input.isKeyDown(Input.KEY_Q)){
            System.exit(0);
         }
      }
   }
   
   private Animation setlooping(boolean B)/> {
	// TODO Auto-generated method stub
	return null;
}

public int getID(){
      return 3;
   }
}






Try This:

      movingUp = new Animation(walkUp, duration, true); //each animation takes array of images, duration for each image, and autoUpdate (just set to false)
movingUp.setLooping(false);  

030       movingDown = new Animation(walkDown, duration, true);
movingDown.setLooping(false);  

031       movingLeft = new Animation(walkLeft, duration, true);
movingLeft.setLooping(false);  

032       movingRight = new Animation(walkRight, duration, true); 
movingRight.setLooping(false); 



That should make the animation run just once, however.. if you plan on reusing it, you will need to restart it each time with:

      if(input.isKeyDown(Input.KEY_DOWN)){
         bucky = movingDown;
         movingDown.restart();
         buckyPositionY -= delta * .1f;
         if(buckyPositionY<-600){
            buckyPositionY += delta * .1f;
         }
      }





I took the setLooping out of your actionevent handler(movingdown) you wont need it to set each time so once in init should be fine. Now just do the other 3 directions the same way and it SHOULD work.. but no guarentees as im just learning mysel..

Goodluck.


i did that but i want to keep the animation running till i release the button.
so the animation runs from the point that i press until i release.
and your solution really helped me thenx for that
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1