What I am trying to do is create an animation that creates a 'running' motion. Whenever I draw it on the screen, the last frame in the animation is left behind (So there is a trail of animation frames left behind when the sprite moves). I've tried if statements and changing the image's draw position when the frame changes:
if(a2.sceneNum() == 0)
spectre_Draw1 = (screenWidth() / 2 - 120 / 2 + 120 - 6);
else
spectre_Draw1 = 0;
g.drawImage(pic[2], spectre_Draw1, (screenHeight() / 2 - 180 / 2), null);
if(a2.sceneNum() == 1)
spectre_Draw2 = (screenWidth() / 2 - 120 / 2 + 120 - 6);
else
spectre_Draw2 = 0;
g.drawImage(pic[3], spectre_Draw2, (screenHeight() / 2 - 180 / 2), null);
if(a2.sceneNum() == 2)
spectre_Draw3 = (screenWidth() / 2 - 120 / 2 + 120 - 6);
else
spectre_Draw3 = 0;
g.drawImage(pic[4], spectre_Draw3, (screenHeight() / 2 - 180 / 2), null);
Is there a way to do this while removing the trailing images?
Note: Code below in example program has some logic errors. Please see answer from Warren K for explanation and fixes
I noticed that you're trying to use different images for the animation image. You know you could use a single animation sprite (given it is formatted in a grid like pattern) and just change the locations of the certain
x
y
positions in thedrawImage
methodSee full description API
That being said, You can use a
javax.swing.Timer
to animate and change the locations of the source image.here are some examples using this same code for all the example you see below. I just changed the image and change the
SPRITE_ROWS
,SPRITE_COLUMNS
, andDELAY
accordingly. See more at How to Use Swing TimesI know it says "Avoid responding to other answers" but as a new registrant (but long-time user) I am only eligible to write "answers" or suggest edits.. I can't add comments to an answer until I am upvoted ! :-)
However, there are just too many errors in the source code in peeskillet's answer to qualify simply as "suggested edits" :-)
Accidental interchange / confusion of ROWS and COLUMNS :
Further down the code, the program is clearly trying to treat rows and columns of the animation pictures in the grid correctly, but it is only because both of these are backwards that the program works. (ie there are 5 Columns and 2 Rows in the grid of images for the sample nerdgirl).
Accidental interchange of DIM_H and DIM_W (1 occurrence) :
"DIM_H" should be "DIM_W"
As listed, this code would cause a premature jumping to the next row of animation images in the grid (not showing the last image in each row) in the case where the individual images are either significantly taller than they are wide, or else there are many images in each row of the original grid. With the nerdgirl sample (833 x 639), the last image in each row would not have been shown if the overall grid had been just 10 pixels taller. (DIM_H = 319, img.getWidth()-DIM_H-5 = 503 and the last frame shows when x1Src = 498 ..only just made it !)
This is because the following if-test :
... should be :
..as the current code only ever displays 2 rows of sprite images even if there were more. (This explains why the flapping bird sample works "ok" without showing the final all-white frame... look carefully and it only shows the first 2 rows of images).
Use of hard-coded 5 for "loss of precision" (meaning when the source images have not been composed evenly into the combined grid and they are not an exact multiple of the frame size) should actually refer to the number of frames present :
This is because the possible "loss of precision" is no more than 1 per frame in each row and column - resulting from the integer rounding that occurs when the animation image width and height are calculated. eg The nerdgirl sample at 833 x 639 calculates an animation frame size of 166 x 319, so after 5 images we have shown up to 830,638 of the original image.
However, I'd suggest the following would make both those if-statements much clearer and simpler :
... and there are a few other little tweaks as shown below.
If the grids were all compiled as a whole number multiple of the individual frame-size (as is the case with the running man png image @ 900x600 - even though the sample provided has picture "overlap" - see image link above), then the program code for the coordinate-checking could be even easier :
However, as input samples grids are unreliable in overall size and composition, the earlier code is recommended as it will cope with these inconsistencies.
I hope my answer saves some others valuable time in the long run !
Cheers,