سؤال

Just a forewarning: I'm new to java - I normally use UnrealScript and C# but I'm branching out, so there is likely one or two things I've done incorrectly or against the normal java convection (that would be more in align with convictions of those other two languages)

Feel free to point them out, and I'll mold myself to them accordingly


I'm making a JRPGish style game in java using BlueJ. I'm not aiming massively high, and am more doing it as a proof-of-concept rather than a full blown game.

So far its going ok. I'm got a sprite animation working using a sprite sheet and the player can walk around with the sprite changing to the correct animation depending on the direction.

However I'm having an issue when the player stops moving - there sometimes is a afterimage of the previous frame - In fairness, this may be happening all the time except you cant see it if the player stops moving on the first and last frame of the walking animations, as those take up the same pixel space and thus are hidden (if that makes sense)

Here is an image of the issue in action:

 Player after moving right

This is after the player has moved, and then stopped, leaving the last frame of the "moveRight" sprite behind.


I have created a small version of my project that has just the character animation playing when you press a key, and stopping when you release, in which the issue appears

The Skeleton class

The Character class

The GameManager class

The KeyManager class

To start the game run the Main method in GameManager

You'll need to save this image with the filename of "James.png" and place it in the same folder of the java project for it to work

enter image description here

Thanks in advance for any help given.

هل كانت مفيدة؟

المحلول

paintComponent() passes Graphics to drawCharactor(). drawCharactor() should not disposes that Graphics object unless you made a copy, this Graphics is shared.

Also, do not call repaint() from drawCharactor(). repaint() schedules another paint cycle. You already do call repaint() from a timer.

Do not use java.util.Timer use javax.swing.Timer for Swing painting. See How to Use Swing Timers

For more information and examples Performing Custom Painting and Painting in AWT and Swing.

Consider posting a minimal example.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top