My game is a rough pokemon clone. The world is tiled. The player movement is smooth and great, but the animation is not working and I can't think of a way to implement it correctly.Basicly, this functionality is my goal:When just pressing once (moving one tile), the animation should be mid-layer on the spritesheet, downer layer, and when hitting the right position still (upper layer).When holding down it should be mid-layer, upper layer, downer-layer, upper layer, and repeating (in order to get a nice walking animation).

In player, targX and -Y is the coords to where my player is going.map.targX and -Y is the pixel coords to where the map should stop.animPointX and -Y is the pixel location of where the player-image should change state (upper-layer to downer-layer or vice-versa).The appearenceUpdate boolean is a flag that is set when a movement-key is pressed. The animUpdate boolean, is a flag that is set when the image should change due to animation.

My ImagesLoader loads the spritesheet and slices it, making it available through coordinates via. a bufferedimage[][] (x, and y).

EDIT:I'm beginning to think that the calcNewCoords methods does not calculate the animPoints correctly, even though it seems to me that they are

calcNewCoords now only handles location targets, and the keylistener now just sets the isMoving boolean, and the moveDir (the direction the sprite is facing).

However, this doesn't work right: The animation isn't "smooth". The last frame (animation[2]) is usually a lot longer than the other two. I think this implementation of animating the walking is easier to manage, and the problem is smaller. I think what's happening, is that the animation-counter quickly reaches 2, and stays there for the two last frames. To me, it looks like it should not.

Yeah, it seems like you're thinking about it the wrong way. Try to go more object oriented. In reality, you've got three separate things:- The sprite sheet and the logic of which frame is where- The animation, which knows which frames get swapped after the others and how long the delay is between frames- The game logic, which decides when to show which animation

Usually for me this translates to my Entity class having an AnimationSet. An AnimationSet is a HashMap of Animation's with a String as a key, so I can say entity.setAnimation("WalkUp") and it will start playing that one. The AnimationSet has logic for priorities and whatnot so that certain Animation's automatically play by default (like an Idle animation) and others can't be interrupted (like a Death animation). Each Animation has an array of String's that reference Sprites stored in memory, so I can just draw a different image depending on what the current frame is. You seem to understand how a Sprite sheet / atlas works, so I won't bother explaining that part.

..so I can just draw a different image depending on what the current frame is.

About here I jumped off.. I'm using slick now, so basicly I can have my enities have a hashmap of animations (as in the objects in Slick), and play them according to the state of the entity (walking different directions and dying)?

I'm not sure if Slick lets me move it around once started.. Else I'll have to make it myself.. I think I know how to go about this, except for one thing: How do I know when to update the picture in the animation? Should I just let it play to the end? If so, what about holding down the key for continous walking? Replay, to the end?

I had a look at Slick, but decided to only use Java2D instead of any extra libraries. My animations are controlled with 2 integers for the direction which the character is facing(dirx, diry, where -1, 0 and 1 are values). This will help determine which image set to render.

My images are seperated instead of being on an image set, but there will just be a separate bit of programming for you to do. Whenever the actionListioner() is called by your timer, another method will alter another variable which changes the image currently being displayed. Here is a code representation, please keep in mind this only represents a single direction:

Oh, and one other thing, it may be useful to use the 'Sprite' class found in the tools directory of your Slick download. It represents a single sprite in your program, but you could make an AnimatedSprite class which extends Sprite and impliments the above code or something somehow... Just an idea

I had a look at Slick, but decided to only use Java2D instead of any extra libraries. My animations are controlled with 2 integers for the direction which the character is facing(dirx, diry, where -1, 0 and 1 are values). This will help determine which image set to render.

My images are seperated instead of being on an image set, but there will just be a separate bit of programming for you to do. Whenever the actionListioner() is called by your timer, another method will alter another variable which changes the image currently being displayed. Here is a code representation, please keep in mind this only represents a single direction:

publicvoidchangeImg()//A method to change the image to be displayed{currentImage++;//Increase the current image variable

if(currentImage==3)//If the current image is '3', which cant be used to get the image later, set it to 0.currentImage=0;}

publicImagegetImage(){returnimgArr[currentImage];}

This way, you can change the image in the paint() method of your program. Hope this helps a bit!

Hi N15MO

I actually used that technique, but for me it played too fast over the first image, and then coughed when reaching the end before planned. I also had an event where the sprite reached the position, where I would set the image back to the "standing" image. A combination of that worked really bad. However, I think just setting the "standing" image, when the animation is done will fix that. The problems are just going to fall on me when I reach a point, where I'd like to implement NPC's that are able to walk. I also have the pseudocode, but I doubt it'll actually work Oh, and do you mind explaining why you use two integers, for the direction?I like to just have one integer/short/byte going from 1 through 4. North, east, south, and west.

I'm not sure how Slick works, but you update the animation by calling tick() on it every timestep. That will then increment a counter, and when it reaches the delay for that frame it moves on to the next one.

I see youre predicament... Hmmmm i really havent done much to be able to help u with anything, but when i do i will post it here and show u. Because i am still pretty new to this forum, i cant past any links to websites, but please go ahead and type "pacman java 2d game tutorial" in Google and look at the one from Zetcode. It is what i am basing my animation around, also maybe look at their tutorial on animation itself and moving sprites, i think there will be valuable information in there to help u

Concerning the two integer variables, becasue its 2d we're working in, u may want a character to to so something really strange like strafe or side step, and u cannot account for that with one variable... but if you only intend to have the character going in the usual up/down/left/right sort of fashion, it can suffice too when i have some spare time this week, ill take a look at your code and maybe help out a bit if i can

I plan to have my project finished in the next 2/3 weeks before university starts

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