# Slight delay in drawing the next frame of animation then become smooth afterwards

This topic is 2177 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I need a fresh pairs of eyes to look at this issue. It is an interesting and sneaky problem.

How the code works:

The reason why my animation sequence is in double instead of int was because I was testing whether or not the delay issue was related to the sequence not being in a continous interval. Whether the sequence is in double or int, the problem still persists.

2D Images in .PNG format of the character are stored in an arraylist.

I will have a timeFrame start at zero.

To create the effect of the character moving upon a keyPressed event, the timeFrame will increment by one when the right arrow key is pressed. The timeFrame increment code is inside the keyPressed method.

Some may think it has to do with the interval in the animationSequence being too large but this is not the case because the animations after the next frame have no delay in them.

Problem:

There is a slight delay in my code that draws the next frame of animation in the animation sequence that consists of .PNG files. But after that next frame is drawn, the animation no longer has the slight delay unless the user decides to stop pressing the key and then press the arrow key again to encounter the issue again.

The delay seems to be appear in less than a second time. Definitely not a second or even more of a second. The delay is noticeable.

I think the problem has to do with the algorithm than how Java works. I might be wrong.

Note: The code is written in Java. I am using the Standard Java library. The code is trying to simulate smooth animation in 2D.

private double [] animationSequence = {0,8,16,24,32,40};
private ArrayList<Image> rightAnimationList;
private timeFrame = 0;

    public void draw(Graphics g) {

for(int i = 0; i < animationSequence.length;i++)
{
if(timeFrame <= animationSequence[i] && horizontalDirection.equals("right"))
{

g.drawImage(rightAnimationList.get(i),(int)position.getX(),(int)position.getY(),null);
break;
}

}

public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
// Handle the player keys
switch (e.getKeyCode()) {
case (KeyEvent.VK_RIGHT):

horizontalDirection = "right";

timeFrame += 1 ;

if (!isRightPressed) {
isRightPressed = true;

}
break;


Edited by warnexus

##### Share on other sites

idk if this is part of the reason (I kinda doubt it) but shouldnt "if (horizontalDirection.equals("right"))"be above the for loop otherwise you looping no matter what and when horizontalDirection.equals("right")) becomes true there is no guarentee that your for loop is at 0 it may be on 4 or 5 or any number when the if statement becomes true causing your animation to start on a odd frame then again this assumes that the kepressed runs on a different thread which i kinda doubt

Edited by zylski

##### Share on other sites

idk if this is part of the reason (I kinda doubt it) but shouldnt "if (horizontalDirection.equals("right"))"be above the for loop otherwise you looping no matter what and when horizontalDirection.equals("right")) becomes true there is no guarentee that your for loop is at 0 it may be on 4 or 5 or any number when the if statement becomes true causing your animation to start on a odd frame then again this assumes that the kepressed runs on a different thread which i kinda doubt

your suggestion is more along the line of executing the animation only when the character is in that particular state. While your suggestion does not solve my animation delay , you do have a nice point about drawing the animation given a specific condition. so thanks for that. I have modified the code.

To give you feedback, if I move the horizontal direction above the for loop or leave my code unchanged, there is a guarantee it will reach every frame of animation because the timeFrame increments by one which covers every number in the animation sequence represented as an int array.

my code is not starting at odd frames. It just that it delays executing the frame it is suppose to change to and then after that there is no delay.

Edited by warnexus

##### Share on other sites

I thought I post my solution for anyone who will encounter this issue later on during their game development hobby journey:

The idea is to not increase timeFrame during keyPressed but to increase it during the update method.

This approach ensures a smooth response to trigger the animation.

• 18
• 29
• 11
• 21
• 16