0

I have multiple sprite sheets for the game, but I can't add the .png file properly into the code. It's seem to be working but it throws some errors every once in a while. And the attack animation doesn't animate fully.

Here is what I've done so far. The idle and run animation seem to be fine sometimes it shows some errors. The reason I'm having the array for bufferedimage is to avoid cropping the sprite into "run-1","run-2", etc.

the errors it shows

package entity;


import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

public class Player extends Entity{

    private int aniTick, aniIndex, aniSpeed = 30;
    private boolean left, up, right, down;
    private boolean moving = false, attacking = false;
    private BufferedImage[] idleAni, runAni, atkAni, hurtAni, jumpAni, dedAni;
    private float playerSpeed =  2.0f;
    public Player(float x, float y) {
        
        super(x, y);
        loadAnimation();
        
    }
    
    public void update() {
        
        updatePos();
        updateAnimationTick();
        
    }
    public void render(Graphics g) {
        
        if(moving) {
            g.drawImage(runAni[aniIndex], (int)x, (int)y, null);
        }
        else if(attacking) {
            g.drawImage(atkAni[aniIndex], (int)x, (int)y, null);
        }
        else g.drawImage(idleAni[aniIndex], (int)x, (int)y, null);
        
    
    }
    
    
    private void updatePos() {
        // TODO Auto-generated method stub
        moving =false;
        
        if(left && !right) {
            x-= playerSpeed;
            moving =true;
        } else if (right && !left) {
            x+= playerSpeed;
            moving =true;
        }
        
        if(up && !down) {
            y-= playerSpeed;
            moving =true;
        } else if (down && !up) {
            y+= playerSpeed;
            moving =true;
        }
    }

    private void updateAnimationTick() {
        // TODO Auto-generated method stub
        aniTick++;
        if(aniTick >=aniSpeed) {
            aniTick = 0;
            aniIndex++;
            if(moving) {
                if(aniIndex >= runAni.length) {
                    aniIndex = 0;
                    attacking = false;
                }
            }
            else if(aniIndex >= idleAni.length) {
                aniIndex = 0;
                attacking = false;
            }
            else if(attacking) {
                if(aniIndex >= atkAni.length) {
                    aniIndex = 0;
                    attacking = false;
                }
            }
        }
    }
    
    
    private void loadAnimation() {
        // TODO Auto-generated method stub
        InputStream is = getClass().getResourceAsStream("/Idle.png");
        InputStream is1 = getClass().getResourceAsStream("/Run.png");
        InputStream is2 = getClass().getResourceAsStream("/Attack_1.png");
        InputStream is3 = getClass().getResourceAsStream("/Dead.png");
        InputStream is4 = getClass().getResourceAsStream("/Hurt.png");
        InputStream is5 = getClass().getResourceAsStream("/Jump.png");
        
        try {
            BufferedImage idle = ImageIO.read(is);
            BufferedImage run = ImageIO.read(is1);
            BufferedImage atk = ImageIO.read(is2);
            BufferedImage ded = ImageIO.read(is3);
            BufferedImage hurt = ImageIO.read(is4);
            BufferedImage jump = ImageIO.read(is5);
            
            idleAni = new BufferedImage[7];
            runAni = new BufferedImage[8];
            atkAni = new BufferedImage[10];
            hurtAni = new BufferedImage[3];
            jumpAni = new BufferedImage[8];
            dedAni = new BufferedImage[5];
            
            for(int i=0; i<idleAni.length; i++) {
                idleAni[i] = idle.getSubimage(i*128, 0, 128, 128);
            }
            for(int a=0; a<runAni.length; a++) {
                runAni[a] = run.getSubimage(a*128, 0, 128, 128);
            }
            for(int b=0; b<10; b++) {
                atkAni[b] = atk.getSubimage(b*128, 0, 128, 128);
            }
            for(int c=0; c<hurtAni.length; c++) {
                hurtAni[c] = hurt.getSubimage(c*128, 0, 128, 128);
            }
            for(int d=0; d<jumpAni.length; d++) {
                jumpAni[d] = jump.getSubimage(d*128, 0, 128, 128);
            }
            for(int e=0; e<dedAni.length; e++) {
                dedAni[e] = ded.getSubimage(e*128, 0, 128, 128);
            }
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
    }
    
    public void resetDirBoolean() {
        left = false;
        right = false;
        down = false;
        up = false;
    }
    
    public void setAttack(boolean atk) {
        this.attacking = atk;
    }

    public boolean isLeft() {
        return left;
    }

    public void setLeft(boolean left) {
        this.left = left;
    }

    public boolean isUp() {
        return up;
    }

    public void setUp(boolean up) {
        this.up = up;
    }

    public boolean isRight() {
        return right;
    }

    public void setRight(boolean right) {
        this.right = right;
    }

    public boolean isDown() {
        return down;
    }

    public void setDown(boolean down) {
        this.down = down;
    }
    
    

}

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Please don't post images of text. Copy the stack trace and paste it (formatted) into your question. – tgdavies Jul 23 '23 at 00:03
  • For the "attack animation not animating fully": look at the order of your else clauses in `updateAnimationTick`. – tgdavies Jul 23 '23 at 00:05
  • I wouldn't be trying to track multiple arrays with different lengths with a single index, instead, I'd be making each sheet it's own self contained context - maybe something more like [this](https://stackoverflow.com/questions/35472233/load-a-sprites-image-in-java/35472418#35472418) – MadProgrammer Jul 23 '23 at 00:32
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Jul 23 '23 at 21:11

0 Answers0