Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Sprite Animation with ArrayLists

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sprite Animation with ArrayLists

    Hokay. New here. Hi, nice to meet you, no really the pleasure is all mine.

    Anyway it's finals time here at UCLA my school, and I'm trying to make a game. It's a simple fighting game, where two sprites can attack each other until one of the two's HP is brought down to zero. I'm sure it sounds simple enough, but I guess my brain doesn't work in java well enough yet. >_<

    The first thing I want to do is get the sprites animated and working in a frame, the rest I can worry about later. So, with this being my initial intent, I made an ArrayList of BufferedImages (Is this the best thing to do? What are my alternatives, and how do I do them? It might just be me and my juvenile java, but an ArrayList filled with SOME type of images seemed best.) figuring that I could make a loop that ran through the ArrayList, displaying the BufferedImage that was in the different index values, making an animation. So I spent a good amount of time breaking apart sprite sheets assigning the individual sprites to the different ArrayLists. Like there's an ArrayList for walking, and when you press an arrow key, the character will switch from his standing animation, and go to his walking one as you move across the screen.

    So now I have a bunch of Arrays (hypothetically) filled with images. . . . Now what? How do I make it so that in the tester program, the frames are animated one by one? I figured making an Animation class with all the specifics would do, but what do I put inside the animate method that makes the BufferedImage stored in the different indexes show up? I'm pretty much stuck here.

    Thanks in advance for your help ^-^


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Sprite Animation with ArrayLists

    I think the gist of what you want to do is have a game loop (probably using a Swing Timer) that constantly updates the game state. Then you could have a KeyListener that determines which animation should be playing. In the game loop, update which frame of the animation should be displayed, then call repaint(). In your paintComponent() method, simply draw that frame.

    You could also look for an animation library, but I would suggest understanding what's going on before using a library.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sprite Animation with ArrayLists

    After working on it yesterday and today, I've come up with this:

             //Animation class
             public void stand()
    	{
    		for(i=1; i<stand.size()+2; i++)
    		{
    			if(i== stand.size()+1)
    				i = 2;
     
    			walkImg= stand.get(i);
    			repaint();
     
     
    		}
    	}
     
    	public void paint(Graphics g)
    	{
    		//recover Graphics2D
    		Graphics2D g2 = (Graphics2D) g;
    		g2.drawImage(walkImg, x, y, null);
    	}

     
    //Tester class, merely for the purpose of testing to see if the sprite really animates.
    public class Tester
    {
    	public static void main(String[] args)
    	{
    	JFrame frame = new JFrame();
     
    	Sprite andy = new Andy();
     
    	Animate anim = new Animate(andy);
     
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setSize(70,140);
    	frame.setVisible(true);
    	frame.add(anim);
    	anim.stand();
    	frame.pack();
    	}
     
     
     
    }

    This gives me the first sprite in the Andy array, but does not cycle through. It also gives me an indexoutofbounds error for the loop in the stand() class. Since I actually have a sprite on screen, I'm taking this as a good sign, but thats about all that works right now.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Sprite Animation with ArrayLists

    The for loop you're using is going to be over and done in a matter of milliseconds (if not faster), so you aren't going to see any of the images, just the last one. You want to introduce a pause in between the changing of each image- the best way to do this in your case is to use a Swing Timer that fires once for each frame (so if your game is 30 fps, 30 times a second).

    And you're getting that exception because, what happens when i == stand.size()? Keep in mind that indexes start at 0 and go to size-1.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    May 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sprite Animation with ArrayLists

    Okay >_< Good news. I got it to go through the frames and animate. But like you said, it's animating too fast. Now I do need to put the timer in, but I'm not sure which class the method should go into and where exactly to put it...

    public void stand()
    	{
    		for(i=0; i<=stand.size(); i++)
    		{
    			if(i== stand.size())
    				i = 1;
    			img= stand.get(i);
                                          timer.start();
    			repaint();
     
    		}
    	}
     
     
    	public void paint(Graphics g)
    	{
    		//recover Graphics2D
    		Graphics2D g2 = (Graphics2D) g;
    		g2.drawImage(img, x, y, null);
    	}
     
    class TimerListener implements ActionListener
        {
        	public void actionPerformed(ActionEvent event)
        	{
        	}
        }

    I think it needs to be in the Animate class so it can call the timer, but I'm not sure what to put in the actionPerfomed method, or how to make it work...

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Sprite Animation with ArrayLists

    If I were you, I would put the Timer in your top-level game class. I would give it control over everything in the game- each frame, it calls a method of each game object, updating it, doing collision detection, sprite changing, etc.

    Googling something like "java timer game loop" would probably give you a better understanding of what I'm talking about.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    May 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sprite Animation with ArrayLists

    Okay, Mr.Workman. Thanks for all your help. My animation does animate, and how I got it to that point was by doing this:

    public class Animate extends Component
    {
    	Timer timer;
    	BufferedImage img;
    	private final ArrayList<BufferedImage> walk;
     
    	public Animate(Sprite anything)
    	{
    		ActionListener listener = new TimerListener();
    		timer = new Timer(1000, listener);
    		timer.setInitialDelay(1000);
    		walk = anything.spriteWalk();
     
    	public void walk()
    	{
    		for(int i=0; i<=walk.size(); i++)
    		{
    			if(i == walk.size())
    				i = 1;
    			timer.start(); 
    			img = walk.get(i);
    		}
    	}
     
     
        class TimerListener implements ActionListener
        {
        	public void actionPerformed(ActionEvent event)
        	{
        		repaint();
        	}
        }
     
    }

    That code gives me each sprite at the interval specified in new Timer(1000, listener). It works, and slows down each frame at by 1000 miliseconds. Its awesome. But I still have one last problem. The animation shows the frames in a random order. Well, at least it looks random to me.

    I made .gif files that simply had 1,2,3 and 4 on the inside and put them into the walk array instead of the actual sprites and ran it twice. This is the order that it was shown in the first and second time:

    First : 2 3 1 2 4 4 1 2 1 2 2 4 2 3
    Second: 3 4 1 2 2 4 2 1 3 4 3 1 3 4

    I thought before that the loop I wrote would go from index 0 (frame 1) to index 3 (frame 4) and then change the index back to index 1 (frame 2) and keep going to index 3 (frame 4) and starting back at index 1. But it looks to me like it's going at random. Do you have any idea how to fix this?

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Sprite Animation with ArrayLists

    I'm not sure when you're calling that walk() function, but I'm going to assume you're calling it from a KeyListener. Instead of doing that, you want to simply set a boolean from the KeyListener- whether or not the sprite should be walking (later, you might replace that with an enum for different animations). If the boolean is true, the Timer cycles through the sprites and calls repaint.

    What's happening now is that your KeyListener is being triggered a ton of times in between Timer firings, but you're only calling repaint once every second.

    Does that make any sense?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    May 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sprite Animation with ArrayLists

    I understand what you're saying Mr.Workman, but I don't understand how it works in relation to my code. In my Tester class, I only have the walk() method called once. I haven't put any key listeners into it yet. And I also thought that my loop would prevent the array from ever going to index 0 after the first run through, but the animation continues to return the number 1, and I have no idea why. This is my complete code:

    import java.awt.Image;
    import java.util.ArrayList;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Component;
    import java.awt.image.BufferedImage;
    import javax.swing.Timer;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
     
    public class Animate extends Component
    {
     
    	Timer timer;
    	BufferedImage img;
    	private final ArrayList<BufferedImage> walk;
    	private final ArrayList<BufferedImage> punch;
    	private final ArrayList<BufferedImage> kick;
    	private final ArrayList<BufferedImage> back;
    	private final ArrayList<BufferedImage> stand;
    	private Animate anim;
    	private int x = 0;
    	private int y = 0;
    	private int dx = 0;
    	private final int THIS_X = 170;
    	private final int THIS_Y = 20;
    	private int key;
    	private Graphics g;
     
    	public Animate(Sprite anything)
    	{
    		ActionListener listener = new TimerListener();
    		timer = new Timer(1000, listener);
    		timer.setInitialDelay(1000);
    		walk = anything.spriteWalk();
    		punch = anything.spritePunch();
    		kick = anything.spriteKick();
    		back = anything.spriteBackStep();
    		stand = anything.spriteStand();
    		//this.setX(THIS_X);
    		//this.setY(THIS_Y);
     
    		anim = this;
     
    	}	
     
    	 public void stand()
    	{
    		for(int i=0; i<=stand.size(); i++)
    		{
    			if(i== stand.size())
    				img = stand.get(1);
     
    			img = stand.get(i);
    			timer.start();
     
    		}
    	}
     
    	public void punch()
    	{
    		for(int i=0; i<=punch.size()-1; i++)
    		{
    			img= punch.get(i);
    			timer.start(); 
     
    		}
    	}
     
    	public void kick()
    	{
    		for(int i=0; i<=kick.size()-1; i++)
    		{
    			img= kick.get(i);
    			timer.start(); 
    		}
    	}
     
    	public void walk()
    	{
    		for(int i=0; i<=walk.size(); i++)
    		{
    			if(i == walk.size())
    				i = 1;
    			img = walk.get(i);
    			timer.start();
    		}
    	}
     
    	public void back()
    	{
    		for(int i=0; i<=back.size(); i++)
    		{
    			if(i== back.size())
    				i = 1;
    			img= back.get(i);
    			timer.start(); 
    		}
    	}
     
    	public void paint(Graphics g)
    	{
    		//recover Graphics2D
    		Graphics2D g2 = (Graphics2D) g;
    		g2.drawImage(img, dx, y, null);
    	}
     
     
     
        class TimerListener implements ActionListener
        {
        	public void actionPerformed(ActionEvent event)
        	{
        		repaint();
        	}
        }
     
    }


    import javax.swing.JFrame;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    public class Tester
    {
    	public static void main(String[] args)
    	{
    	JFrame frame = new JFrame();
     
    	Sprite andy = new Andy();
     
    	final Animate anim = new Animate(andy);
     
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setSize(700,200);
     
    	frame.setVisible(true);
    	frame.add(anim);
    	anim.walk();
     
    	}
     
    }


    import java.util.ArrayList;
    /*
     *This interface provides the methods for creating the arrays of characters
     *that are animated. It is an interface to provide commonality for characters
     *so there is only need for one Animate class, and not four.
     */
    public interface Sprite
    {
    	public ArrayList spriteWalk();
     
    	public ArrayList spritePunch();
     
    	public ArrayList spriteKick();
     
    	public ArrayList spriteBackStep();
     
    	public ArrayList spriteStand();
    }


    import java.util.ArrayList;
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;
    import java.io.*;
    import java.io.IOException;
     
     
    /* Inside this character class, the methods for creating the ArrayLists
     *for the animation of the characters are supplied.
     */
     
    public class Andy implements Sprite
    {
    	private ArrayList<BufferedImage> spriteWalker;
    	private ArrayList<BufferedImage> spritePunch;
    	private ArrayList<BufferedImage> spriteKick;
    	private ArrayList<BufferedImage> spriteStand;
    	private ArrayList<BufferedImage> spriteBackStep;
    	BufferedImage walker1;
    	BufferedImage walker2;
    	BufferedImage walker3;
    	BufferedImage walker4;
    	BufferedImage punch1;
    	BufferedImage punch2;
    	BufferedImage punch3;
    	BufferedImage punch4;
    	BufferedImage punch5;
    	BufferedImage punch6;
    	BufferedImage punch7;
    	BufferedImage punch8;
    	BufferedImage punch9;
    	BufferedImage kick1 ;
    	BufferedImage kick2;
    	BufferedImage kick3;
    	BufferedImage kick4;
    	BufferedImage kick5;
    	BufferedImage kick6;
    	BufferedImage kick7;
    	BufferedImage kick8;
    	BufferedImage stand1;
    	BufferedImage stand2;
    	BufferedImage stand3;
    	BufferedImage stand4;
    	BufferedImage stand5;
    	BufferedImage stand6;
    	BufferedImage stand7;
    	BufferedImage backstep1;
    	BufferedImage backstep2;
    	BufferedImage backstep3;
    	BufferedImage backstep4;
    	BufferedImage backstep5;
     
    	public Andy()
    	{
    		spriteWalker = new ArrayList<BufferedImage>();
    		spritePunch = new ArrayList<BufferedImage>();
    		spriteKick = new ArrayList<BufferedImage>();
    		spriteStand = new ArrayList<BufferedImage>();
    		spriteBackStep = new ArrayList<BufferedImage>();
     
    		spriteWalk();
    		spritePunch();
    		spriteKick();
    		spriteStand();
    		spriteBackStep();
     
    	}
     
    	//Adds objects of type Image to the spriteWalk array
    	public ArrayList spriteWalk()
    	{
    		try
    		{
    		walker1 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_walk1.gif"));
    		walker2 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_walk2.gif"));
    		walker3 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_walk3.gif"));
    		walker4 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_walk4.gif"));
    		}
    		catch(IOException e)
    		{
    		}
    		spriteWalker.add(walker1);
    		spriteWalker.add(walker2);
    		spriteWalker.add(walker3);
    		spriteWalker.add(walker4);
     
    		return spriteWalker;
    	}
     
    	public ArrayList spritePunch()
    	{
    		try
    		{		
    		punch1 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_punch1.gif"));
    		punch2 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_punch2.gif"));
    		punch3 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_punch3.gif"));
    		punch4 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_punch4.gif"));
    		punch5 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_punch5.gif"));
    		punch6 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_punch6.gif"));
    		punch7 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_punch7.gif"));
    		punch8 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_punch8.gif"));
    	    punch9 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_punch9.gif"));
    		}
    		catch(IOException e)
    		{
    		}
    		spritePunch.add(punch1);
    		spritePunch.add(punch2);
    		spritePunch.add(punch3);
    		spritePunch.add(punch4);
    		spritePunch.add(punch5);
    		spritePunch.add(punch6);
    		spritePunch.add(punch7);
    		spritePunch.add(punch8);
    		spritePunch.add(punch9);
     
    		return spritePunch;
    	}
     
    	public ArrayList spriteKick()
    	{
     
    		try{
    		kick1 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_kick1.gif"));
    		kick2 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_kick2.gif"));
    		kick3 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_kick3.gif"));
    		kick4 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_kick4.gif"));
    		kick5 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_kick5.gif"));
    		kick6 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_kick6.gif"));
    		kick7 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_kick7.gif"));
    		kick8 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_kick8.gif"));
    		}
    		catch(IOException e)
    		{
    		}
    		spriteKick.add(kick1);
    		spriteKick.add(kick2);
    		spriteKick.add(kick3);
    		spriteKick.add(kick4);
    		spriteKick.add(kick5);
    		spriteKick.add(kick6);
    		spriteKick.add(kick7);
    		spriteKick.add(kick8);
     
    		return spriteKick;
    	}
     
    	public ArrayList spriteStand()
    	{
     
     
    		try
    		{
    		stand1 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_stand1.gif"));
    		stand2 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_stand2.gif"));
    		stand3 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_stand3.gif"));
    		stand4 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_stand4.gif"));
    		stand5 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_stand5.gif"));
    		stand6 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_stand6.gif"));
    		stand7 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_stand7.gif"));
    		}
    		catch(IOException e)
    		{
    		}
    		spriteStand.add(stand1);
    		spriteStand.add(stand2);
    		spriteStand.add(stand3);
    		spriteStand.add(stand4);
    		spriteStand.add(stand5);
    		spriteStand.add(stand6);
    		spriteStand.add(stand7);
     
    		return spriteStand;
    	}
     
    	public ArrayList spriteBackStep()
    	{
     
    		try
    		{			
    		backstep1 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_backstep1.gif"));
    		backstep2 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_backstep2.gif"));
    		backstep3 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_backstep3.gif"));;
    		backstep4 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_backstep4.gif"));;
    		backstep5 = ImageIO.read(this.getClass().getResource("Sprites/Andy/andy_backstep5.gif"));;
    		}
    		catch(IOException e)
    		{
    		}
    		spriteBackStep.add(backstep1);
    		spriteBackStep.add(backstep2);
    		spriteBackStep.add(backstep3);
    		spriteBackStep.add(backstep4);
    		spriteBackStep.add(backstep5);
     
    		return spriteBackStep;
    	}
     
     
    }

    I included the Sprite interface and the Andy class, in case that was what was causing the problem.
    Last edited by FShounen; May 19th, 2011 at 09:25 AM.

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Sprite Animation with ArrayLists

    The problem is, you don't need a for loop at all. The only time you want to increment the sprite being displayed is in the Timer's ActionListener.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Junior Member
    Join Date
    May 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sprite Animation with ArrayLists

    Thanks so much Mr. Workman, you've really helped me a lot. It seems like my Animate class is nearly done, except for one small problem I can't seem to fix. Everything you've suggested at this point has worked.

    import java.awt.Image;
    import java.util.ArrayList;
    import java.awt.event.KeyEvent;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Component;
    import java.awt.image.BufferedImage;
    import javax.swing.Timer;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    /*This class is the meat and potatoes of the animation (so it's only
     *natural that it is named Animate) this class creates the individual
     *ArrayLists for the animations, and animates them, paints them, and
     *shows them inside the frame window of the game. It also keeps
     *track of where the character is on screen, and updates it in
     *relation to which buttons are pressed.
     */
     
     
    public class Animate extends Component
    {
     
    	Timer timer;
    	BufferedImage img;
    	private final ArrayList<BufferedImage> walk;
    	private final ArrayList<BufferedImage> punch;
    	private final ArrayList<BufferedImage> kick;
    	private final ArrayList<BufferedImage> back;
    	private final ArrayList<BufferedImage> stand;
    	private Animate anim;
    	private int x = 0;
    	private int y = 100;
    	private int dx = 0;
    	private final int THIS_X = 170;
    	private final int THIS_Y = 20;
    	private int key;
    	private Graphics g;
    	int walkindex = 0;
    	int punchindex = 0;
    	int kickindex = 0;
    	int backindex = 0;
    	int standindex = 0;
    	boolean walking;
    	boolean backing;
    	boolean punching;
    	boolean kicking;
    	boolean standing;
     
    	public Animate(Sprite anything)
    	{
    		ActionListener listener = new TimerListener();
    		timer = new Timer(150, listener);
    		walk = anything.spriteWalk();
    		punch = anything.spritePunch();
    		kick = anything.spriteKick();
    		back = anything.spriteBackStep();
    		stand = anything.spriteStand();
    		//this.setX(THIS_X);
    		//this.setY(THIS_Y);
     
    	}
     
    	 public void stand()
    	{
    		standing = true;
    		timer.start();
    	}
     
    	public void punch()
    	{
    		punching = true;
    		timer.start();
    	}
     
    	public void kick()
    	{
    		kicking = true;
    		timer.start();
    	}
     
    	public void walk()
    	{
    		walking = true;
    		timer.start();
    	}
     
    	public void back()
    	{
    		backing = true;
    		timer.start();
    	}
     
     
    	public void paint(Graphics g)
    	{
    		//recover Graphics2D
    		Graphics2D g2 = (Graphics2D) g;
    		g2.drawImage(img, dx, y, null);
    	}
     
     
        class TimerListener implements ActionListener
        {
        	public void actionPerformed(ActionEvent event)
        	{
     
    				if(walking)
    				{
    					if(walkindex == walk.size())
    						walkindex = 1;
    					img = walk.get(walkindex);
    					walkindex++;
    					repaint();
    				}
     
    				if(punching)
    				{
    					if(punchindex == punch.size())
    					{
    						punchindex=0;
    						punching = false;
    					}
    					img = punch.get(punchindex);
    					punchindex++;
    					repaint();
    				}
     
    				if(kicking)
    				{
    					if(kickindex == kick.size())
    					{
    						kicking = false;
    						kickindex = 0;
    					}
    					img = kick.get(kickindex);
    					kickindex++;
    					repaint();
    				}
     
    				if(standing)
    				{
    					if(standindex == stand.size())
    						standindex = 1;
    					img = stand.get(standindex);
    					standindex++;
    					repaint();
    				}
     
    				if(backing)
    				{
    					if(backindex == back.size())
    						backindex = 1;
    					img = back.get(backindex);
    					backindex++;
    					repaint();
    				}
     
     
        	}
        }
     
        public void keyPressed(KeyEvent e)
        {
            key = e.getKeyCode();
     
            if (key == KeyEvent.VK_LEFT)
            {
            	back();
                dx -= 3;
                x += dx;
            }
     
            if (key == KeyEvent.VK_RIGHT)
            {
            	walk();
                dx += 3;
                x += dx;
            }
     
            if (key == KeyEvent.VK_CONTROL)
    		{
    			punch();
    		}
     
    		else if (key == KeyEvent.VK_ALT)
    		{
    			kick();
    		}
     
    		if (standing)
    			standing = false;
     
        }
     
        public void keyReleased(KeyEvent e)
        {
            int key = e.getKeyCode();
     
            	stand();
     
            if(backing)
            	backing = false;
            if(walking)
            	walking = false;
            if(punching)
            	punching = false;
            if(kicking)
            	kicking = false;
     
        }
     
    }

    This code make the sprite respond to keyboard stimulus, when you press left he goes left with the back animation, when you press right he goes right with the walking animation etc. But for some reason you have to hold down the ctrl and alt keys to make it punch and kick. Do you know how I can make it so that you only have to press for him to punch and kick?

  12. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Sprite Animation with ArrayLists

    Well, you set punching and kicking to false when the button is released, so that's going to cause the animation to stop.

    I think what you want to do is force that animation to play the whole way through once and not repeat, unlike walking.

    And just a word to the wise, I'd also put your player movement in the Timer, not in the KeyListener- I would have the Timer fire several times a second (whatever the FPS of your game is, 15-60 times a second generally). You'd only update the sprites or the player position every x amount of frames. But the general idea is that the Timer calls the game loop, which calls everything else. Otherwise, your game is going to depend on things like key repeat rate, which you don't want.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    Junior Member
    Join Date
    May 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sprite Animation with ArrayLists

    I'm a little confused as to what you mean by forcing it to run the whole way through. I thought that what I had WAS going to force it to run, but it isn't. Even when I rem out the code that assigns the punching boolean to false, it still stops animating the punch when I release the key.

    There probably is some code I haven't learned in class tha explains how to get this to work properly @_@

        class TimerListener implements ActionListener
        {
        	public void actionPerformed(ActionEvent event)
        	{
     
    				if(walking)
    				{
    					if(walkIndex == walk.size())
    						walkIndex = 1;
    					img = walk.get(walkIndex);
    					walkIndex++;
    					repaint();
    				}
     
    				if(punching)
    				{
    					if(punchIndex == punch.size())
    					{
    						punchIndex=0;
    						punching = false;
    					}
    					img = punch.get(punchIndex);
    					punchIndex++;
    					repaint();
    				}
     
    				if(kicking)
    				{
    					if(kickIndex == kick.size())
    					{
    						kicking = false;
    						kickIndex = 0;
    					}
    					img = kick.get(kickIndex);
    					kickIndex++;
    					repaint();
    				}
     
    				if(standing)
    				{
    					if(standIndex == stand.size())
    						standIndex = 1;
    					img = stand.get(standIndex);
    					standIndex++;
    					repaint();
    				}
     
    				if(backing)
    				{
    					if(backIndex == back.size())
    						backIndex = 1;
    					img = back.get(backIndex);
    					backIndex++;
    					repaint();
    				}
     
     
        	}
        }
     
        public void keyPressed(KeyEvent e)
        {
            key = e.getKeyCode();
     
            if (key == KeyEvent.VK_LEFT)
            {
            	if(dx>10)
            	{
            		back();
                	dx -= 3;
            	}
            }
     
            if (key == KeyEvent.VK_RIGHT)
            {
            	if(dx<675)
            	{
            		walk();
                	dx += 3;
            	}
     
            }
     
            if (key == KeyEvent.VK_CONTROL)
    		{
    			punch();
    		}
     
    		else if (key == KeyEvent.VK_ALT)
    		{
    			kick();
    		}
     
     
    		if (standing)
    			standing = false;
     
        }
     
        public void keyReleased(KeyEvent e)
        {
     
            int key = e.getKeyCode();
     
            	stand();
     
            if(backing)
            	backing = false;
            if(walking)
            	walking = false;
            //if(punching)
            //	punching = false;
            //if(kicking)
            //	kicking = false;
     
        }

  14. #14
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Sprite Animation with ArrayLists

    That's because in your keyReleased() method, you call stand(), which I gather sets the animation to the standing animation (or the standing sprite, whatever). You probably want to check which key was released- only call stand() if no walking or animating should be done (for example, what happens if I hold in left and right, then release one of the keys?).
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Moving sprite leaves a trail
    By chris2307 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 26th, 2011, 09:01 AM
  2. Encapsulation question regarding ArrayLists
    By LDM91 in forum Collections and Generics
    Replies: 3
    Last Post: October 27th, 2010, 11:51 AM
  3. Alphabet from sprite sheet
    By Asido in forum Java Theory & Questions
    Replies: 2
    Last Post: September 18th, 2010, 11:49 AM
  4. Transferring elements between arraylists
    By KipTheFury in forum Collections and Generics
    Replies: 6
    Last Post: August 23rd, 2010, 02:13 PM
  5. Problem with sprite rotation in graphics
    By Katotetu in forum Algorithms & Recursion
    Replies: 0
    Last Post: May 8th, 2009, 07:27 PM