Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8085173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:01:57+00:00 2026-06-05T18:01:57+00:00

What I have right now is an animation that starts when I press space:

  • 0

What I have right now is an animation that starts when I press space:

if(input.isKeyDown(Input.KEY_SPACE)) player = movingRightSwingingSword;

However when I stop clicking space my animation continues. until I move my player, how do I make it so it stops the animation right when you let go of space?

A few lines

Animation player, movingUp, movingDown, movingLeft, movingRight, movingRightSwingingSword;
int[] duration = {200,200};

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
    {
    Image[] attackRight = {new Image("res/buckysRightSword1.png"), new Image("res/buckysRightSword2.png")};

    movingRightSwingingSword = new Animation(attackRight, duration, true);
    }

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
    {
     if(input.isKeyDown(Input.KEY_SPACE)) player = movingRightSwingingSword;
     }

Full Code

package javagame;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Play extends BasicGameState
{
    Animation player, movingUp, movingDown, movingLeft, movingRight, movingRightSwingingSword;
    Image worldMap;
    boolean quit = false;
    int[] duration = {200,200};
    float playerPositionX = 0;
    float playerPositionY = 0;
    float shiftX = playerPositionX + 320;
    float shiftY = playerPositionY + 160;

    public Play(int state)
    {

    }

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
    {
        worldMap = new Image("res/world.png");
        Image[] walkUp = {new Image("res/buckysBack.png"), new Image("res/buckysBack.png")};
        Image[] walkDown = {new Image("res/buckysFront.png"), new Image("res/buckysFront.png")};
        Image[] walkLeft = {new Image("res/buckysLeft.png"), new Image("res/buckysLeft.png")};
        Image[] walkRight = {new Image("res/buckysRight.png"), new Image("res/buckysRight.png")};
        Image[] attackRight = {new Image("res/buckysRightSword1.png"), new Image("res/buckysRightSword2.png")};

        movingUp = new Animation(walkUp, duration, false);
        movingDown = new Animation(walkDown, duration, false);
        movingLeft = new Animation(walkLeft, duration, false);
        movingRight = new Animation(walkRight, duration, false);
        //doesnt work! vvv
        movingRightSwingingSword = new Animation(attackRight, duration, true);
        player = movingDown;

    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException
    {
        worldMap.draw(playerPositionX, playerPositionY);
        player.draw(shiftX, shiftY);
        g.drawString("Player X: " + playerPositionX + "\nPlayer Y: " + playerPositionY, 400, 20);

        if (quit == true)
        {
            g.drawString("Resume (R)", 250, 100);
            g.drawString("MainMenu (M)", 250, 150);
            g.drawString("Quit Game (Q)", 250, 200);
            if (quit==false)
            {
                g.clear();
            }
        }
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
    {
        Input input = gc.getInput();

        if(input.isKeyDown(Input.KEY_UP)) 
        {
            player = movingUp;
            playerPositionY += delta * .1f;
            if(playerPositionY>162) playerPositionY -= delta * .1f; 
        }

        if(input.isKeyDown(Input.KEY_DOWN)) 
        {
            player = movingDown;
            playerPositionY -= delta * .1f;
            if(playerPositionY<-600) playerPositionY += delta * .1f; 
        }

        if(input.isKeyDown(Input.KEY_RIGHT)) 
        {
            player = movingRight;
            playerPositionX -= delta * .1f;
            if(playerPositionX<-840) playerPositionX += delta * .1f; 
        }

        if(input.isKeyDown(Input.KEY_LEFT)) 
        {
            player = movingLeft;
            playerPositionX += delta * .1f;
            if(playerPositionX>318) playerPositionX -= delta * .1f; 
        }

        if(input.isKeyDown(Input.KEY_SPACE)) player = movingRightSwingingSword;

        if(input.isKeyDown(Input.KEY_ESCAPE)) quit = true;
        if(input.isKeyDown(Input.KEY_R)) if (quit == true) quit = false;
        if(input.isKeyDown(Input.KEY_M)) if (quit == true) {sbg.enterState(0); quit = false;}
        if(input.isKeyDown(Input.KEY_Q)) if (quit == true) System.exit(0);
    }

    public int getID()
    {
        return 1;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:01:59+00:00Added an answer on June 5, 2026 at 6:01 pm

    In the update, check if the current animation is movingRightSwingingSword and, if it is, check if it is ended (guess the duration can be used for that).

    I think it could be something like this:

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
       if(input.isKeyDown(Input.KEY_SPACE)) player = movingRightSwingingSword;
       //This is the new code, I don´t know what are the real functions to be used, but the concept is this
       if (player == movingRightSwingingSword && player.ended()) player = defaultAnimation;
    }
    

    Edit: If you want to change it when the spacebar key is realeased, just change the animation in the else part of the condition:

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
           if(input.isKeyDown(Input.KEY_SPACE)) player = movingRightSwingingSword;
           else if (player == movingRightSwingingSword) player = defaultAnimation;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application right now that starts a process, then opens a file
I have some code right now that executes when a link is clicked. The
I actually have right now two questions: 1) What font faces are preferred for
This is what I have right now for a file residing on the same
I have this right now: <%= f.select :credit, (0..500) %> This will result in
I have a problem right now with CodeIgniter : I use the REST Controller
We have a script right now which our Windows users run on a Linux
I have this code right now to get the row value from jquery grid..
I am new to NHibernate and have just started right now. I have very
I have an UIActivityIndicator that starts animating on the top of all of my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.