Jump to content
  • Advertisement
Sign in to follow this  
Souvannasouck Mark

Animation is buggy in Java

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

My name is Mark Souvannasouck and I am developing a game for a contest.I am having a hard time identifying the issue in the animation class.It doesn't display the animation correctely and i don't know what i did wrong.I spend nights searching for an answer but i couldn't find anything.So can someone please look on the code and the sprite,please.I developed it in Eclipse Neon.Im new on this forum so I am sorry if i missed something.

 

Share this post


Link to post
Share on other sites
Advertisement

1. Wrong forum

2. I'm not sure how you expect us to help if you haven't even posted source code or explained the problem properly (what exactly is wrong?)

Share this post


Link to post
Share on other sites

Moved to general programming.

As Zoashi points out, it would be good to start with a full description of the problem, followed by the code you believe may be at fault. Currently you appear to have posted the compiled code. When I used an online decompiler ( view a version of the source code, I see 

package Entity;
 
import java.awt.image.BufferedImage;
 
 
public class Animation
{
  private BufferedImage[] frames;
  private int currentFrame;
  private int numFrames;
  private int count;
  private int delay;
  private int timesPlayed;
  
  public Animation()
  {
    timesPlayed = 0;
  }
  
  public void setFrames(BufferedImage[] frames) {
    this.frames = frames;
    currentFrame = 0;
    count = 0;
    timesPlayed = 0;
    delay = 2;
    numFrames = frames.length;
  }
  
  public void setDelay(int i) { delay = i; }
  public void setFrame(int i) { currentFrame = i; }
  public void setNumFrames(int i) { numFrames = i; }
  
  public void update()
  {
    if (delay == -1) { return;
    }
    count += 1;
    
    if (count == delay) {
      currentFrame += 1;
      count = 0;
    }
    if (currentFrame == numFrames) {
      currentFrame = 0;
      timesPlayed += 1;
    }
  }
  
 
  public int getFrame() { return currentFrame; }
  public int getCount() { return count; }
  public BufferedImage getImage() { return frames[currentFrame]; }
  public boolean hasPlayedOnce() { return timesPlayed > 0; }
  public boolean hasPlayed(int i) { return timesPlayed == i; }
}

Hard to tell from that, most likely any bug is in how the calling code interacts with this class.

Some good questions to ask:

  • Is there an error? If so, post it and the affected code.
  • Is something unexpected happening? Describe the expected behaviour and what is actually happening.
Edited by rip-off

Share this post


Link to post
Share on other sites

Im sorry,I usually don't pay attention to detail because I am dumb and always in a hurry.I forgot to attach the code from where the animation is generated.

package Entity;

import Audio.AudioPlayer;
import TileMap.TileMap;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import javax.imageio.ImageIO;











public class Player
  extends MapObject
{
  private ArrayList<Enemy> enemies;
  private int health;
  private int maxHealth;
  private int fire;
  private int maxFire;
  private boolean dead;
  private boolean flinching;
  private long flinchTimer;
  private int damage;
  private boolean firing;
  private int fireCost;
  private int fireBallDamage;
  private ArrayList<FireBall> fireBalls;
  private boolean scratching;
  private int scratchDamage;
  private int scratchRange;
  private boolean gliding;
  private ArrayList<BufferedImage[]> sprites;
  private final int[] numFrames = {
    1, 8, 5, 3, 3, 5, 3, 8, 2, 1, 3 };
  

  private final int[] FRAMEWIDTHS = {
    40, 40, 80, 40, 40, 40, 80, 40, 40, 40, 40 };
  
  private final int[] FRAMEHEIGHTS = {
    40, 40, 40, 40, 40, 80, 40, 40, 40, 40, 40 };
  
  private final int[] SPRITEDELAYS = {
    -1, 3, 2, 6, 5, 2, 2, 2, 1, -1, 1 };
  
  private Rectangle ar;
  
  private Rectangle aur;
  
  private Rectangle cr;
  
  private static final int IDLE = 0;
  
  private static final int WALKING = 1;
  
  private static final int JUMPING = 2;
  private static final int FALLING = 3;
  private static final int GLIDING = 4;
  private static final int FIREBALL = 5;
  private static final int SCRATCHING = 6;
  private HashMap<String, AudioPlayer> sfx;
  private boolean isFalling;
  
  public Player(TileMap tm)
  {
    super(tm);
    ar = new Rectangle(0, 0, 0, 0);
    ar.width = 30;
    ar.height = 20;
    aur = new Rectangle((int)x - 15, (int)y - 45, 30, 30);
    cr = new Rectangle(0, 0, 0, 0);
    cr.width = 50;
    cr.height = 40;
    
    width = 30;
    height = 30;
    cwidth = 15;
    cheight = 38;
    
    moveSpeed = 0.3D;
    maxSpeed = 1.6D;
    stopSpeed = 0.4D;
    fallSpeed = 0.15D;
    maxFallSpeed = 4.0D;
    jumpStart = -4.8D;
    stopJumpSpeed = 0.3D;
    
    facingRight = true;
    
    health = (this.maxHealth = 5);
    

    fireCost = 200;
    fireBallDamage = 5;
    fireBalls = new ArrayList();
    
    scratchDamage = 8;
    scratchRange = 40;
    

    try
    {
      BufferedImage spritesheet = ImageIO.read(
        getClass().getResourceAsStream(
        "/Sprites/Player/playersprites.gif"));
      

      int count = 0;
      sprites = new ArrayList();
      for (int i = 0; i < numFrames.length; i++)
      {
        BufferedImage[] bi = 
          new BufferedImage[numFrames[i]];
        
        for (int j = 0; j < numFrames[i]; j++)
        {
          if (i != 6) {
            bi[j] = spritesheet.getSubimage(
              j * FRAMEWIDTHS[i], count, FRAMEWIDTHS[i], FRAMEHEIGHTS[i]);
          }
          else
          {
            bi[j] = spritesheet.getSubimage(
              j * FRAMEWIDTHS[i], count, FRAMEWIDTHS[i], FRAMEHEIGHTS[i]);
          }
        }
        


        sprites.add(bi);
        count += FRAMEHEIGHTS[i];
      }
      
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    

    animation = new Animation();
    currentAction = 0;
    animation.setFrames((BufferedImage[])sprites.get(0));
    animation.setDelay(400);
    
    sfx = new HashMap();
    sfx.put("jump", new AudioPlayer("/SFX/jump.mp3"));
    sfx.put("scratch", new AudioPlayer("/SFX/scratch.mp3"));
  }
  
  public void reset() {
    health = maxHealth;
    facingRight = true;
    currentAction = -1;
    stop();
  }
  
  public void stop() { left = (this.right = this.up = this.down = this.flinching = 0); }
  
  private void setDead(WindowEvent evt)
  {
    health = 0;
    System.exit(0);
  }
  
  public int getHealth() { return health; }
  public int getMaxHealth() { return maxHealth; }
  public int getFire() { return fire; }
  public int getMaxFire() { return maxFire; }
  
  public void setFiring() {
    firing = true;
  }
  
  public void setScratching() { scratching = true; }
  
  public void setGliding(boolean b) {
    gliding = b;
  }
  

  public void checkAttack(ArrayList<Enemy> enemies)
  {
    for (int i = 0; i < enemies.size(); i++)
    {
      Enemy e = (Enemy)enemies.get(i);
      

      if (scratching) {
        if (facingRight)
        {
          if ((e.getx() > x) && 
            (e.getx() < x + scratchRange) && 
            (e.gety() > y - height / 2) && 
            (e.gety() < y + height / 2))
          {
            e.hit(scratchDamage);
          }
          

        }
        else if ((e.getx() < x) && 
          (e.getx() > x - scratchRange) && 
          (e.gety() > y - height / 2) && 
          (e.gety() < y + height / 2))
        {
          e.hit(scratchDamage);
        }
      }
    }
  }
  


















  public void hit(int damage)
  {
    if (flinching) return;
    health -= damage;
    if (health < 0) health = 0;
    if (health == 0) dead = true;
    flinching = true;
    flinchTimer = System.nanoTime();
  }
  
  public void setJumping(boolean b) { if (flinching) return;
    if ((b) && (!jumping)) {}
    

    jumping = b;
  }
  



  private void getNextPosition()
  {
    double maxSpeed = this.maxSpeed;
    

    if (left) {
      dx -= moveSpeed;
      if (dx < -maxSpeed) {
        dx = (-maxSpeed);
      }
    }
    else if (right) {
      dx += moveSpeed;
      if (dx > maxSpeed) {
        dx = maxSpeed;
      }
      
    }
    else if (dx > 0.0D) {
      dx -= stopSpeed;
      if (dx < 0.0D) {
        dx = 0.0D;
      }
    }
    else if (dx < 0.0D) {
      dx += stopSpeed;
      if (dx > 0.0D) {
        dx = 0.0D;
      }
    }
    
    if ((jumping) && (!falling))
    {
      dy = jumpStart;
      falling = true;
      isFalling = falling;
      getNextPosition();
      checkTileMapCollision();
      setPosition(xtemp, ytemp);
      

      if ((!jumping) && (!falling)) {
        dx = 0.0D;
      }
    }
    
    int i = 0;
    currentAction = i;
    animation.setFrames((BufferedImage[])sprites.get(currentAction));
    animation.setDelay(SPRITEDELAYS[currentAction]);
    width = FRAMEWIDTHS[currentAction];
    height = FRAMEHEIGHTS[currentAction];
    


    if (((currentAction == 6) || (currentAction == 5)) && 
      (!jumping) && (!falling)) {
      dx = 0.0D;
    }
    

    if ((jumping) && (!falling)) {
      ((AudioPlayer)sfx.get("jump")).play();
      dy = jumpStart;
      falling = true;
    }
    

    if (falling)
    {
      if ((dy > 0.0D) && (gliding)) dy += fallSpeed * 0.1D; else {
        dy += fallSpeed;
      }
      if (dy > 0.0D) jumping = false;
      if ((dy < 0.0D) && (!jumping)) { dy += stopJumpSpeed;
      }
      if (dy > maxFallSpeed) { dy = maxFallSpeed;
      }
    }
  }
  



  public void update()
  {
    getNextPosition();
    checkTileMapCollision();
    setPosition(xtemp, ytemp);
    

    if ((currentAction == 6) && 
      (animation.hasPlayedOnce())) { scratching = false;
    }
    if ((currentAction == 5) && 
      (animation.hasPlayedOnce())) { firing = false;
    }
    

    fire += 1;
    if (fire > maxFire) fire = maxFire;
    if ((firing) && (currentAction != 5) && 
      (fire > fireCost)) {
      fire -= fireCost;
      FireBall fb = new FireBall(tileMap, facingRight);
      fb.setPosition(x, y);
      fireBalls.add(fb);
    }
    












    if (flinching) {
      long elapsed = 
        (System.nanoTime() - flinchTimer) / 1000000L;
      if (elapsed > 1000L) {
        flinching = false;
      }
    }
    

    if (scratching) {
      if (currentAction != 6) {
        ((AudioPlayer)sfx.get("scratch")).play();
        currentAction = 6;
        animation.setFrames((BufferedImage[])sprites.get(6));
        animation.setDelay(50);
        width = 60;
      }
    }
    else if (firing) {
      if (currentAction != 5) {
        currentAction = 5;
        animation.setFrames((BufferedImage[])sprites.get(5));
        animation.setDelay(100);
        width = 30;
      }
    }
    else if (dy > 0.0D) {
      if (gliding) {
        if (currentAction != 4) {
          currentAction = 4;
          animation.setFrames((BufferedImage[])sprites.get(4));
          animation.setDelay(100);
          width = 30;
        }
      }
      else if (currentAction != 3) {
        currentAction = 3;
        animation.setFrames((BufferedImage[])sprites.get(3));
        animation.setDelay(100);
        width = 30;
      }
    }
    else if (dy < 0.0D) {
      if (currentAction != 2) {
        currentAction = 2;
        animation.setFrames((BufferedImage[])sprites.get(2));
        animation.setDelay(-1);
        width = 30;
      }
    }
    else if ((left) || (right)) {
      if (currentAction != 1) {
        currentAction = 1;
        animation.setFrames((BufferedImage[])sprites.get(1));
        animation.setDelay(40);
        width = 30;
      }
      
    }
    else if (currentAction != 0) {
      currentAction = 0;
      animation.setFrames((BufferedImage[])sprites.get(0));
      animation.setDelay(400);
      width = 30;
    }
    

    animation.update();
    

    if ((currentAction != 6) && (currentAction != 5)) {
      if (right) facingRight = true;
      if (left) { facingRight = false;
      }
    }
  }
  
  public void draw(Graphics2D g)
  {
    setMapPosition();
    







    if (flinching) {
      long elapsed = 
        (System.nanoTime() - flinchTimer) / 1000000L;
      if (elapsed / 100L % 2L == 0L) {
        return;
      }
    }
    
    super.draw(g);
  }
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!