#Animation Problems
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
There are a few common reasons why animations may flicker in Java Swing:
-
Inefficient rendering: Make sure that you are using double buffering to reduce flickering. Double buffering involves drawing the animation off-screen and then displaying it all at once, which can help prevent flickering.
-
Improper timing: Make sure that you are updating and repainting your animation at a consistent rate. If the updates are happening too quickly or too slowly, it can cause flickering.
-
Overlapping components: If you have multiple components overlapping each other in your animation, it can cause flickering as the components are redrawn.
-
Hardware acceleration: Sometimes hardware acceleration settings on your computer can cause issues with animations in Java Swing. Try disabling hardware acceleration to see if it improves the flickering.
-
Memory issues: If your animation is using a lot of memory, it can slow down the rendering process and cause flickering. Make sure to optimize your code and use resources efficiently.
By addressing these common issues, you should be able to reduce or eliminate the flickering in your Java Swing animations.
Without any information about your code i can only give random guesses to ur issue
package entities;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import javax.imageio.ImageIO;
import static utilz.Constants.PlayerConstants.;
import static utilz.Constants.Directions.;
public class Player extends Entity {
private BufferedImage[][] playerAnimation;
private int animationIndex, animationTick, animationSpeed = 25;
private int playerAction = IDLE;
private int playerDirection = -1;
private boolean moving = false;
public Player(float x, float y) {
super(x, y);
loadAnimation();
}
public void update() {
updateAnimation();
setAnimation();
updatePosition();
}
public void render(Graphics g) {
g.drawImage(playerAnimation[playerAction][animationIndex], (int) x, (int) y, 168, 168, null);
}
private void updateAnimation() {
animationTick++;
if (animationTick >= animationSpeed) {
animationTick = 0;
animationIndex++;
if (animationIndex >= getSpriteAmount(playerAction)) {
animationIndex = 0;
}
}
}
public void setDirection(int direction) {
this.playerDirection = direction;
moving = true;
}
public void setMoving(boolean moving) {
this.moving = moving;
}
private void setAnimation() {
if (moving) {
playerAction = WALKING;
} else {
playerAction = IDLE;
}
}
public void setMoving(boolean moving) {
this.moving = moving;
}
private void setAnimation() {
if (moving) {
playerAction = WALKING;
} else {
playerAction = IDLE;
}
}
private void updatePosition() {
if (moving) {
switch (playerDirection) {
case UP:
y -= 1;
break;
case DOWN:
y += 1;
break;
case LEFT:
x -= 1;
break;
case RIGHT:
x += 1;
break;
default:
break;
}
}
}
private void loadAnimation() {
InputStream is = getClass().getResourceAsStream("/player_animations.png");
try {
BufferedImage image = ImageIO.read(is);
playerAnimation = new BufferedImage[15][12];
for (int j = 0; j < playerAnimation.length; j++) {
for (int i = 0; i < playerAnimation[j].length; i++) {
playerAnimation[j][i] = image.getSubimage(i * 56, j * 56, 56, 56);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Looks for me like it does flick when you change direction to move
Please use this format for posting code:
```java
// Example java program
int value = 5;
System.out.println(value);
```
Which results in:
// Example java program
int value = 5;
System.out.println(value);
For syntax highlighting, you have to add the name of the language after the three backticks, like ```java. Please make sure to use exactly this format, so no space between the backticks and the language name, and a newline before the code starts. If done right, the syntax highlighting will even be applied to your text as you type, before sending.
oh okey
package entities;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import javax.imageio.ImageIO;
import static utilz.Constants.PlayerConstants.;
import static utilz.Constants.Directions.;
public class Player extends Entity {
private BufferedImage[][] playerAnimation;
private int animationIndex, animationTick, animationSpeed = 25;
private int playerAction = IDLE;
private int playerDirection = -1;
private boolean moving = false;
public Player(float x, float y) {
super(x, y);
loadAnimation();
}
public void update() {
updateAnimation();
setAnimation();
updatePosition();
}
public void render(Graphics g) {
g.drawImage(playerAnimation[playerAction][animationIndex], (int) x, (int) y, 168, 168, null);
}
private void updateAnimation() {
animationTick++;
if (animationTick >= animationSpeed) {
animationTick = 0;
animationIndex++;
if (animationIndex >= getSpriteAmount(playerAction)) {
animationIndex = 0;
}
}
}
public void setDirection(int direction) {
this.playerDirection = direction;
moving = true;
}
public void setMoving(boolean moving) {
this.moving = moving;
}
private void setAnimation() {
if (moving) {
playerAction = WALKING;
} else {
playerAction = IDLE;
}
}
public void setMoving(boolean moving) {
this.moving = moving;
}
private void setAnimation() {
if (moving) {
playerAction = WALKING;
} else {
playerAction = IDLE;
}
}
Detected code, here are some useful tools:
private void updatePosition() {
if (moving) {
switch (playerDirection) {
case UP:
y -= 1;
break;
case DOWN:
y += 1;
break;
case LEFT:
x -= 1;
break;
case RIGHT:
x += 1;
break;
default:
break;
}
}
}
private void loadAnimation() {
InputStream is = getClass().getResourceAsStream("/player_animations.png");
try {
BufferedImage image = ImageIO.read(is);
playerAnimation = new BufferedImage[15][12];
for (int j = 0; j < playerAnimation.length; j++) {
for (int i = 0; i < playerAnimation[j].length; i++) {
playerAnimation[j][i] = image.getSubimage(i * 56, j * 56, 56, 56);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Detected code, here are some useful tools:
Try to log the current image from playerAnimation which is used and view the logs when the flicker occur, maybe there is an empty image or else. I cant really see anything obvious in ur code unless fully debugging it
okey
nope but i see that is inconsistent sometimes it flikers sometimes works fine
with a simpler sprite works just fine
@tardy nexus share the sprite
It's shared alongside the video
private void loadAnimation() {
InputStream is = getClass().getResourceAsStream("/player_animations.png");
try {
BufferedImage image = ImageIO.read(is);
playerAnimation = new BufferedImage[15][12];
for (int j = 0; j < playerAnimation.length; j++) {
for (int i = 0; i < playerAnimation[j].length; i++) {
playerAnimation[j][i] = image.getSubimage(i * 56, j * 56, 56, 56);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Also... you know that try-with-resources is a thing ?
And don't catch Exception
The first message
ah thanks
Nope didn't know
then use it instead, it's way better
Okey ty
@tardy nexus are you using swing or awt ?
Swing
it seems to only do that at the end of an animation 🤔
But it's only sometimes
I tried another sprite simpler 16 bits 7 images per animation
And works perfect
i think i fixed it
private void setAnimation() {
int startAnimation = playerAction;
if (moving) {
playerAction = WALKING;
} else {
playerAction = IDLE;
}
if (playerAction != startAnimation) {
resetAnimationTick();
}
}
private void resetAnimationTick() {
animationTick = 0;
animationIndex = 0;
}
with this
Detected code, here are some useful tools:
private void setAnimation() {
int startAnimation = playerAction;
if (moving) {
playerAction = WALKING;
}
else {
playerAction = IDLE;
}
if (playerAction != startAnimation) {
resetAnimationTick();
}
}
private void resetAnimationTick() {
animationTick = 0;
animationIndex = 0;
}
and modified the loadAnimation:
private void loadAnimation() {
try (InputStream is = getClass().getResourceAsStream("/player_animations.png")) {
BufferedImage image = ImageIO.read(is);
playerAnimation = new BufferedImage[15][12];
for (int j = 0; j < playerAnimation.length; j++) {
for (int i = 0; i < playerAnimation[j].length; i++) {
playerAnimation[j][i] = image.getSubimage(i * 56, j * 56, 56, 56);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
Detected code, here are some useful tools:
you meant like this right ?
yes
also, you probably want to crash if it doesn't find it
with a if (is == null) ?
You would normally do a if (foo != false) {your code} else { var is null }
okey tysm for the help