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 1
Oldest  Newest  Rating
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 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 · 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
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
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 Josaped Moreno · October 25, 2014
wow it worked. Thank you so much!
+1 Alex Sweps · October 21, 2014
Or start your deltaY at 0. Then use your up arrow to call a method to set the speed. Thats probably cleaner. 

float deltaY = 0;

private void setDeltaY(float speed){
deltaY = speed;

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
Input input = gc.getInput();
if(boxY <= 0){
boxY = 0;
boxY += deltaY;

The key press will trigger the method and the else statement will start the boxes rolling once the lift your finger. This has the benifit of being able to use that method throughout your program to adjust the boxes speed aswell.
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. 
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 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... 
  • 1
  • 2

Java / Android Development


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

Bucky Roberts Administrator