Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Sprite Animation Tutorial  (Read 2480 times)
0 Members and 1 Guest are viewing this topic.
Offline Samdy

Junior Newbie





« Posted 2013-10-25 03:53:25 »

Hi, I'm a newbie in java game programming, and I read some articles regarding Sprite Animation and has no luck understand it. Do you guys know a simple tutorial to understand the basic of it?
thanks.
Offline Rayexar
« Reply #1 - Posted 2013-10-25 06:22:14 »

I don't know a simple tutorial right now, but what specifically don't you understand? Is it something about the general process of sprite animations, or how to actually use?
Offline trollwarrior1
« Reply #2 - Posted 2013-10-25 09:07:37 »

Hi, I'm a newbie in java game programming, and I read some articles regarding Sprite Animation and has no luck understand it. Do you guys know a simple tutorial to understand the basic of it?
thanks.

It's pretty simple. Do you know how to render a basic sprite? If no, then you shouldn't be even thinking about sprite animation. First step is to know a way to render sprite onto the screen.

Once you know that, you can simple do something like this:
-make an array of sprites (say array which contains 3 different sprites);
-make an integer which should always be within 0-2 in this case (because we have an array of 3 members);
-take a sprite from that array passing your integer you just created as an index;
-adjust that integer to your liking (this will change the frame displayed);

This would like something likes this in code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
public class SpriteDemo {
     
      private Sprite[] sprites = new Sprite[];
      private int currentSprite = 0; // should be from 0 to sprites.length - 1;
     
      public SpriteDemo() {
         
         //Fill your array of sprites with sprites of some sort. All should be different
         //if you want to get animation look.
         for(int i=0;i<sprites.length;i++) {
            sprites[i] = new Sprite();
         }
      }
     
      public void update() {

         currentSprite += 1; // increase frame counter by 1
         
         //check and make sure animation counter is within your sprite
         //array bounds.
         if(currentSprite >= sprites.length) {
            currentSprite = 0; // reset animation to start
         }
         
         
      }
     
      public void render() {
         sprites[i].draw();
      }


Don't try to enter this code and expect it to work. This is just a template of how animation class might look like.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Dxu1994
« Reply #3 - Posted 2013-10-25 09:12:25 »

Hi, I'm a newbie in java game programming, and I read some articles regarding Sprite Animation and has no luck understand it. Do you guys know a simple tutorial to understand the basic of it?
thanks.

It's pretty simple. Do you know how to render a basic sprite? If no, then you shouldn't be even thinking about sprite animation. First step is to know a way to render sprite onto the screen.

Once you know that, you can simple do something like this:
-make an array of sprites (say array which contains 3 different sprites);
-make an integer which should always be within 0-2 in this case (because we have an array of 3 members);
-take a sprite from that array passing your integer you just created as an index;
-adjust that integer to your liking (this will change the frame displayed);

This would like something likes this in code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
public class SpriteDemo {
     
      private Sprite[] sprites = new Sprite[];
      private int currentSprite = 0; // should be from 0 to sprites.length - 1;
     
      public SpriteDemo() {
         
         //Fill your array of sprites with sprites of some sort. All should be different
         //if you want to get animation look.
         for(int i=0;i<sprites.length;i++) {
            sprites[i] = new Sprite();
         }
      }
     
      public void update() {
         
         //check and make sure animation counter is within your sprite
         //array bounds.
         if(currentSprite >= sprites.length) {
            currentSprite = 0; // reset animation to start
         }
         
         currentSprite += 1; // increase frame counter by 1
         
      }
     
      public void render() {
         sprites[i].draw();
      }


Don't try to enter this code and expect it to work. This is just a template of how animation class might look like.

Your code has a bug:

1  
2  
3  
4  
5  
         if(currentSprite >= sprites.length) {
            currentSprite = 0; // reset animation to start
         }
         
         currentSprite += 1; // increase frame counter by 1


This will cause currentSprite to never equal zero, therefore the first frame will never be displayed.

What it should be:

1  
2  
3  
4  
5  
6  
         if(currentSprite == sprites.length - 1) {
            currentSprite = 0; // reset animation to start
         } else {
         
                 currentSprite ++; // increase frame counter by 1
                        }


Of course you want to run update with a timer or something.

Offline trollwarrior1
« Reply #4 - Posted 2013-10-25 09:32:06 »

Thanks DXU, I fixed the issue Tongue Didn't even read what I wrote.
Offline Samdy

Junior Newbie





« Reply #5 - Posted 2013-11-07 14:20:29 »

thx for the tutorial. I'll try to make some invaders move since i'm trying to make my own space invaders. THX
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (38 views)
2014-10-17 03:59:02

Norakomi (29 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (56 views)
2014-10-14 00:39:48

TehJavaDev (55 views)
2014-10-14 00:35:47

TehJavaDev (46 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!