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 Josaped Moreno · October 25, 2014
wow it worked. Thank you so much!
0 Josaped Moreno · October 26, 2014
I have another question if you don't mind.

could you tell me what this code means?


for(int z = dots; z > 0; z--){
  x[z] = y[(z - 1)];
  y[z] = y[(z - 1)];

This is the declations:

private final int DOT_SIZE = 10;
private final int ALL_DOTS = 900;
private final int RANDOM_POS = 29;
private final int DELAY = 140;

private final int x[] = new int[ALL_DOTS];
private final int y[] = new int[ALL_DOTS];

private int dots;

0 Alex Sweps · October 26, 2014
Answering questions hungover is so much harder than I thought haha but il do my best.

Ok so your for loop is basically creating an X and a Y array with the indexes going backwards. Each iteration of the loop is adding the value of Y to x untill x is filled.

Hmmm how do I explain this...

ALL_DOTS = 900;
private final int x[] = new int[ALL_DOTS]    //This is creating an integer array that will hold 900 items as ALL_DOTS is set to 900. Exactly same thing for y[]

First iteration is doing this:
x[900] = y[899]; //At position x[900] we want to store the value that is in y[899]

The next iteration through the loop subtracts 1 from z. So now it would be doing this:
x[899] = y[898]; //At position x[899] we want to store the value that is in y[898]

Once all of the indexes in the x array have been filled stop the loop. 

Add a System.out.println(x[z])  at the bottom of the for loop and look at what it prints out. You should see all the numbers counting backwards.

Yeah maybe someone can explain it better than me.
0 Josaped Moreno · October 26, 2014
haha I'm still confused:'( but thank you, I will hardly try to understand it, probably read your answer many times haha!:D:D:D
0 Alex Sweps · October 26, 2014
Yeha just throw in some System.out.println  throughout the loop so you can see what its doing more closely. Change some values around and see what happens.
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.
  • 1
  • 2

Java / Android Development


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

Bucky Roberts Administrator