Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
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  
  Building a better animation infrastructure  (Read 594 times)
0 Members and 1 Guest are viewing this topic.
Offline DeepBlue97

Junior Newbie





« Posted 2013-05-16 00:20:11 »

So I'm working on a really basic "game" just to really test out sprite animation and make a decent, reusable infrastructure.
basically in GraphicsShell I set up a loopGame that double buffers and renders, and renders then updates.
I set it up to to loop every 30 milliseconds, in which it calls the draw methods of my background class and player class.
in player, I use to a subclass to load multiple frames into an arrays.

Some animations I would like to be "forced" as the frames cant change until the array finished the animation, (for example, and attack animation would be executed one, and not repeated) while other animations could be changed mid array loop such (such as changing from a left facing run animation to a down facing animation)

GraphicsShell


package gameshellv2;  

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  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
//imports  
public class GraphicsShell extends JFrame {  
     
    int x = 0,y = 0;  
    private Image dbImage;  
    private Graphics dbg;  
   
    int numOfTimes;  
    long Interval;  
     
 
    Player playerA;  
    Background masterBackground;  
    BufferedImage sprite;  
    final int ANIM_SPEED = 30;  
     
    public static void main (String[] args) {  
     
        GraphicsShell MasterShell = new GraphicsShell();  
 
    }  
    public GraphicsShell(){  
     
      //setting up the JFrame  
       
        lnitialize();  
        loopGame();  
         
         
    }  
    public void loopGame() {  
     
         
        long time1= System.currentTimeMillis();  
 
    numOfTimes = 1;  
    long time2 = time1 + 1;  
    while (true) {  
                long time3= System.currentTimeMillis();  
         
                 
        if (time2 <= time3) {  
         
                    time1 = System.currentTimeMillis();  
                gameRender();  
                gameUpdate();  
                 
                time2 = time1+ANIM_SPEED;  
                 
                numOfTimes++;  
                if (numOfTimes == 31) {  
                numOfTimes = 0;  
                 
                }  
                }  
    }  
    }  
    private void gameUpdate() {    
        Graphics g = this.getGraphics();  
        g.drawImage(dbImage, 0, 0, this);        
    }  
   
    private void lnitialize() {  
     //setting up references to BG and Player  
      //setting up doubleBuffer references for the sake    
   }  
    private void gameRender() {          
        dbg = masterBackground.draw(dbg,numOfTimes);  
        dbg = playerA.draw(dbg,numOfTimes);  
    }  
    public class AL extends KeyAdapter {  
      key pressed and released stuff being passed to playerA  
        }  
    }        
}  


Player


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  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
package gameshellv2;  
 
//setting up imports  
 
public class Player implements Drawable{  
   
    int x =10,  y=10;  
   
 setting up sprite getters  
     
//arrays  
 
 BufferedImage[] rightFrames,leftFrames,upFrames,downFrames,currentFrames;  
 BufferedImage[] rightIdleFrames,upIdleFrames,downIdleFrames,leftIdleFrames,lastFrames;  
 BufferedImage[] rightAttackStationary,leftAttackStationary,downAttackStationary;  
 
    final int PLAYER_BASE_SPEED = 2;  
    //keydown booleans  
     
 
 //more variables  
     
    public Player() {  
        //resets the frame # and idle delay  
       frameNumber = 0;  
        idleDelay = 0;  
         
         
        init();  
    }  
    public void init () {  
        //bufferedImage arrays init.  
       //image and subimage getter initialization  
       spriteManager SM = new spriteManager();  
        //filling the movement arrays  
       SM.setSpriteArrays(downFrames);  
        SM.setSpriteArrays(upFrames);  
       //fills the rest of the arrays  
 
         currentFrames = rightIdleFrames;  
    }  
    @Override  
    public Graphics draw(Graphics g, int numOfTimes) {  
         if (numOfTimes % 5 == 0 ) {  
         sprite = currentFrames[frameNumber];  
          frameNumber++;  
        }  
         
         if (isActionAnimRunning) {  
                 
                    System.out.println("if(isActionAnimRunning) has executed");  
                 
               if (frameNumber == 6 ) {  
                 
                   isActionAnimRunning = false;  
                   currentFrames = rightIdleFrames;  
                   frameNumber = 0;  
                     
               }  
                 }  
         checkKeys();  
     
         
        g.drawImage(sprite , x,y, null);  
         
        if (frameNumber == currentFrames.length ) {  
            frameNumber = 0;  
        }  
        return g;  
       }  
    public void checkKeys() {  
   
         
         
       if (!isActionAnimRunning) {attack();}  
       if(frameNumber == currentFrames.length -1) {isActionAnimRunning = false;}  
       move();  
         
         
         
    }  
   
    public void attack () {  
       if (isRightDown) {  
         
            frameNumber = 0;  
            isActionAnimRunning = true;  
            currentFrames = rightAttackStationary;  
     
       }  
        if (isDownDown) {  
     
            frameNumber = 0;  
            isActionAnimRunning = true;  
            currentFrames = downAttackStationary;  
     
         
         
       }  
     
    }  
    public void move () {  //only changes frame and coords every 5 loops  
    if (isWDown) {  
         if(!isActionAnimRunning) {currentFrames = upFrames;}  
         y = y-PLAYER_BASE_SPEED;  
         idleDelay = 0;  
         
     }  
       if (isADown) {  
           if(!isActionAnimRunning) {currentFrames = leftFrames;}  
           x = x-PLAYER_BASE_SPEED;  
       idleDelay = 0;  
       }  
       if (isSDown) {  
           y = y+PLAYER_BASE_SPEED;  
if(!isActionAnimRunning) {currentFrames = downFrames;}  
           idleDelay = 0;  
       }  
       if (isDDown) {  
if(!isActionAnimRunning) {currentFrames = rightFrames;}  
          x = x+PLAYER_BASE_SPEED;  
       idleDelay = 0;  
       }  
       if (!isWDown && !isADown && !isSDown&& !isDDown) {  
           idleDelay++;  
      }  
       if (!isWDown && !isADown && !isSDown&& !isDDown && idleDelay  == 1) {  
             
           idleDelay = 0;  
           if (currentFrames == rightFrames) {  
           currentFrames = rightIdleFrames;            
           }  
           if (currentFrames == leftFrames) {  
           currentFrames = leftIdleFrames;            
           }  
           if (currentFrames == upFrames) {  
           currentFrames = upIdleFrames;            
           }  
           if (currentFrames == downFrames) {  
           currentFrames = downIdleFrames;            
           }  
        }      
    }  
     
    public void keyPressed(KeyEvent e) {  
      int keyCode = e.getKeyCode();  
         
         
      if(keyCode == e.VK_W){  
          isWDown = true;  
          isADown = false;  
          isSDown = false;  
          isDDown = false;  
        }  
        //setting up A,S, and D booleans  
       }  
       if(keyCode == e.VK_RIGHT) {  
           isRightDown = true;  
           isLeftDown = false;  
           isDownDown = false;  
       }  
     
       //setting up left,right,and down booleans  
   }  
     
 public void keyReleased(KeyEvent e) {  
 int keyCode = e.getKeyCode();  
        if(keyCode == e.VK_W){  
          isWDown = false;  
        }  
       //setting key codes to false in a similar fashion  
         
 }  
 
    public class spriteManager {  
     
        public spriteManager () {  
           //setting up sprite getters,  
  }  
         
         public void setSpriteArrays ( BufferedImage[] array) {  
        if (array == rightFrames) {  
        array[0] =   ss.grabSprite(1,1,24,24);    
        array[1] =   ss.grabSprite(26,1,24,24);    
        array[2] =   ss.grabSprite(1,26,24,24);    
        array[3] =   ss.grabSprite(26,26,24,24);    
       
        //more setting up sprites in a similar fashion  
       }  
        }  
         }    
    }      
}      

I feel that my current animation method are inefficient, in your opinion, whats the best way to handle these things?
Offline Longarmx
« Reply #1 - Posted 2013-05-16 01:41:07 »

Please use [.code][/code.] braces.

Offline DeepBlue97

Junior Newbie





« Reply #2 - Posted 2013-05-16 02:58:42 »

Tis' done! sorry about that.  Any suggestions though?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Andre Lopes
« Reply #3 - Posted 2013-05-16 15:23:27 »

Tis' done! sorry about that.  Any suggestions though?


Are you using an inner class? Sprite manager inside player class?

Why?
Offline DeepBlue97

Junior Newbie





« Reply #4 - Posted 2013-05-16 16:34:43 »

yes, I am using an inner-sprite loading class, I just really did that because I wanted to isolate the hundred lines x,y,width,height code for loading the arrays.
Should I not? Is there a better way to do it?
Thanks in advance.
Offline 65K
« Reply #5 - Posted 2013-05-16 17:02:59 »

Create a dedicated Animation class which holds a bunch of frames, has a current frame, knows it animation speed, gets updated frequently to switch the current frame and is able to draw itself. Just a graphics work horse. No relation to the player whatsoever.

You can develop and test that completely independent of the player and the whole game.

Let the player use that, give it a reference to one current animation object. Exchange the animation object depending on the player's state like left/right moving, attacking, etc.
No images stored inside the player.
When the player needs to be rendered, call the animation object to do the hard work. Use the player to hold its location and movement, hit points, inventory, etc.

Offline DeepBlue97

Junior Newbie





« Reply #6 - Posted 2013-05-16 17:16:05 »


Hmm... seems like a good idea, Ill try it out. Thanks!  Grin
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.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (20 views)
2014-09-24 16:12:06

Grunnt (46 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (64 views)
2014-09-21 02:42:18

BurntPizza (36 views)
2014-09-21 01:30:30

moogie (42 views)
2014-09-21 00:26:15

UprightPath (51 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!