Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (843)
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  
  simple sprite animation loop  (Read 2007 times)
0 Members and 1 Guest are viewing this topic.
Offline ryuzaki

Junior Newbie

« Posted 2007-06-01 03:55:40 »

This has probably been asked before, but I searched the forums and couldn't find a specific answer...Anyway, I have three images that I want to loop when the up arrow key is pressed, three to loop when down is pressed, same for left and right (making it look like the sprite is walking)...The question is, how would I go about making this loop? Is the while loop best for this sort of thing? I've tried a few different things on my own but so far I haven't been able to make it loop like it should.
Offline ravenger

Senior Newbie

« Reply #1 - Posted 2007-06-01 08:12:44 »

what i would do, is to put the sprites in an array, and each frame increment the index of the array, because the game basicly and normally is one big while loop anyway where you handle input, logic and rendering each iteration of the main loop. When the index is equal to the size of the array, you can then reset the index to keep it looping.
Offline oNyx

JGO Coder

Medals: 2

pixels! :x

« Reply #2 - Posted 2007-06-01 09:15:01 »

This thing is easier with a 1D array. To change the slice method (from the other thread) accordingly is easy to do:
-make it return a 1D array
-change new BufferedImage[cols][rows] to new BufferedImage[cols*rows]
-before the two loops create some variable like int num=0;
-replace slices[x][y] with slices[num]
-add num++; at the end of the inner loop

Alright. Now that you have a 1D array you can define a single animation as a sequence of numbers. Eg {6,7,8,9,25,10,11,12}. A 2D array can hold all those animations (the arrays inside the array doesn't necessarily have the same length).

If you want to display that animation you don't need another loop, since you already got a game loop, which handles the logic and draws the stuff... over and over again.

So, in your logic bit you have like... some object which represents like... some little dude. This dude can have different states. When the state changes from one to another you reset the frame index (you set it to 0 - the first frame) and set the animation type (first dimension index) accordingly. If the state didn't change you increase the frame index. If the frame index isn't < length anymore you can either change it to 0 (making it loop) or set it to 0 and change the animation type (for making a specific animation stop).


If the animation is very simple. Like the walk animation of Rick Dangerous or a Shyguy from Mario... then you only have 3 frames, which are played very quickly... and it doesn't really matter if you start with the "right" one. In that case you can use some variable, which is increased once each loop and the frame is picked via %3 (or %foo[type].length heh).

弾幕 ☆ @mahonnaiseblog
Pages: [1]
  ignore  |  Print  

EgonOlsen (42 views)
2018-06-10 19:43:48

EgonOlsen (22 views)
2018-06-10 19:43:44

EgonOlsen (43 views)
2018-06-10 19:43:20

DesertCoockie (197 views)
2018-05-13 18:23:11

nelsongames (124 views)
2018-04-24 18:15:36

nelsongames (123 views)
2018-04-24 18:14:32

ivj94 (863 views)
2018-03-24 14:47:39

ivj94 (124 views)
2018-03-24 14:46:31

ivj94 (768 views)
2018-03-24 14:43:53

Solater (140 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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‑
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!