how to keep moving an object

+2 Josaped Moreno · October 19, 2014
hello guys, newbie here,

I would like to ask how to move an object or picture in a single click of up button on my keyboard?

this is my code:

I'm using java slick.

boxY is the Y location of the image and the deltaY is supposed to be adding increment to Y direction after a single click of the arrow key without holding pressed it. thanks

    boxY -= deltaY;
    if(boxY <= 0){
    boxY = 0;

Post a Reply


- page 2
Oldest  Newest  Rating
0 Alex Sweps · October 20, 2014
Hmmm im not too sure what the problem is. If you are refreshing at around 25fps and your keypressed is adding to the axis then it should be moving smoothly untill you lift your finger.  Are you able to post any more code so we can see how its all communicating?
0 Josaped Moreno · November 9, 2014
Hi again. I've manage how to move and understand those codes :) but I hope you're still here to help me, I don't want to make another topic to this forum, instead I wan't to update this post.

I hope it's ok to you if i ask again. :) I just want to know how to rotate an image according to my mouseMoved() method, again in java slick2d. I've manage to move the image to where the mouse go inside the screen that I have created then give the image the limit to stop if it hits the border of the screen. What I want is, for example, if I move the mouse pointer circling inside the screen, the image will rotate accordingly or follow the pointer (cursor) by circling around without the image moving or following the mouse's cursor.

If I move the cursor to the right, the front or up direction of the image will rotate to the right, same to left, up and down, but the image will not move or it will stay where it is. Sorry for my grammar or my English, I'm not very good in English but I hope you understand what I wanted to point out.

here's what I did to move the image to where the mouse pointer go inside the screen.

//mouse listener interface
public void mouseMoved(int oldx, int oldy, int newx, int newy) {
super.mouseMoved(oldx, oldy, newx, newy);
robotX = newx;
robotY = newy;

robotX and robotY are integer coordinates of the image.

Thank you again in advance.
0 Alex Sweps · October 20, 2014
Not sure if you should be creating a new input object with each update. Input input = gc.getInput();
Just a tiny mistake with boxY -= delta * .1f;      Should be deltaY. 

Im not sure why that is not working though. Maybe someone else with some more experience in games programming can help because from what im seeing that looks good unless the new input object on every update is messing up your key presses. Iv only written one game and its using similar code to what you have there so we could be overlooking something simple.

I would like to know the solution when you find the problem... 
0 Josaped Moreno · October 21, 2014
This code
boxY -= deltaY;

Works fine if I hold down the arrow up key but when I release it, the object stops. Can't find the solution yet on how can I continuously move the object after Iv pressed the arrow up key. Anyway thank you so much for your help, I gues I have to search more. Will update this thread if I get the solution.
0 Alex Sweps · October 21, 2014
Ahhhhh. I thought your code litrally wasent working.... Create a method with a boolean argument. Something like:

public void setMotion(Boolean motion){
                    boxY -= deltaY

Then you can have your box stopped and when you press the up arrow include so this:

if(boxY <= 0){
boxY = 0;

Something like that will work but you may need to play around with it to get it perfect. 
-1 Josaped Moreno · October 20, 2014
This is my Play class:

package javagame;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Play extends BasicGameState{

private Animation box, Up, Down, Left, Right;
private static final String mess = "Game Over!";
private Image background, headBox;
private boolean quit = false;
private boolean gameover = false;
private int width = 25;
private int height =25;
private int backgroundX = 0;
private int backgroundY = 0;
private float boxX = 375;
private float boxY = 375;
private float deltaX = 0.2f;
private float deltaY = 0.2f;

public Play(int state){

public void setBoxX(float boxX) {
this.boxX = boxX;

public void setBoxY(float boxY) {
this.boxY = boxY;

public float getBoxX() {
return boxX;

public float getBoxY() {
return boxY;

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
background = new Image("res/background.png");
headBox = new Image("res/head.png");

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
background.draw(backgroundX, backgroundY);
headBox.draw(boxX, boxY);


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

boxY -= delta * .1f;
if(boxY <= 0){
boxY += delta * .1f;

boxX += deltaX;
if(boxX + width >= gc.getWidth() - 1){
boxX = gc.getWidth() - width;
deltaX += 0.01f;

public int getID(){
return 1;

I've tried different codes but still no luck. Thank you.
  • 1
  • 2

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator