Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (553)
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 1081 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).

Basically.

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  
 
 
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.

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56

Norakomi (42 views)
2014-08-06 19:49:38
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!